gpt4 book ai didi

machine-learning - 构建神经网络 - 将网络作为参数传递在 keras 中不起作用

转载 作者:行者123 更新时间:2023-11-30 09:46:17 26 4
gpt4 key购买 nike

我正在使用 Keras 代码。当我写这样的代码时,

model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(784,) ))
model.add(Dense(128, activation='relu'))
model.add(Dense(784, activation='relu'))
model.compile(optimizer='adam', loss='mean_squared_error')

它工作起来没有任何问题。但是如果通过将上一层作为参数传递给下一层来实现这一点,那么我会得到错误。

layer1 = Dense(64, activation='relu', input_shape=(784,) )
layer2 = Dense(128, activation='relu') (layer1)
layer3 = Dense(784, activation='relu') (layer2)
model = Model(layer1, layer3)
model.compile(optimizer='adam', loss='mean_squared_error')

错误如下

ValueError: Layer dense_2 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.layers.core.Dense'>. Full input: [<keras.layers.core.Dense object at 0x7f1317396310>]. All inputs to the layer should be tensors.

我该如何解决这个问题?

最佳答案

您错过了输入层。

x = Input((784,))
layer1 = Dense(64, activation='relu')(x)
layer2 = Dense(128, activation='relu') (layer1)
layer3 = Dense(784, activation='relu') (layer2)
model = Model(inputs=x, outputs=layer3)
model.compile(optimizer='adam', loss='mean_squared_error')

关于machine-learning - 构建神经网络 - 将网络作为参数传递在 keras 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com