在对流层上使用keras api时出错

时间:2019-09-16 19:58:48

标签: python python-3.x keras theano

我正在尝试构建一个看起来像这样的模型:

input
  /
convlayers/flatten
   /         \
first_output  \
     \        /
   second_output

但是在第一个转换层失败,并显示以下错误:

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

,并且错误指向具有inputshape调用的第一个转换之后的层。

我们将不胜感激。

代码如下:

conv1 = Conv2D(8, 4, padding = "same", strides = 2)(inputs)
conv2 = Conv2D(16 ,4, padding = "same", strides = 2)(conv1)
flat = Flatten()(conv2)
dense1 = Dense(32)(flat)

dense2 = Dense(32)(dense1)
first_output = Dense(64)(dense2)
merged = concatenate([flat,first_output])
second_output_dense1 = Dense(32)(merged)
second_output_dense2 = Dense(32)(second_output_dense1)
second_output = Dense(64)(second_output_dense2)

model = Model(inputs=conv1, outputs=[first_output,second_output])
model.compile(loss = "mse", optimizer = "adam" )

答案:

我的印象是,您可以在没有输入层的情况下调用模型,而只需在第一层中定义输入:conv1 = Conv2D(8,4,padding =“ same”,步幅= 2, < em> input_shape =(6,8,8,)

但这没用,因此您必须删除输入形状的东西并创建输入层,这里是固定代码

inputs = Input(shape=(6,8,8,))
conv1 = Conv2D(8, 4, padding = "same", strides = 2, input_shape = (6,8,8,))
conv2 = Conv2D(16 ,4, padding = "same", strides = 2)(conv1)
flat = Flatten()(conv2)
dense1 = Dense(32)(flat)

dense2 = Dense(32)(dense1)
first_output = Dense(64)(dense2)
merged = concatenate([flat,first_output])
second_output_dense1 = Dense(32)(merged)
second_output_dense2 = Dense(32)(second_output_dense1)
second_output = Dense(64)(second_output_dense2)

model = Model(inputs=inputs, outputs=[first_output,second_output])
model.compile(loss = "mse", optimizer = "adam" )

0 个答案:

没有答案