在Python中使用TimeDistributed会导致维度错误

时间:2017-09-06 01:02:10

标签: python deep-learning keras lstm

我正在使用Keras API - 特别是SimpleRNN和LSTM图层。

在尝试使用TimeDistributed图层时,我收到了一些我不理解的尺寸错误:

Error when checking target: expected activation_5 to have 3 dimensions, but got array with shape (3252, 2).

这是我的代码:

batch_size=32
nb_epoch=100
nb_classes=2

label=np.ones((total_length,), dtype='float32')
samples_per_class=2602 # number of normal


s=0
r=samples_per_class
for classIndex in range(nb_classes):
    label[s:r]=classIndex
    s=r


r=s+samples_per_class

data,label=shuffle(PPG,label,random_state=2)
train_data=[data,label]
(X,y)=(train_data[0],train_data[1])

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.375)
X_train = X_train.reshape(X_train.shape[0], 1000, 1)
X_test = X_test.reshape(X_test.shape[0] , 1000, 1)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255 
X_test /= 255

y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
y_train= np.array(y_train)
y_test = np.array(y_test)

model = Sequential()
model.add(SimpleRNN(16, input_shape=(1000,1), return_sequences=True,
                    activation='softsign', dropout_W=0.2, dropout_U=0.2))
model.add(SimpleRNN(16, return_sequences=True,activation='softsign',
                    dropout_W=0.2, dropout_U=0.2))
model.add(TimeDistributed(Dense(2)))
model.add(Activation('softmax'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer=Nadam(),
          metrics=['accuracy', 'binary_accuracy'])

0 个答案:

没有答案