how i can create 3d input / 3d output Convolution model with keras?

一个人想着一个人 提交于 2021-02-20 05:12:34

问题


I have a bit question i couldnt solve.

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.

Also i want

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)

Ok, i understand that Dense should take positive integer unit (explanation in Keras docs. ). But how i can implement matrix output to my model ?

I tried that:

model.add(Dense((1287,8), activation="softmax"))

and something else but i couldnt find any solution.

Thanks very much.


回答1:


The Conv2D layer requires an input shape of (batch_size, height, width, channels). This means that each sample is a 3D array.

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.

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.

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)


来源:https://stackoverflow.com/questions/53601593/how-i-can-create-3d-input-3d-output-convolution-model-with-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!