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,輸出的結(jié)果保持輸入tensor的形狀,設置為False,輸出結(jié)果會降低維度;
- 第四個參數(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 ,結(jié)果:
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運算);
到此這篇關(guān)于tensorflow中tf.reduce_mean函數(shù)的使用的文章就介紹到這了,更多相關(guān)tensorflow tf.reduce_mean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中的json數(shù)據(jù)和pyecharts模塊入門示例教程
JSON是一種輕量級的數(shù)據(jù)交互格式??梢园凑?JSON指定的格式去組織和封裝數(shù)據(jù),這篇文章主要介紹了python中的json數(shù)據(jù)和pyecharts模塊入門,需要的朋友可以參考下2022-12-12

