TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)
初學(xué)tensorflow,如果寫的不對(duì)的,請(qǐng)更正,謝謝!
tf.reshape(tensor, shape, name=None)
函數(shù)的作用是將tensor變換為參數(shù)shape的形式。
其中shape為一個(gè)列表形式,特殊的一點(diǎn)是列表中可以存在-1。-1代表的含義是不用我們自己指定這一維的大小,函數(shù)會(huì)自動(dòng)計(jì)算,但列表中只能存在一個(gè)-1。(當(dāng)然如果存在多個(gè)-1,就是一個(gè)存在多解的方程了)
好了我想說的重點(diǎn)還有一個(gè)就是根據(jù)shape如何變換矩陣。其實(shí)簡單的想就是,
reshape(t, shape) => reshape(t, [-1]) => reshape(t, shape)
首先將矩陣t變?yōu)橐痪S矩陣,然后再對(duì)矩陣的形式更改就可以了。
官方的例子:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]]
# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7
在舉幾個(gè)例子或許就清楚了,有一個(gè)數(shù)組z,它的shape屬性是(4, 4)
z = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
z.shape
(4, 4)
z.reshape(-1)
z.reshape(-1) array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
z.reshape(-1, 1)
也就是說,先前我們不知道z的shape屬性是多少,但是想讓z變成只有一列,行數(shù)不知道多少,通過`z.reshape(-1,1)`,Numpy自動(dòng)計(jì)算出有12行,新的數(shù)組shape屬性為(16, 1),與原來的(4, 4)配套。
z.reshape(-1,1)
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15],
[16]])
z.reshape(-1, 2)
newshape等于-1,列數(shù)等于2,行數(shù)未知,reshape后的shape等于(8, 2)
z.reshape(-1, 2)
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
到此這篇關(guān)于TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)TensorFlow的reshape操作 tf.reshape內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 關(guān)于Tensorflow中的tf.train.batch函數(shù)的使用
- tf.truncated_normal與tf.random_normal的詳細(xì)用法
- tensorflow之變量初始化(tf.Variable)使用詳解
- tensorflow 用矩陣運(yùn)算替換for循環(huán) 用tf.tile而不寫for的方法
- 對(duì)tf.reduce_sum tensorflow維度上的操作詳解
- Tensorflow 利用tf.contrib.learn建立輸入函數(shù)的方法
- Tensorflow中tf.ConfigProto()的用法詳解
- tensorflow中tf.slice和tf.gather切片函數(shù)的使用
- tf.concat中axis的含義與使用詳解
- tensorflow實(shí)現(xiàn)在函數(shù)中用tf.Print輸出中間值
相關(guān)文章
python實(shí)現(xiàn)時(shí)間序列自相關(guān)圖(acf)、偏自相關(guān)圖(pacf)教程
這篇文章主要介紹了python實(shí)現(xiàn)時(shí)間序列自相關(guān)圖(acf)、偏自相關(guān)圖(pacf)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
簡明 Python 基礎(chǔ)學(xué)習(xí)教程
無論您剛接觸電腦還是一個(gè)有經(jīng)驗(yàn)的程序員,本書都將有助您學(xué)習(xí)使用Python語言2007-02-02
python 將視頻 通過視頻幀轉(zhuǎn)換成時(shí)間實(shí)例
這篇文章主要介紹了python 將視頻 通過視頻幀轉(zhuǎn)換成時(shí)間實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Django中和時(shí)區(qū)相關(guān)的安全問題詳解
這篇文章主要給大家介紹了關(guān)于Django中和時(shí)區(qū)相關(guān)的安全問題的相關(guān)資料,需要的朋友可以參考下2020-10-10
關(guān)于python中remove的一些坑小結(jié)
這篇文章主要給大家介紹了關(guān)于python中remove的一些坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python中關(guān)于面向?qū)ο蟾拍畹脑敿?xì)講解
要了解面向?qū)ο笪覀兛隙ㄐ枰戎缹?duì)象到底是什么玩意兒。關(guān)于對(duì)象的理解很簡單,在我們的身邊,每一種事物的存在都是一種對(duì)象??偨Y(jié)為一句話也就是:對(duì)象就是事物存在的實(shí)體2021-10-10
ubuntu20.04運(yùn)用startup application開機(jī)自啟動(dòng)python程序的腳本寫法
這篇文章主要介紹了ubuntu20.04運(yùn)用startup application開機(jī)自啟動(dòng)python程序的腳本寫法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10

