What is the difference between np.mean and tf.reduce_mean?

前端 未结 4 1898
小蘑菇
小蘑菇 2021-01-30 02:02

In the MNIST beginner tutorial, there is the statement

accuracy = tf.reduce_mean(tf.cast(correct_prediction, \"float\"))

tf.cast

4条回答
  •  忘掉有多难
    2021-01-30 02:28

    The new documentation states that tf.reduce_mean() produces the same results as np.mean:

    Equivalent to np.mean

    It also has absolutely the same parameters as np.mean. But here is an important difference: they produce the same results only on float values:

    import tensorflow as tf
    import numpy as np
    from random import randint
    
    num_dims = 10
    rand_dim = randint(0, num_dims - 1)
    c = np.random.randint(50, size=tuple([5] * num_dims)).astype(float)
    
    with tf.Session() as sess:
        r1 = sess.run(tf.reduce_mean(c, rand_dim))
        r2 = np.mean(c, rand_dim)
        is_equal = np.array_equal(r1, r2)
        print is_equal
        if not is_equal:
            print r1
            print r2
    

    If you will remove type conversion, you will see different results


    In additional to this, many other tf.reduce_ functions such as reduce_all, reduce_any, reduce_min, reduce_max, reduce_prod produce the same values as there numpy analogs. Clearly because they are operations, they can be executed only from inside of the session.

提交回复
热议问题