Keras后端函数:InvalidArgumentE

2024-04-25 04:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法使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).

我的神经网络有三个输入。当我试图计算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)`

Tags: inhomeinputoutputmodellayersfunctionembed
1条回答
网友
1楼 · 发布于 2024-04-25 04:23:43

您定义的函数仅使用一个输入层(即model.layers[0].input)作为其输入。相反,它必须使用所有的输入,这样模型才能运行。模型有inputsoutputs属性,您可以使用这些属性以较少的详细程度包含所有输入和输出:

f = K.function(model.inputs, model.outputs)

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

outs = f([[[3]], [[23]], [[0.0]]])

相关问题 更多 >