【问题标题】:How to properly feed specific tensor to keras model如何正确地将特定张量提供给 keras 模型
【发布时间】:2017-11-23 03:41:43
【问题描述】:

为了允许将 Keras 模型用作标准 tensorflow 操作的一部分,我使用特定占位符为输入创建了一个模型。

但是,在尝试执行 model.predict 时,我得到一个错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [100,84,84,4]
 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[100,84,84,4], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

我的代码如下:

from keras.layers import Convolution2D, Dense, Input
from keras.models import Model
from keras.optimizers import Nadam
from keras.losses import mean_absolute_error
from keras.activations import relu
import tensorflow as tf
import numpy as np
import gym

state_size = [100, 84, 84, 4]

input_tensor = tf.placeholder(dtype=tf.float32, shape=state_size)

inputL = Input(tensor=input_tensor)
h1 = Convolution2D(filters=32, kernel_size=(5,5), strides=(4,4), activation=relu) (inputL)
h2 = Convolution2D(filters=64, kernel_size=(3,3), strides=(2,2), activation=relu) (h1)
h3 = Convolution2D(filters=64, kernel_size=(3,3), activation=relu) (h2)
h4 = Dense(512, activation=relu) (h3)
out = Dense(18) (h4)

model = Model(inputL, out)

opt = Nadam()


disc_rate=0.99

sess = tf.Session()
dummy_input = np.ones(shape=state_size)

model.compile(opt, mean_absolute_error)

writer = tf.summary.FileWriter('./my_graph', sess.graph)
writer.close()

print(out)

print(model.predict({input_tensor: dummy_input}))

我也尝试直接输入输入(没有字典,只有值)——同样的例外。但是,我可以让模型像这样工作:

print(sess.run( model.output, {input_tensor: dummy_input }))

有没有办法让我仍然使用普通的 Keras .p​​redict 方法?

【问题讨论】:

  • 您可以将 keras.Input 实例视为占位符(不一定是 TensorFlow 占位符)。不要为其提供预定义的张量,而是使用shapedtype 对其进行初始化,然后您可以使用model.predict(dummy_input)

标签: machine-learning tensorflow deep-learning keras


【解决方案1】:

以下工作(我们需要初始化全局变量):

sess.run(tf.global_variables_initializer()) # initialize 
print(sess.run([model.output], feed_dict={input_tensor: dummy_input}))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2019-11-06
    相关资源
    最近更新 更多