Keras后端函数:InvalidArgumentError

我无法正常使用keras.backend.function。我试图按照这篇文章的指示进行操作:

如何使用Keras计算预测不确定性?

在这篇文章中,他们创建了一个函数f

f = K.function([model.layers[0].input],[model.layers[-1].output]) #(实际上我简化了这个函数)

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

InvalidArgumentError: 您必须为占位符张量'input_3'提供一个值,其数据类型为float,形状为[?,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) #这将在测试阶段保持dropout开启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)`

回答:

您定义的函数仅使用了一个输入层(即model.layers[0].input)作为其输入。相反,它必须使用所有输入,以便模型能够运行。模型有inputsoutputs属性,您可以使用这些属性以更少的冗长方式包含所有输入和输出:

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

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

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注