why can I do this without error:
model2 = keras.Sequential([
layers.Flatten(input_shape=[28,28]),
layers.Dense(124, activation='relu'),
layers.Dense(124, activation='relu'),
layers.Dense(10, activation='softmax'),
])
but not with the functional api?:
input_ = layers.Input(shape=(28, 28))
hidden_a = layers.Dense(300, activation='relu')(input_)
hidden_b = layers.Dense(100, activation='relu')(hidden_a)
concat = layers.concatenate([input_,hidden_b])
output = layers.Dense(10, activation="softmax")(concat)
model3 = keras.Model(inputs = [input_], outputs= [output])
At the moment of fitting the model, this is the error given:
InvalidArgumentError: assertion failed: [Condition x == y did not hold element-wise:] [x (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/Shape_1:0) = ] [32 1] [y (sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/strided_slice:0) = ] [32 28]
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/assert_equal_1/Assert/Assert (defined at \AppData\Local\Temp\ipykernel_3956\4049426950.py:1) ]] [Op:__inference_train_function_131704]
Thanks
CodePudding user response:
I came up with the solution: As you cannot use layers.Flatten() as your first layer, you use it as your second one(duh) answer:
input_ = layers.Input(shape=(28,28))
flatten = keras.layers.Flatten()(input_)
hidden_a = layers.Dense(124, activation='relu')(flatten)
hidden_b = layers.Dense(124, activation='relu')(hidden_a)
concat = layers.concatenate([flatten,hidden_b])
output = layers.Dense(10, activation="softmax")(concat)
model3 = keras.Model(inputs = input_, outputs= output)