我遇到一些麻烦Keras' LSTM。我已经重塑了一些数据(NUM_ROWS,num_timesteps,num_dimensions),但是当我尝试适合说我得到一个错误
TypeErrorTraceback (most recent call last)
<ipython-input-61-a1844d288e79> in <module>()
10 print("Actual input: {}".format(X.shape))
11 print("Actual output: {}".format(Y.shape))
---> 12 history = model.fit(X, Y, epochs=2, batch_size=100, verbose=1)
/opt/conda/lib/python3.6/site-packages/keras/models.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
843 class_weight=class_weight,
844 sample_weight=sample_weight,
--> 845 initial_epoch=initial_epoch)
846
847 def evaluate(self, x, y, batch_size=32, verbose=1,
/opt/conda/lib/python3.6/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, **kwargs)
1403 class_weight=class_weight,
1404 check_batch_axis=False,
-> 1405 batch_size=batch_size)
1406 # prepare validation data
1407 if validation_data:
/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1305 for (ref, sw, cw, mode)
1306 in zip(y, sample_weights, class_weights, self._feed_sample_weight_modes)]
-> 1307 _check_array_lengths(x, y, sample_weights)
1308 _check_loss_and_target_compatibility(y,
1309 self._feed_loss_fns,
/opt/conda/lib/python3.6/site-packages/keras/engine/training.py in _check_array_lengths(inputs, targets, weights)
208 y_lengths = [y.shape[0] for y in targets]
209 w_lengths = [w.shape[0] for w in weights]
--> 210 set_x = set(x_lengths)
211 if len(set_x) > 1:
212 raise ValueError('All input arrays (x) should have '
TypeError: unhashable type: 'Dimension'
我的代码,所有的进口和读取数据后,是
X = reshape(np.array(dat), [10000, 101, 26])
model = Sequential()
model.add(LSTM(1, input_shape=(101, 26), return_sequences=False))
model.compile(loss='binary_crossentropy',
optimizer= 'adam',
metrics=['binary_accuracy'])
model.summary()
print("Inputs: {}".format(model.input_shape))
print("Outputs: {}".format(model.output_shape))
print("Actual input: {}".format(X.shape))
print("Actual output: {}".format(Y.shape))
history = model.fit(X, Y, epochs=2, batch_size=100, verbose=1)
和完整性检查行给出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_19 (LSTM) (None, 1) 112
=================================================================
Total params: 112.0
Trainable params: 112
Non-trainable params: 0.0
_________________________________________________________________
Inputs: (None, 101, 26)
Outputs: (None, 1)
Actual input: (10000, 101, 26)
Actual output: (10000, 1)
我究竟做错了什么?
在情况下,如果其他人有同样的问题:尝试铸造为int,它为我工作。我得到这个错误试图CONCAT层通过以下方式重塑其中之一后:
shapes = previous_layer.shape
reshape_layer = Reshape( (shapes[1], shapes[3], 1) )(previous_layer) #as the shapes were: (?, shapes[1], 1, shapes[3])
concat_layer = Concatenate(axis = 1)([reshape_layer, some_other_layer_to_concat_with] )
所以在我的情况下,
reshape_layer = Reshape( (int(shapes[1]), int(shapes[3]), 1) )(previous_layer)
解决了这个问题。
找到了解决办法。该生产线
X = reshape(np.array(dat), [10000, 101, 26])
指着不同的重塑功能。我把它改成
X = np.reshape(np.array(dat), [10000, 101, 26])
其工作。