深度学习中张量flatten处理(flatten,reshape,reduce)
生活随笔
收集整理的這篇文章主要介紹了
深度学习中张量flatten处理(flatten,reshape,reduce)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
先看一下flatten的具體用法
1-對(duì)于一般數(shù)值,可以直接flatten
2-對(duì)列表變量,需要做一下處理
>>> a=array([[1,2],[3,4],[5,6]]) >>> [y for x in a for y in x] [1, 2, 3, 4, 5, 6]3-對(duì)mat對(duì)象
>>> 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為一個(gè)列表形式,特殊的是列表可以實(shí)現(xiàn)逆序的遍歷,即list(-1).-1所代表的含義是我們不用親自去指定這一維的大小,函數(shù)會(huì)自動(dòng)進(jìn)行計(jì)算,但是列表中只能存在一個(gè)-1。如一個(gè)6元素的數(shù)組,想要reshape成2*3時(shí),下面三種寫法得到的結(jié)果都是一樣的
tf.reshape(a,[2,3]) tf.reshape(a,[2,-1]) tf.reshape(a,[-1,3])總結(jié)
以上是生活随笔為你收集整理的深度学习中张量flatten处理(flatten,reshape,reduce)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 帮助理解GAN的一些补充内容
- 下一篇: 我的机器学习入门之路(中)——深度学习(