Keras后端函数:InvalidArgumentError

时间:2018-12-10 21:29:38

标签: python machine-learning keras neural-network

我无法让keras.backend.function正常工作。我正在尝试关注该帖子:

How to calculate prediction uncertainty using Keras?

在本文中,他们创建了一个函数f

f = K.function([model.layers[0].input],[model.layers[-1].output]) #(I actually simplified the function a little bit).

在我的神经网络中,我有3个输入。当我尝试计算f([[3], [23], [0.0]])时,出现此错误:

InvalidArgumentError: You must feed a value for placeholder tensor 'input_3' with dtype float and shape [?,1]
 [[{{node input_3}} = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]

现在,我知道在测试阶段使用[[3], [23], [0.0]]作为模型中的输入不会给我带来错误。谁能告诉我我要去哪里错了?

这很重要,这就是我的模型的样子:

home_in = Input(shape=(1,))
away_in = Input(shape=(1,))
time_in = Input(shape = (1,))
embed_home = Embedding(input_dim = in_dim, output_dim = out_dim, input_length = 1)
embed_away = Embedding(input_dim = in_dim, output_dim = out_dim, input_length = 1)
embedding_home = Flatten()(embed_home(home_in))
embedding_away = Flatten()(embed_away(away_in))
keras.backend.set_learning_phase(1) #this will keep dropout on during the testing phase
model_layers = Dense(units=2)\
    (Dropout(0.3)\
    (Dense(units=64, activation = "relu")\
    (Dropout(0.3)\
    (Dense(units=64, activation = "relu")\
    (Dropout(0.3)\
    (Dense(units=64, activation = "relu")\
    (concatenate([embedding_home, embedding_away, time_in]))))))))
model = Model(inputs=[home_in, away_in, time_in], outputs=model_layers)`

1 个答案:

答案 0 :(得分:0)

您定义的函数仅使用输入层之一(即select distinct dt ,count(*) over(partition by dt) as day_total ,count(*) over(order by dt) as cumsum ,sum(flag) over(order by dt) as cumdist from (select t.* ,case when lag(dt) over(partition by email order by dt) is not null then 0 else 1 end as flag from tbl t ) t )作为输入。相反,它必须使用所有输入,以便可以运行模型。该模型有model.layers[0].inputinputs个属性,您可以使用它们来包括所有输入和输出的详细程度较低:

outputs

更新:所有输入数组的形状必须为f = K.function(model.inputs, model.outputs) 。因此,您需要传递列表列表(例如(num_samples, 1))而不是列表(例如[[3]]):

[3]