Python np.mean 和 tf.reduce_mean 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34236252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:40:41  来源:igfitidea点击:

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

pythonnumpymachine-learningmeantensorflow

提问by O.rka

In the MNIST beginner tutorial, there is the statement

MNIST初学者教程中,有这样的声明

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

tf.castbasically changes the type of tensor the object is, but what is the difference between tf.reduce_meanand np.mean?

tf.cast基本上改变了对象的张量类型,但是tf.reduce_mean和之间有什么区别np.mean

Here is the doc on tf.reduce_mean:

这是文档tf.reduce_mean

reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)

input_tensor: The tensor to reduce. Should have numeric type.

reduction_indices: The dimensions to reduce. If None(the defaut), reduces all dimensions.

# 'x' is [[1., 1. ]]
#         [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]

reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)

input_tensor:要减少的张量。应该有数字类型。

reduction_indices: 要减少的尺寸。如果None(默认),则减少所有维度。

# 'x' is [[1., 1. ]]
#         [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]

For a 1D vector, it looks like np.mean == tf.reduce_mean, but I don't understand what's happening in tf.reduce_mean(x, 1) ==> [1., 2.]. tf.reduce_mean(x, 0) ==> [1.5, 1.5]kind of makes sense, since mean of [1, 2]and [1, 2]is [1.5, 1.5], but what's going on with tf.reduce_mean(x, 1)?

对于一维向量,它看起来像np.mean == tf.reduce_mean,但我不明白tf.reduce_mean(x, 1) ==> [1., 2.]. tf.reduce_mean(x, 0) ==> [1.5, 1.5]有点道理,因为[1, 2]and [1, 2]is 的意思[1.5, 1.5],但是这是怎么回事tf.reduce_mean(x, 1)

采纳答案by Shubhashis

The functionality of numpy.meanand tensorflow.reduce_meanare the same. They do the same thing. From the documentation, for numpyand tensorflow, you can see that. Lets look at an example,

的功能numpy.meantensorflow.reduce_mean是相同的。他们做同样的事情。从文档中,对于numpytensorflow,您可以看到这一点。让我们看一个例子,

c = np.array([[3.,4], [5.,6], [6.,7]])
print(np.mean(c,1))

Mean = tf.reduce_mean(c,1)
with tf.Session() as sess:
    result = sess.run(Mean)
    print(result)

Output

输出

[ 3.5  5.5  6.5]
[ 3.5  5.5  6.5]

Here you can see that when axis(numpy) or reduction_indices(tensorflow) is 1, it computes mean across (3,4) and (5,6) and (6,7), so 1defines across which axis the mean is computed. When it is 0, the mean is computed across(3,5,6) and (4,6,7), and so on. I hope you get the idea.

在这里您可以看到,当axis(numpy) 或reduction_indices(tensorflow) 为 1 时,它计算跨 (3,4) 和 (5,6) 和 (6,7)1的均值,因此定义跨哪个轴计算均值。当它为 0 时,在 (3,5,6) 和 (4,6,7) 之间计算平均值,依此类推。我希望你能明白。

Now what are the differences between them?

现在它们之间有什么区别?

You can compute the numpy operation anywhere on python. But in order to do a tensorflow operation, it must be done inside a tensorflow Session. You can read more about it here. So when you need to perform any computation for your tensorflow graph(or structure if you will), it must be done inside a tensorflow Session.

您可以在 python 上的任何位置计算 numpy 操作。但是为了进行 tensorflow 操作,它必须在 tensorflow 内部完成Session。您可以在此处阅读更多相关信息。因此,当您需要对 tensorflow 图(或结构,如果您愿意的话)执行任何计算时,它必须在 tensorflow 内完成Session

Lets look at another example.

让我们再看一个例子。

npMean = np.mean(c)
print(npMean+1)

tfMean = tf.reduce_mean(c)
Add = tfMean + 1
with tf.Session() as sess:
    result = sess.run(Add)
    print(result)

We could increase mean by 1in numpyas you would naturally, but in order to do it in tensorflow, you need to perform that in Session, without using Sessionyou can't do that. In other words, when you are computing tfMean = tf.reduce_mean(c), tensorflow doesn't compute it then. It only computes that in a Session. But numpy computes that instantly, when you write np.mean().

我们可以自然地通过1in增加 mean numpy,但是为了在 tensorflow 中做到这一点,您需要在 in 中执行该操作Session,而不使用Session则无法做到。换句话说,当你在计算时tfMean = tf.reduce_mean(c),tensorflow 不会计算它。它只在 a 中计算Session。但是 numpy 会立即计算,当您编写np.mean().

I hope it makes sense.

我希望这是有道理的。

回答by shadowtalker

1usually refers to rows, and 2usually refers to columns. Reducing "over" index 1means to reduce rowwise.

