Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras (tensorflow backend) getting "TypeError: unhashable type: 'Dimension'"

Hi I get a dimension error when fitting this model, would anyone know why?

num_classes = 11
input_shape = (64,64,1)
batch_size = 128
epochs = 12
X_train = tf.reshape(X_train, [-1, 64, 64, 1])
X_test = tf.reshape(X_test, [-1, 64, 64, 1])

model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=1, activation='relu', input_shape=input_shape)) 
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
          optimizer=keras.optimizers.Adadelta(),
          metrics=['accuracy'])

model.fit(X_train, y_train,
      batch_size=batch_size,
      epochs=epochs,
      verbose=1,
      validation_data=(X_test, y_test))

The dimension of each variable is

X_train = (27367, 64, 64, 1)
X_test = (4553, 64, 64, 1)
y_train = (164202, 11)
y_test = (27318, 11)
like image 708
Joe Phongpreecha Avatar asked Jun 06 '26 08:06

Joe Phongpreecha


1 Answers

This is because you are using tf.reshape, which returns a Tensor, and the fit method of Keras models don't work well with tensors.

Consider using np.reshape instead, which will do the exact same thing.

like image 102
havanagrawal Avatar answered Jun 08 '26 22:06

havanagrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!