Keras 函数式 api 多输出
为什么我可以在没有错误的情况下执行此操作:
model2 = keras.Sequential([
layers.Flatten(input_shape=[28,28]),
layers.Dense(124, activation='relu'),
layers.Dense(124, activation='relu'),
layers.Dense(10, activation='softmax'),
])
而不是使用功能性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])
在拟合模型时,这是给出的错误:
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]
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出了解决方案:
由于您不能使用 Layers.Flatten() 作为您的第一层,因此您可以将其用作第二层(废话)
回答:
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: