python - Keras( tensorflow 后端)获取 "TypeError: unhashable type: ' Dimension'"

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

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

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)

最佳答案

这是因为您使用的是 tf.reshape,它返回一个张量,而 Keras 模型的 fit 方法不能很好地处理张量。

考虑使用 np.reshape 来代替,它会做完全相同的事情。

关于python - Keras( tensorflow 后端)获取 "TypeError: unhashable type: ' Dimension'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48415957/

相关文章:

python - 如何在Python中以小端、填充零和无 '0x'的方式打印十六进制数?

Pythonic 方法检查是否为 : all elements evaluate to False -OR- all elements evaluate to True

python - 在 Python 中仅从字符串中提取字符

python - 带有马氏距离损失的 Keras 自定义损失函数如何

python - TensorFlow 无法识别 feed_dict 输入

python - 无法在 matlab 中导入 keras(python 生成)模型 - 不存在的字段 "class_name"

python - 当数据集不适合内存时,Keras 优化器状态

python - 用 beautifulsoup 抓取 html id

python - 使用 tfx 运行多个训练器时 Kubeflow Pipeline RuntimeError

c++ - Caffe 中的最小-最大归一化层