Im experiencing some problems when scaling to 3d array in TensorFlow. In a nutshell I resumed the issues in the following code.
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)
The model2d
works properly, but on the model3d
rise the following issues when trying to execute out2 = mymodel3d.predict(xx3d)
line.
The rised error is:
ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 28, 28, 3), found shape=(None, 28, 3)
Can someone give me a hint in understanding such a behaviour?
CodePudding user response:
Your arrays shape should begin with one, so instead call
xx2d = np.zeros((1, 28, 28))
xx3d = np.zeros((1, 28, 28, 3))
For an explanation you can look here.