TensorFlow 提出了从 2 维到 3 维的缩放问题

TensorFlow rises issues scaling fro 2 to 3 dimensions

我在 TensorFlow 中缩放到 3d 数组时遇到了一些问题。简而言之,我在以下代码中恢复了问题。

import numpy as np
import tensorflow as tf


mymodel2d = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
mymodel2d.compile(optimizer='adam', loss='mse')


mymodel3d = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(28, 28, 3)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
mymodel3d.compile(optimizer='adam', loss='mse')

xx2d = np.zeros((28,28))
xx3d = np.zeros((28,28,3))

print(xx2d.shape)
print(xx3d.shape)

out1 = mymodel2d.predict(xx2d)
out2 = mymodel3d.predict(xx3d)

model2d 工作正常,但在 model3d 上尝试执行 out2 = mymodel3d.predict(xx3d) 行时出现以下问题。

出现的错误是: ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 28, 28, 3), found shape=(None, 28, 3)

有人可以给我一些提示来理解这种行为吗?

您的数组形状应以 1 开头,因此请调用

xx2d = np.zeros((1, 28, 28))
xx3d = np.zeros((1, 28, 28, 3))

有关解释,您可以查看