我有DNA数据作为Keras的输入,DNA数据是一个单一的编码阵列,每个DNA序列是4个通道(每种类型的核苷酸一个) . 单热矩阵在matlab中,其尺寸为:(4,400,100)100个样本 .
第一个matlab有维度行* cloumn * slice(4,400,100),但我改变尺寸得到(100,4,400)像python格式
import scipy.io
x = scipy.io.loadmat('x.mat')
x2 = x['x']
x2 = np.ascontiguousarray(x2.T)
x2 = np.ascontiguousarray(x2.swapaxes(1, 2))
X_train =x2
y = scipy.io.loadmat('y.mat')
y2 = y['y']
Y_train = np_utils.to_categorical(y2, 2)
现在X_train形状是:(100,4,400)Y_train形状是(100,2)
2)
我的模型是Conv1D看起来像这样:
model = Sequential()
model.add(Conv1D(32, 3, activation='relu', input_shape=(4, 400)))
model.add(MaxPooling1D(2))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=16, epochs=10)
错误按摩
Traceback (most recent call last): in <module>
model.fit(X_train, Y_train, batch_size=16, epochs=5)
in _standardize_user_data
exception_prefix='target')
in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (100, 2)
1 回答
试试这个版本:
我在
Dropout
之后添加了Flatten
图层(记得导入它)由于输出为(2,),您需要将第二个
Dense
图层设为Dense(2)
如果您将输出更改为具有尺寸(1,),然后再次放入
Dense(1)
,但也将损失从categorical_crossentropy
更改为binary_crossentropy