Keras 函数式 api 多输出

发布于 2025-01-21 03:38:09 字数 1168 浏览 1 评论 0原文

为什么我可以在没有错误的情况下执行此操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2025-01-28 03:38:09

我想出了解决方案:
由于您不能使用 Layers.Flatten() 作为您的第一层,因此您可以将其用作第二层(废话)
回答:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文