tf.reducemean()到底是什么意思?


https://blog.csdn.net/he_min/article/details/78694383

在tensorflow中經常見到reducemean這個api,到底有什么用,到底是對誰求均值?

api中是這樣寫的:

tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
  • 1
Computes the mean of elements across dimensions of a tensor. Reduces input_tensor along the dimensions given in axis. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in axis. If keep_dims is true, the reduced dimensions are retained with length 1.If axis has no entries, all dimensions are reduced, and a tensor with a single element is returned.
  • 1
  • 2

直觀的翻譯就是

根據給出的axis在input_tensor上求平均值。除非keep_dims為真,axis中的每個的張量秩會減少1。如果keep_dims為真,求平均值的維度的長度都會保持為1.如果不設置axis,所有維度上的元素都會被求平均值,並且只會返回一個只有一個元素的張量。
  • 1

為了更加清楚的理解其含義,給出一個簡單的例子:

import tensorflow as tf

import numpy as np

'''
x = [[1.,2.],[3.,4.]]

print(tf.reduce_mean(x))
print(tf.reduce_mean(x,0))
print(tf.reduce_mean(x,1))
'''
x = np.array([[1.,2.,3.],[4.,5.,6.]])

with tf.Session() as sess:
#返回所有元素的平均值
mean_none = sess.run(tf.reduce_mean(x))
#返回各列的平均值
mean_0 = sess.run(tf.reduce_mean(x, 0))
#返回各行的平均值向量
mean_1 = sess.run(tf.reduce_mean(x, 1))

print(x)
print(mean_none)
print(mean_0)
print(mean_1)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM