unhashable type: 'Dimension' in Keras LSTM

Viewed 3702

I'm having some trouble with Keras' LSTM. I've reshaped some data into (num_rows, num_timesteps, num_dimensions) but I'm getting an error when I try to fit saying

        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'

My code, after all the imports and reading in the data, is

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)

and the sanity check lines give:

_________________________________________________________________
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)

What am I doing wrong?

2 Answers

In case if anyone else has the same problem: Try casting to int, it worked for me. I got this error trying to concat layers after reshaping one of them in the following way:

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] )

So in my case

reshape_layer = Reshape( (int(shapes[1]), int(shapes[3]), 1) )(previous_layer)

solved the problem.

Related