在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Java  Python/ keras lstm模型 fit函數(shù)報(bào)錯(cuò)

keras lstm模型 fit函數(shù)報(bào)錯(cuò)

# build model


model = Sequential()
model.add(LSTM(20, return_sequences=True,
               input_shape=(max_len, data_dim)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(train_x, train_y, batch_size=16, epochs=10)
score = model.evaluate(test_x, test_y, batch_size=16)

print(score)

輸入數(shù)據(jù)的維度信息:
shape of train_x: (409, 60, 30)
shape of test_x: (103, 60, 30)
shape of train_y: (409, 1)
shape of test_y: (103, 1)

為什么會(huì)報(bào)錯(cuò)如下:

F:\Anaconda\envs\tensorflow\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1591             class_weight=class_weight,
   1592             check_batch_axis=False,
-> 1593             batch_size=batch_size)
   1594         # Prepare validation data.
   1595         do_validation = False

F:\Anaconda\envs\tensorflow\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
   1428                                     output_shapes,
   1429                                     check_batch_axis=False,
-> 1430                                     exception_prefix='target')
   1431         sample_weights = _standardize_sample_weights(sample_weight,
   1432                                                      self._feed_output_names)

F:\Anaconda\envs\tensorflow\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    108                         ': expected ' + names[i] + ' to have ' +
    109                         str(len(shape)) + ' dimensions, but got array '
--> 110                         'with shape ' + str(data_shape))
    111                 if not check_batch_axis:
    112                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_8 to have 3 dimensions, but got array with shape (409, 1)
回答
編輯回答
荒城

把LSTM層的return_sequences改為False即可正確運(yùn)行

2017年10月6日 02:46