1通常指行,2通常指列。减少“过度”索引1意味着按行减少。

[1., 2.]is just [ <row 1 mean> , <row 2 mean> ].

[1., 2.]只是[ <row 1 mean> , <row 2 mean> ].

This index numbering convention is typical in stats software, especially R.

这种索引编号约定在统计软件中很典型,尤其是 R。

回答by Salvador Dali

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

新文档指出tf.reduce_mean()产生与 np.mean 相同的结果:

Equivalent to np.mean

相当于 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:

它还具有与np.mean完全相同的参数。但这里有一个重要的区别:它们仅在浮点值上产生相同的结果:

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_prodproduce the same values as there numpy analogs. Clearly because they are operations, they can be executed only from inside of the session.

在附加到这一点,许多其他tf.reduce_的功能,例如reduce_allreduce_anyreduce_minreduce_maxreduce_prod产生相同的值作为有numpy的类似物。显然,因为它们是操作,所以它们只能从会话内部执行。

回答by Nikhil George

The key here is the word reduce, a concept from functional programming, which makes it possible for reduce_mean in TensorFlow to keep a running average of the results of computations from a batch of inputs.

这里的关键是reduce这个词,它是函数式编程的一个概念,它使得TensorFlow中的reduce_mean可以保持一批输入计算结果的运行平均值。

If you are not familiar with functional programming, this can seem mysterious. So first let us see what reduce does. If you were given a list like [1,2,5,4] and were told to compute the mean, that is easy - just pass the whole array to np.mean and you get the mean. However what if you had to compute the mean of a stream of numbers? In that case, you would have to first assemble the array by reading from the stream and then call np.mean on the resulting array - you would have to write some more code.

如果您不熟悉函数式编程,这可能看起来很神秘。所以首先让我们看看reduce是做什么的。如果你得到一个像 [1,2,5,4] 这样的列表,并被告知计算平均值,那很容易——只需将整个数组传递给 np.mean 就可以得到平均值。但是,如果您必须计算数字流的平均值怎么办?在这种情况下,您必须首先通过从流中读取来组装数组,然后对结果数组调用 np.mean - 您将不得不编写更多代码。

An alternative is to use the reduce paradigm. As an example, look at how we can use reduce in python to calculate the sum of numbers: reduce(lambda x,y: x+y, [1,2,5,4]).

另一种方法是使用reduce 范式。作为一个例子,看看我们如何可以使用减少蟒蛇来计算数字的总和: reduce(lambda x,y: x+y, [1,2,5,4])

It works like this:

它是这样工作的:

  1. Step 1: Read 2 digits from the list - 1,2. Evaluate lambda 1,2. reduce stores the result 3. Note - this is the only step where 2 digits are read off the list
  2. Step 2: Read the next digit from the list - 5. Evaluate lambda 5, 3 (3 being the result from step 1, that reduce stored). reduce stores the result 8.
  3. Step 3: Read the next digit from the list - 4. Evaluate lambda 8,4 (8 being the result of step 2, that reduce stored). reduce stores the result 12
  4. Step 4: Read the next digit from the list - there are none, so return the stored result of 12.
  1. 第 1 步:从列表中读取 2 位数字 - 1,2。评估 lambda 1,2。reduce 存储结果 3. 注意 - 这是从列表中读取 2 位数字的唯一步骤
  2. 第 2 步:从列表中读取下一个数字 - 5. 评估 lambda 5、3(3 是第 1 步的结果,reduce 存储)。reduce 存储结果 8.
  3. 第 3 步:从列表中读取下一个数字 - 4. 评估 lambda 8,4(8 是第 2 步的结果,reduce 存储)。reduce 存储结果 12
  4. 第 4 步:从列表中读取下一个数字 - 没有,因此返回 12 的存储结果。

Read more here Functional Programming in Python

在此处阅读更多Python 函数式编程

To see how this applies to TensorFlow, look at the following block of code, which defines a simple graph, that takes in a float and computes the mean. The input to the graph however is not a single float but an array of floats. The reduce_mean computes the mean value over all those floats.

要了解这如何应用于 TensorFlow,请查看以下代码块,该代码块定义了一个简单的图形,它接收一个浮点数并计算平均值。然而,图形的输入不是单个浮点数,而是浮点数数组。reduce_mean 计算所有这些浮点数的平均值。

import tensorflow as tf


inp = tf.placeholder(tf.float32)
mean = tf.reduce_mean(inp)

x = [1,2,3,4,5]

with tf.Session() as sess:
    print(mean.eval(feed_dict={inp : x}))

This pattern comes in handy when computing values over batches of images. Look at The Deep MNIST Examplewhere you see code like:

当计算批量图像的值时,这种模式会派上用场。查看Deep MNIST 示例,您会看到如下代码:

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))