【问题标题】:Keras (tensorflow backend) getting "TypeError: unhashable type: 'Dimension'"Keras(张量流后端)得到“TypeError:unhashable type:'Dimension'”
【发布时间】:2018-01-24 06:18:26
【问题描述】:

您好,我在拟合此模型时遇到尺寸错误,有人知道为什么吗?

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

每个变量的维度是

X_train = (27367, 64, 64, 1)
X_test = (4553, 64, 64, 1)
y_train = (164202, 11)
y_test = (27318, 11)

【问题讨论】:

  • 你能发布完整的堆栈跟踪吗?

标签: python tensorflow neural-network keras conv-neural-network


【解决方案1】:

这是因为您使用的是 tf.reshape,它返回一个张量,而 Keras 模型的 fit 方法不适用于张量。

考虑改用np.reshape,它会做同样的事情。

【讨论】:

  • 是的,现在可以使用了。非常感谢。基本上,我需要对其进行重塑,然后为其添加频道。即 X_train= X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
  • 我该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多