ValueError:模型的输出张量必须是Keras Layer`

时间:2018-09-07 23:48:09

标签: python keras

我试图创建一个具有权重并具有多个分支的模型,因此每个分支层都附加在列表上,但是当我将列表输入模型输出时却给了我错误:

ValueError: Output tensors to a Model must be the output of a Keras `Layer` (thus holding past layer metadata). Found: [<tf.Tensor 'dense_260/Relu:0' shape=(?, 3) dtype=float32>, <tf.Tensor 'dense_263/Relu:0' shape=(?, 3) dtype=float32>, <tf.Tensor 'dense_266/Relu:0' shape=(?, 3) dtype=float32>, <tf.Tensor 'dense_269/Relu:0' shape=(?, 3) dtype=float32>, <tf.Tensor 'dense_272/Relu:0' shape=(?, 1) dtype=float32>]

这是代码:

branch_config = [
                   ["Steer", "Gas", "Brake"], ["Steer", "Gas", "Brake"], 
                   ["Steer", "Gas", "Brake"], ["Steer", "Gas", "Brake"],
                   ["Speed"] 
                ]

def conv_block(inputs, filters, kernel_size, strides):
    x = Conv2D(filters, (kernel_size, kernel_size), strides = strides, activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(1,1), strides=(1,1))(x)
    x = BatchNormalization()(x)
    x = Dropout(0.4)(x)

    return x

def fc_block(inputs, units):
    fc = Dense(units, activation = 'relu')(inputs)
    fc = Dropout(0.2)(fc)
    return fc

branches = []

inputs = Input(input_image)

'''inputs, filters, kernel_size, strides'''

""" Conv 1 """
x = conv_block(inputs, 32, 5, 2)
x = conv_block(x, 32, 3, 1)

""" Conv 2 """
x = conv_block(x, 64, 3, 2)
x = conv_block(x, 64, 3, 1)

""" Conv 3 """
x = conv_block(x, 128, 3, 2)
x = conv_block(x, 128, 3, 1)

""" Conv 4 """
x = conv_block(x, 256, 3, 1)
x = conv_block(x, 256, 3, 1)

""" Reshape """
x = Flatten()(x)

""" FC1 """
x = fc_block(x, 512)

""" FC2 """
x = fc_block(x, 512)

"""Process Control"""

""" Speed (measurements) """
speed = input_data[1] # input_speed
speed = fc_block(speed, 128)
speed = fc_block(speed, 128)

""" Joint sensory """
j = concatenate([x, speed])
j = fc_block(j, 512)


for i in range(len(branch_config)):

    # Use only image input to predict the speed

    if branch_config[i][0] == "Speed":
        branch_output = fc_block(x, 256)
        branch_output = fc_block(branch_output, 256)

    else:
        branch_output = fc_block(j, 256)
        branch_output = fc_block(branch_output, 256)

    fully_connected = Dense(len(branch_config[i]), activation = 'relu')(branch_output)
    branches.append(fully_connected)


model = Model(inputs=inputs,outputs=[branches])

我的目标是能够训练列表中选定分支的网络,以在branch_config上添加尽可能多的分支

0 个答案:

没有答案