【问题标题】:Tensoflow ValueError: initial_value must have a shape specifiedTensorflow ValueError:initial_value 必须具有指定的形状
【发布时间】:2021-05-04 01:15:21
【问题描述】:

我通常使用 Tensorflow Keras 后端,但最近我正在做一个需要 T.F 1.x 的项目

我正在尝试一个简单的代码,但出现错误:

x2 = tf.constant(-2.0, name="x", dtype=tf.float32)
a = tf.placeholder(name='a',dtype=tf.float32)
b = tf.constant(13.0, name="b", dtype=tf.float32)

y = tf.Variable(tf.add(tf.multiply(a, x2), b))

init = tf.global_variables_initializer()

with tf.Session() as session:
    print(session.run(init,feed_dict={a:5.0})) 

ValueError: initial_value 必须具有在 y=Variable()... 行指定的形状。

有人知道解决办法吗?提前致谢

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    变量“y”取决于变量“a”,它是一个占位符。所以定义“a”的形状会让代码正常运行

    x2 = tf.constant(-2.0, name="x", dtype=tf.float32)
    a = tf.placeholder(name='A',shape=(1,),dtype=tf.float32)
    b = tf.constant(13.0, name="b", dtype=tf.float32)
    
    y = tf.Variable(tf.add(tf.multiply(a, x2), b))
    
    init = tf.global_variables_initializer()
    
    with tf.Session() as session:
        print(session.run(init,feed_dict={a:[5.0]}))
        print(session.run(y))
    

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 1970-01-01
      • 2018-06-29
      • 2018-07-28
      • 1970-01-01
      • 2021-09-19
      • 2020-10-20
      • 2020-12-02
      • 1970-01-01
      相关资源
      最近更新 更多