【问题标题】:Tensorflow 1.x Error: The value of a feed cannot be a tf.Tensor object. Restoring placeholdersTensorflow 1.x 错误:提要的值不能是 tf.Tensor 对象。恢复占位符
【发布时间】:2020-10-15 16:37:44
【问题描述】:

这是我得到的错误: Feed 的值不能是 tf.Tensor 对象。可接受的提要值包括 Python 标量、字符串、列表、numpy ndarray 或 TensorHandles。作为参考,张量对象是 Tensor("x_19:0", shape=(?, 1), dtype=float32) ,它使用键 Tensor("x:0", shape=(?, 1) , dtype=float32)。

这是我的代码:

self.x_tf = tf.placeholder(tf.float32, shape=[None, self.x.shape[1]], name='x')
self.t_tf = tf.placeholder(tf.float32, shape=[None, self.t.shape[1]], name='t')

self.theta = self.net_theta(self.x_tf, self.t_tf)

def net_theta(self, x, t):  
        new_saver = tf.train.import_meta_graph('__file__theta.ckpt.meta')
        new_saver.restore(model.sess,tf.train.latest_checkpoint('./')) 
        graph = tf.get_default_graph()
        x_name = graph.get_tensor_by_name("x:0")
        t_name = graph.get_tensor_by_name('t:0')
        Pred = graph.get_tensor_by_name('Pred:0')
        feed_dict = {x_name:x,t_name: t}
        theta = self.sess.run(Pred,feed_dict)
        return theta

【问题讨论】:

    标签: python tensorflow placeholder


    【解决方案1】:

    将会话作为 sess 启动后,您可以使用 your_tensor.eval(session=sess)sess.run(your_tensor) 将张量输入为 numpy.array 的格式,然后将其输入占位符。

    【讨论】:

    • 所以 x_name 和 t_name 代表我的张量,该语法看起来如何?我在执行该建议时遇到了一些麻烦。谢谢!!
    • 问题是您将张量传递到您的提要字典中,这是不允许的。你可以做的是x_name_np = sess.run(x_name)t_name_np = sess.run(t_name)。然后像feed_dict = {x:x_name_np, t:t_name_np}一样初始化feed_dict
    • 不幸的是,这不起作用。这是我得到的错误“InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: You must feed a value for placeholder tensor 'x' with dtype float and shape [?,1] [[{{node x }}]] [[x/_101]] (1) 无效参数:您必须为占位符张量 'x' 提供一个值,其 dtype 为 float 和 shape [?,1] [[{{node x}}]] 0 成功操作。忽略 0 个派生错误。"
    • 你确定你的张量 x_name 和 t_name 不为空吗?当您将空数组传递给您的提要字典时,会发生此错误。为了进行完整性检查,请在将它们传递到您的提要字典之前尝试打印它们
    • 我不确定,好像不是空的?这就是我打印出 x_name、t_name 和 pred.Tensor("x:0", shape=(?, 1), dtype=float32) Tensor("t:0", shape=(?, 1) , dtype=float32) Tensor("Pred:0", shape=(?, 1), dtype=float32)
    猜你喜欢
    • 2016-04-20
    • 2018-07-02
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2016-10-03
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多