我如何使用 keras 创建 3d 输入/3d 输出卷积模型? [英] how i can create 3d input / 3d output Convolution model with keras?
问题描述
我有一个小问题无法解决.
I have a bit question i couldnt solve.
我想将具有完全连接的 MLP 的 CNN 模型实现到我的具有 2589 个蛋白质的蛋白质数据库中.每个蛋白质有 1287 行 69 列作为输入和 1287 行和 8 列作为输出.实际上有 1287x1 的输出,但我对类标签使用了一种热编码,以便在我的模型中使用交叉熵损失.
I wanna implement CNN model with fully-connected MLP to my protein database which has 2589 proteins. Each protein has 1287 rows and 69 columns as input and and 1287 rows and 8 columns as output. Actually there was 1287x1 output, but i used one hot encoding for class labels to use crossentropy loss in my model.
我也想要
如果我们将图像视为图像,我有一个 3d 矩阵 ** X_train = (2589, 1287, 69) 用于输入** 和 y_train =(2589, 1287, 8) 输出,我的意思是输出也是矩阵.
if we consider as image i have an 3d matrix ** X_train = (2589, 1287, 69) for input** and y_train =(2589, 1287, 8) output, i mean output is also matrix.
在我的 keras 代码下面:
Below my codes of keras:
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(Conv2D(32, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense((8), activation="softmax"))
但是我遇到了关于密集层的错误:
But I encountered with Error about Dense layer :
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (2589, 1287, 8)
好的,我知道 Dense 应该采用正整数单位(Keras 文档中的解释.).但是我如何实现矩阵输出到我的模型?
Ok, i understand that Dense should take positive integer unit (explanation in Keras docs. ). But how i can implement matrix output to my model ?
我试过了:
model.add(Dense((1287,8), activation="softmax"))
还有别的东西,但我找不到任何解决方案.
and something else but i couldnt find any solution.
非常感谢.
推荐答案
Conv2D
层需要 (batch_size, height, width, channels)
的输入形状.这意味着每个样本都是一个 3D 数组.
The Conv2D
layer requires an input shape of (batch_size, height, width, channels)
. This means that each sample is a 3D array.
您的实际输入是 (2589, 1287, 8)
意味着每个样本的形状都是 (1289, 8)
- 一个二维形状.因此,您应该使用 Conv1D
而不是 Conv2D
.
Your actual input is (2589, 1287, 8)
meaning that each sample is of shape (1289, 8)
- a 2D shape. Because of this, you should be using Conv1D
instead of Conv2D
.
其次,您需要 (2589, 1287, 8)
的输出.由于每个样本都是 2D 形状,因此 Flatten()
输入是没有意义的 - Flatten()
会将每个样本的形状减少到 1D,并且您想要每个样本都是二维的.
Secondly you want an output of (2589, 1287, 8)
. Since each sample is of a 2D shape, it makes no sense to Flatten()
the input - Flatten()
would reduce the shape of each sample to 1D, and you want each sample to be 2D.
最后,根据 Conv
层的填充,形状可能会根据 kernel_size
改变.由于要保留 1287
的中间尺寸,请使用 padding='same'
保持大小不变.
Finally depending on the padding of your Conv
layers,the shape may change based on the kernel_size
. Since you want to preserve the middle dimension of 1287
, use padding='same'
to keep the size the same.
from keras.models import Sequential
from keras.layers import Conv1D, Flatten, Dense
import numpy as np
X_train = np.random.rand(2589, 1287, 69)
y_train = np.random.rand(2589, 1287, 8)
model = Sequential()
model.add(Conv1D(64,
kernel_size=3,
activation="relu",
padding='same',
input_shape=(X_train.shape[1],X_train.shape[2])))
model.add(Conv1D(32,
kernel_size=3,
activation="relu",
padding='same'))
model.add(Dense((8), activation="softmax"))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(X_train, y_train)
这篇关于我如何使用 keras 创建 3d 输入/3d 输出卷积模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!