網站首頁 編程語言 正文
解讀tf.reduce_sum()函數和tf.reduce_mean()函數
在學習搭建神經網絡的時候,照著敲別人的代碼,有一句代碼一直搞不清楚,就是下面這句了
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
剛開始照著up主寫的代碼是這樣滴:
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction)))
然后就出現了這樣的結果:
709758.1
nan
nan
nan
nan
nan
nan
nan
nan
nan
nan
怎么肥事,對于萌新小白首先想到的就是找度娘,結果找到的方法都不行,然后開始查函數,終于發現了原因,問題就出在reduce_sum()函數上,哈哈哈,然后小白又叕叕叕開始找博客學習reduce_sum()順帶學下reduce_mean(),結果看了好幾篇,還是腦袋一片漿糊,為啥用reduction_indices=[1],不用reduction_indices=[0]或者干脆不用,費了九牛二虎之力終于讓我給弄懂了,趕緊記錄下來!!
-------------------分割線-------------------
1.tf.reduce_mean 函數
用于計算張量tensor沿著指定的數軸(tensor的某一維度)上的的平均值,主要用作降維或者計算tensor(圖像)的平均值。
reduce_mean(input_tensor, ? ? ? ? ? ? ? ? axis=None, ? ? ? ? ? ? ? ? keep_dims=False, ? ? ? ? ? ? ? ? name=None, ? ? ? ? ? ? ? ? reduction_indices=None)
- 第一個參數input_tensor: 輸入的待降維的tensor;
- 第二個參數axis: 指定的軸,如果不指定,則計算所有元素的均值;
- 第三個參數keep_dims:是否降維度,設置為True,輸出的結果保持輸入tensor的形狀,設置為False,輸出結果會降低維度;
- 第四個參數name: 操作的名稱;
- 第五個參數 reduction_indices:在以前版本中用來指定軸,已棄用;
2.tf.reduce_sum函數
計算一個張量的各個維度上元素的總和,一般只需設置兩個參數
reduce_sum (? ? ? input_tensor ,? ? ? axis = None ,? ? ? keep_dims = False ,? ? ? name = None ,? ? ? reduction_indices = None ?)
- 第一個參數input_tensor: 輸入的tensor
- 第二個參數 reduction_indices:指定沿哪個維度計算元素的總和
最難的就是維度問題,反正本小白看了好幾個博客都沒弄太懂,最后還是按自己的理解,直接上例子
reduce_sum()
tf.reduce_sum matrix1 = [[1.,2.,3.], ? ? ? ? ? ?#二維,元素為列表 ? ? ? ? ? [4.,5.,6.]] matrix2 = [[[1.,2.],[3.,4.]], ? ? ?#三維,元素為矩陣 ? ? ? ? ? ?[[5.,6.],[7.,8.]]] res_2 = tf.reduce_sum(matrix1) res_3 = tf.reduce_sum(matrix2) res1_2 = tf.reduce_sum(matrix1,reduction_indices=[0]) res1_3 = tf.reduce_sum(matrix2,reduction_indices=[0]) res2_2 = tf.reduce_sum(matrix1,reduction_indices=[1]) res2_3 = tf.reduce_sum(matrix2,reduction_indices=[1]) sess = tf.Session() print("reduction_indices=None:res_2={},res_3={}".format(sess.run(res_2),sess.run(res_3))) print("reduction_indices=[0]:res1_2={},res1_3={}".format(sess.run(res1_2),sess.run(res1_3))) print("reduction_indices=[1]:res2_2={},res2_3={}".format(sess.run(res2_2),sess.run(res2_3)))
結果如下:
axis=None:res_2=21.0,res_3=36.0
axis=[0]:res1_2=[5. 7. 9.],res1_3=[[ 6. ?8.]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [10. 12.]]
axis=[1]:res2_2=[ 6. 15.],res2_3=[[ 4. ?6.]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[12. 14.]]
tf.reduce_mean
只需要把上面代碼的reduce_sum部分換成renduce_mean即可
res_2 = tf.reduce_mean(matrix1) res_3 = tf.reduce_mean(matrix2) res1_2 = tf.reduce_mean(matrix1,axis=[0]) res1_3 = tf.reduce_mean(matrix2,axis=[0]) res2_2 = tf.reduce_mean(matrix1,axis=[1]) res2_3 = tf.reduce_mean(matrix2,axis=[1])
結果如下:
axis=None:res_2=3.5,res_3=4.5
axis=[0]:res1_2=[2.5 3.5 4.5],res1_3=[[3. 4.]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[5. 6.]]
axis=[1]:res2_2=[2. 5.],res2_3=[[2. 3.]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[6. 7.]]
可以看到,reduction_indices和axis其實都是代表維度,當為None時,reduce_sum和reduce_mean對所有元素進行操作,當為[0]時,其實就是按行操作,當為[1]時,就是按列操作,對于三維情況,把最里面的括號當成是一個數,這樣就可以用二維的情況代替,最后得到的結果都是在原來的基礎上降一維,下面按專業的方法講解:
對于一個多維的array,最外層的括號里的元素的axis為0,然后每減一層括號,axis就加1,直到最后的元素為單個數字
如上例中的matrix1 = [[1., 2., 3.], [4., 5., 6.]]:
-
axis=0
時,所包含的元素有:[1., 2., 3.]、[4., 5., 6.] -
axis=1
時,所包含的元素有:1.、2.、3.、4.、5.、6.
所以當reduction_indices/axis=[0],應對axis=0上的元素進行操作,故reduce_sum()得到的結果為[5. 7. 9.],即把兩個數組對應元素相加;當reduction_indices/axis=[1],應對axis=1上的元素進行操作,故reduce_sum()得到的結果為[ 6. 15.],即把每個數組里的元素相加。reduce_mean()同理。
不難看出對于三維情況也是同樣的思路,如上例中的matrix2 = [[[1,2],[3,4]], [[5,6],[7,8]]]:
-
axis=0
時,所包含的元素有:[[1., 2.],[3., 4.]]、[[5., 6.],[7., 8.]] -
axis=1
時,所包含的元素有:[1., 2.]、[3., 4.]、[5., 6.]、[7., 8.] -
axis=2
時,所包含的的元素有:1.、2.、3.、4.、5.、6.、7.、8.
當reduction_indices/axis=[0],reduce_sum()得到的結果應為[[ 6. 8.], [10. 12.]],即把兩個矩陣對應位置元素相加;當reduction_indices/axis=[1],reduce_sum()得到的結果應為[[ 4. 6.], [12. 14.]],即把數組對應元素相加。reduce_mean()同理。
一句話就是對哪一維操作,計算完后外面的括號就去掉,相當于降維。
那么問題來了,當reduction_indices/axis=[2]時呢???
- 對于二維情況,當然是報錯了,因為axis最大為1
ValueError: Invalid reduction dimension 2 for input with 2 dimensions. for 'Sum_4' (op: 'Sum') with input shapes: [2,3], [1] and with computed input tensors: input[1] = <2>.
- 對于三維情況,reduce_sum()得到的結果為:[[ 3. 7.], [11. 15.]],即對最內層括號里的元素求和。
-------------------分割線-------------------
回到最開始自己的問題,為什么只有設置參數reduction_indices=[1],loss才不為Nan
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
本程序構建的是一個3層神經網絡,輸入層只有1個神經元,輸入數據為100個樣本點,即shape為(100,1)的列向量,隱藏層有10個神經元,輸出層同樣只有1個神經元,故最后輸出數據的shape也為(100,1)的列向量,那么reduce_sum的參數即為一個二維數組。
- 若reduction_indices=[0],最后得到的是只有一個元素的數組,即[n]
- 若reduction_indices=[1],最后得到的是有100個元素的數組,即[n1,n2…n100]
- 若reduction_indices=None,最后得到的則是一個數
那么再使用reduce_mean()求平均時,想要得到的結果是sum/100,這時就只有reduce_sum()傳入參數reduction_indices=[1],才能實現想要的效果了。
完美解決!!!
原文鏈接:https://blog.csdn.net/weixin_42149550/article/details/98759006
相關推薦
- 2023-02-14 React?useCallback使用教程_React
- 2022-04-06 go語言限制協程并發數的方案詳情_Golang
- 2022-07-29 C++超詳細講解數組操作符的重載_C 語言
- 2022-09-26 Python文件相關操作和方法匯總大全_python
- 2022-12-03 React為什么需要Scheduler調度器原理詳解_React
- 2022-09-16 Python利用LyScript插件實現批量打開關閉進程_python
- 2022-04-16 統計C語言二叉樹中葉子結點個數_C 語言
- 2022-09-16 go?goroutine實現素數統計的示例_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支