我有DNA数据作为Keras的输入,DNA数据是一个单一的编码阵列,每个DNA序列是4个通道(每种类型的核苷酸一个)。单热矩阵在matlab中,其尺寸为:(4,400,100)100个样本。
第一个matlab有尺寸row * 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)
答案 0 :(得分:0)
试试这个版本:
import numpy as np
from keras.models import Sequential
from keras.layers import Conv1D, MaxPooling1D, Dropout, Dense, Flatten
# This generates some test sample for me to check your code
X_train = np.random.rand(100, 4, 400)
Y_train = np.random.rand(100, 2)
model = Sequential()
model.add(Conv1D(32, 3, activation='relu', input_shape=(4, 400)))
model.add(MaxPooling1D(2))
model.add(Dropout(0.5))
model.add(Flatten()) # <- You need a flatten here
model.add(Dense(128, activation='relu'))
model.add(Dense(2, activation='sigmoid')) # <- the last dense must have output 2
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=16, epochs=10)
Flatten
之后添加了Dropout
图层(请记住将其导入)Dense
图层设为Dense(2)
如果您将输出更改为包含尺寸(1,),那么请再次放入Dense(1)
,同时将损失从categorical_crossentropy
更改为binary_crossentropy