深度学习中张量flatten处理(flatten,reshape,reduce)
生活随笔
收集整理的這篇文章主要介紹了
深度学习中张量flatten处理(flatten,reshape,reduce)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先看一下flatten的具體用法
1-對于一般數值,可以直接flatten
2-對列表變量,需要做一下處理
>>> a=array([[1,2],[3,4],[5,6]]) >>> [y for x in a for y in x] [1, 2, 3, 4, 5, 6]3-對mat對象
>>> a = [[1,3],[2,4],[3,5]] >>> a = mat(a) >>> y = a.flatten() >>> y matrix([[1, 3, 2, 4, 3, 5]]) >>> y = a.flatten().A >>> y array([[1, 3, 2, 4, 3, 5]]) >>> shape(y) (1, 6) >>> shape(y[0]) (6,) >>> y = a.flatten().A[0] >>> y array([1, 3, 2, 4, 3, 5])4-壓縮(reduce)
slopes=tf.sqrt(tf.reduce_sum(tf.square(gradients),reduction_indices=[1])其中reduction_indices=1表示按行壓縮,=0表示按列壓縮
5-reshape
tensor是我們輸入的張量,shape是我們希望輸入變成什么形狀,其中的shape為一個列表形式,特殊的是列表可以實現逆序的遍歷,即list(-1).-1所代表的含義是我們不用親自去指定這一維的大小,函數會自動進行計算,但是列表中只能存在一個-1。如一個6元素的數組,想要reshape成2*3時,下面三種寫法得到的結果都是一樣的
tf.reshape(a,[2,3]) tf.reshape(a,[2,-1]) tf.reshape(a,[-1,3])總結
以上是生活随笔為你收集整理的深度学习中张量flatten处理(flatten,reshape,reduce)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 帮助理解GAN的一些补充内容
- 下一篇: Keras入门(一)