I’m trying to construct a simple neural network which gets a 2D Matrix (16×3) and outputs a single value, here is how I am trying to construct that network;
def GenerateModel():
model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer((16,3)))
model.add(tf.keras.layers.Dense(16*16, input_shape=(16,3)))
model.add(tf.keras.layers.Dense(4*4))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(1))
return model
Results of model.summary() is as follows;
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_11 (Dense) (None, 16, 256) 1024
dense_12 (Dense) (None, 16, 16) 4112
flatten_1 (Flatten) (None, 256) 0
dense_13 (Dense) (None, 1) 257
=================================================================
Total params: 5,393
Trainable params: 5,393
Non-trainable params: 0
So when I try to create a model using GenerateModel and pass it the following matrix I get an error.
[[15, 81, -25.169450961198],
[53, 108, -36.4360540112101],
[73, 84, -40.6695085658194],
[0, 69, -20.7084454809044],
[35, 97, -31.3954623571617],
[117, 102, -44.2065629437328],
[48, 68, -35.7085340456925],
[17, 59, -23.1078535464318],
[64, 24, -24.2111029108488],
[101, 2, -25.97821118996],
[57, 35, -23.7173409519286],
[117, 101, -44.1413580763786],
[88, 10, -25.4001185503816],
[11, 14, -22.0778253042297],
[46, 50, -24.7021623105999],
[0, 0, -1]]
The error is;
ValueError: Layer "sequential_5" expects 1 input(s), but it received 48 input tensors. Inputs received: [<tf.Tensor: shape=(), dtype=int32, numpy=15>, <tf.Tensor: shape=(), dtype=int32, numpy=81>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.169450961198>, <tf.Tensor: shape=(), dtype=int32, numpy=53>, <tf.Tensor: shape=(), dtype=int32, numpy=108>, <tf.Tensor: shape=(), dtype=float64, numpy=-36.4360540112101>, <tf.Tensor: shape=(), dtype=int32, numpy=73>, <tf.Tensor: shape=(), dtype=int32, numpy=84>, <tf.Tensor: shape=(), dtype=float64, numpy=-40.6695085658194>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=69>, <tf.Tensor: shape=(), dtype=float64, numpy=-20.7084454809044>, <tf.Tensor: shape=(), dtype=int32, numpy=35>, <tf.Tensor: shape=(), dtype=int32, numpy=97>, <tf.Tensor: shape=(), dtype=float64, numpy=-31.3954623571617>, <tf.Tensor: shape=(), dtype=int32, numpy=117>, <tf.Tensor: shape=(), dtype=int32, numpy=102>, <tf.Tensor: shape=(), dtype=float64, numpy=-44.2065629437328>, <tf.Tensor: shape=(), dtype=int32, numpy=48>, <tf.Tensor: shape=(), dtype=int32, numpy=68>, <tf.Tensor: shape=(), dtype=float64, numpy=-35.7085340456925>, <tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=59>, <tf.Tensor: shape=(), dtype=float64, numpy=-23.1078535464318>, <tf.Tensor: shape=(), dtype=int32, numpy=64>, <tf.Tensor: shape=(), dtype=int32, numpy=24>, <tf.Tensor: shape=(), dtype=float64, numpy=-24.2111029108488>, <tf.Tensor: shape=(), dtype=int32, numpy=101>, <tf.Tensor: shape=(), dtype=int32, numpy=2>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.97821118996>, <tf.Tensor: shape=(), dtype=int32, numpy=57>, <tf.Tensor: shape=(), dtype=int32, numpy=35>, <tf.Tensor: shape=(), dtype=float64, numpy=-23.7173409519286>, <tf.Tensor: shape=(), dtype=int32, numpy=117>, <tf.Tensor: shape=(), dtype=int32, numpy=101>, <tf.Tensor: shape=(), dtype=float64, numpy=-44.1413580763786>, <tf.Tensor: shape=(), dtype=int32, numpy=88>, <tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=float64, numpy=-25.4001185503816>, <tf.Tensor: shape=(), dtype=int32, numpy=11>, <tf.Tensor: shape=(), dtype=int32, numpy=14>, <tf.Tensor: shape=(), dtype=float64, numpy=-22.0778253042297>, <tf.Tensor: shape=(), dtype=int32, numpy=46>, <tf.Tensor: shape=(), dtype=int32, numpy=50>, <tf.Tensor: shape=(), dtype=float64, numpy=-24.7021623105999>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=0>, <tf.Tensor: shape=(), dtype=int32, numpy=-1>]
How can I fix this?
I also tried to generate my network as follows;
def GenerateModel():
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(16*16, input_shape=(16,3)))
model.add(tf.keras.layers.Dense(4*4))
model.add(tf.keras.layers.Dense(1))
return model
and also this;
def GenerateModel():
model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer((16,3)))
model.add(tf.keras.layers.Dense(16*16))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(4*4))
model.add(tf.keras.layers.Dense(1))
return model
I tried to add a Flatten layer after different layers, did not solve my problem.
How can this be solved?
trafon31 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.