【问题标题】:Getting "You must feed a value for placeholder tensor" error when using Keras model使用 Keras 模型时出现“您必须为占位符张量提供值”错误
【发布时间】:2019-04-15 09:54:54
【问题描述】:

我正在尝试在 Keras 中实现模型,但出现以下错误:

您必须为占位符张量提供一个值

这是我的模型:

def create_base_network(input_shape, out_dims):
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                     activation='relu',
                     input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(out_dims, activation='linear'))

    return model

input_shape=(28,28,3)
anchor_in = Input(shape=input_shape)
pos_in = Input(shape=input_shape)
neg_in = Input(shape=input_shape)

base_network = create_base_network(input_shape, 128)
anchor_out = base_network(anchor_in)
pos_out = base_network(pos_in)
neg_out = base_network(neg_in)

merged = concatenate([anchor_out, pos_out, neg_out], axis=-1)

model = Model(inputs=[anchor_in, pos_in, neg_in], outputs=merged)    

然后我尝试使用以下方法从顺序模型中获取输出:

seq_fun = K.function([model.layers[0].input, model.layers[1].input, model.layers[2].input], [model.layers[3].get_output_at(0)])
seq_output = seq_fun([a, p, n])[0]

它的输入来自一个生成器,它是一个 numpy 数组的形式,每个数组都具有所需的形状。那么错误信息是:

InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,28,28,3]
 [[{{node conv2d_1_input}} = Placeholder[dtype=DT_FLOAT, shape=[?,28,28,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
 [[{{node dense_2/BiasAdd/_175}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_102_dense_2/BiasAdd", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

我真的不知道发生了什么。

【问题讨论】:

    标签: python tensorflow machine-learning keras tensor


    【解决方案1】:

    您创建的顺序模型有 4 个输出节点。索引为零的那个,即get_output_at(0),是直接馈送它的结果,而其他三个是使用您定义的输入层之一馈送它时的输出。显然第一个没有连接到您定义的输入层,因此您会收到错误:

    您必须为占位符张量提供一个值...

    因此您需要指定其他三个输出节点(索引 1、2 或 3)作为自定义函数的输出:

    seq_fun = K.function([model.layers[0].input, model.layers[1].input, model.layers[2].input], 
                          [model.layers[3].get_output_at(i)])  # i must be 1, 2 or 3
    

    附带说明,您可以使用模型的inputs 属性更简洁地定义自定义函数:

    seq_fun = K.function(model.inputs, [model.layers[3].get_output_at(i)])
    

    【讨论】:

      【解决方案2】:

      重新安装 tensorflow==1.11.0 和 keras==2.1.2 对我有用。

      【讨论】:

        猜你喜欢
        • 2018-10-23
        • 2019-04-25
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 2018-11-05
        • 1970-01-01
        相关资源
        最近更新 更多