代码之家  ›  专栏  ›  技术社区  ›  Parth Kabariya

我们可以在feed dict中将tf.variable提供给tf.placeholder吗?

  •  -1
  • Parth Kabariya  · 技术社区  · 7 年前

    我想用tensorflow做一个简单的任务。但我犯了一个错误

    import numpy as np
    import pandas as pd
    
    fv = tf.Variable(10.0,name="first_var")
    
    sv = tf.Variable(20.0,np.random.randn(),name="second_var")
    
    fvp = tf.placeholder("float32",name="first_fvp",shape=[])
    
    svp = tf.placeholder("float32",name="second_svp",shape=[])
    
    result = tf.Variable(0.0,name="output")
    
    result =  np.multiply(fvp,svp)
    
    sess = tf.Session()
    
    sess.run(tf.global_variables_initializer())
    
    print(sess.run(result,feed_dict={fvp:fv,svp:sv}))
    

    错误=用序列设置数组元素。

    如果我用

    print(sess.run(result,feed_dict={fvp:5.0,svp:10.0}))
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   null    7 年前

    首先,我还是不太明白你的问题是什么。看来你已经解决了那个错误。如果可能,请编辑。

    关于那个错误:

    你不能用张量来解释 feed_dict

    阅读 tensorflow/python/client/session.py feed_dict={} ,可接受的feed值包括Python标量、字符串、列表、numpy ndarrays或TensorHandles。在你的情况下 fv sv 是张量。

    所以你的第二个 print(sess.run(result,feed_dict={fvp:5.0,svp:10.0})) 会有用的。

    fv = np.array([10.0]) , sv = np.array([20.0])

    result = tf.Variable(0.0,name="output") ,如果要命名输出,可以使用 result = tf.identity(np.multiply(fvp,svp), name="output")

    推荐文章