不可用类型:'维度'在Keras LSTM

时间:2017-08-14 08:20:24

标签: python tensorflow keras lstm

我在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)

我做错了什么?

2 个答案:

答案 0 :(得分:0)

找到解决方案。这条线

X = reshape(np.array(dat), [10000, 101, 26])

指的是一个不同的重塑功能。我把它改成了

X = np.reshape(np.array(dat), [10000, 101, 26])

哪个有效。

答案 1 :(得分:0)

万一其他人遇到同样的问题: 尝试将其转换为int,它对我有用。在以以下方式重塑其中的一层后,尝试合并图层时出现此错误:

pd.Series.value_counts

所以就我而言

NaN

解决了问题。