在TensorFlow中缩放到三维数组时遇到一些问题。简而言之,我在下面的代码中恢复了这些问题。
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)
有人能提示我理解这种行为吗?
您的数组形状应该以一开头,因此请调用
xx2d = np.zeros((1, 28, 28))
xx3d = np.zeros((1, 28, 28, 3))
你可以在这里找到解释。