我如何使用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.
在我的喀拉拉邦代码下方:
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)
-2D形状.因此,您应该使用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,而您希望每个样本为2D.
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屋!