代码之家  ›  专栏  ›  技术社区  ›  piccolo

TensorFlow会话运行不使用均方误差

  •  0
  • piccolo  · 技术社区  · 7 年前

    我确信我忽略了一些显而易见的东西,但是当我试图用tensorflow得到均方误差时,我得到了一条错误消息。

    import tensorflow as tf
    
    a = tf.constant([3, -0.5, 2, 7])
    b = tf.constant([2.5, 0.0, 2, 8])
    c = tf.metrics.mean_squared_error(a,b)
    
    sess = tf.Session()
    print(sess.run(c))
    

    出现错误:

    FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_squared_error/count
         [[Node: mean_squared_error/count/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](mean_squared_error/count)]]
    

    但单独打印c不会产生错误:

    print c
    
    (<tf.Tensor 'mean_squared_error/value:0' shape=() dtype=float32>, <tf.Tensor 'mean_squared_error/update_op:0' shape=() dtype=float32>)
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Patwie    7 年前

    根据 the implementation 以下方法有效

    import tensorflow as tf 
    a = tf.constant([3, -0.5, 2, 7])
    b = tf.constant([2.5, 0.0, 2, 8])
    c = tf.metrics.mean_squared_error(a,b)
    sess = tf.InteractiveSession()
    sess.run(tf.local_variables_initializer())
    sess.run(tf.global_variables_initializer())
    print(sess.run(c))
    

    请理解这是一个流媒体操作。不要把它和函数混在一起 tf.losses.mean_squared_error .

        2
  •  0
  •   Ali Aqrabawi    7 年前

    在访问变量之前需要初始化变量, 初始化:

    import tensorflow as tf
    
    a = tf.constant([3, -0.5, 2, 7])
    b = tf.constant([2.5, 0.0, 2, 8])
    c = tf.metrics.mean_squared_error(a,b)
    init = tf.global_variables_initializer() <--
    sess = tf.Session()
    sess.run(init) <---
    print(sess.run(c))
    
    推荐文章