我正在使用Keras Code。当我这样写代码时,
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.
我该如何解决?
答案 0 :(得分:1)
您错过了Input
层。
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')