tensorflow中tf.reduce_mean函數(shù)的使用
更新時間:2020年04月19日 11:28:41 作者:-牧野-
這篇文章主要介紹了tensorflow中tf.reduce_mean函數(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
tf.reduce_mean 函數(shù)用于計算張量tensor沿著指定的數(shù)軸(tensor的某一維度)上的的平均值,主要用作降維或者計算tensor(圖像)的平均值。
reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
- 第一個參數(shù)input_tensor: 輸入的待降維的tensor;
- 第二個參數(shù)axis: 指定的軸,如果不指定,則計算所有元素的均值;
- 第三個參數(shù)keep_dims:是否降維度,設置為True,輸出的結果保持輸入tensor的形狀,設置為False,輸出結果會降低維度;
- 第四個參數(shù)name: 操作的名稱;
- 第五個參數(shù) reduction_indices:在以前版本中用來指定軸,已棄用;
以一個維度是2,形狀是[2,3]的tensor舉例:
import tensorflow as tf x = [[1,2,3], [1,2,3]] xx = tf.cast(x,tf.float32) mean_all = tf.reduce_mean(xx, keep_dims=False) mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False) mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False) with tf.Session() as sess: m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1]) print m_a # output: 2.0 print m_0 # output: [ 1. 2. 3.] print m_1 #output: [ 2. 2.]
如果設置保持原來的張量的維度,keep_dims=True ,結果:
print m_a # output: [[ 2.]] print m_0 # output: [[ 1. 2. 3.]] print m_1 #output: [[ 2.], [ 2.]]
類似函數(shù)還有:
- tf.reduce_sum :計算tensor指定軸方向上的所有元素的累加和;
- tf.reduce_max : 計算tensor指定軸方向上的各個元素的最大值;
- tf.reduce_all : 計算tensor指定軸方向上的各個元素的邏輯和(and運算);
- tf.reduce_any: 計算tensor指定軸方向上的各個元素的邏輯或(or運算);
到此這篇關于tensorflow中tf.reduce_mean函數(shù)的使用的文章就介紹到這了,更多相關tensorflow tf.reduce_mean內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
通過實例解析python subprocess模塊原理及用法
這篇文章主要介紹了通過實例解析python subprocess模塊原理及用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10Python網(wǎng)絡編程中urllib2模塊的用法總結
使用urllib2模塊進行基于url的HTTP請求等操作大家也許都比較熟悉,這里我們再深入來了解一下urllib2針對HTTP的異常處理相關功能,一起來看一下Python網(wǎng)絡編程中urllib2模塊的用法總結:2016-07-07