TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)
初學(xué)tensorflow,如果寫的不對的,請更正,謝謝!
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矩陣,然后再對矩陣的形式更改就可以了。
官方的例子:
# 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)容請搜索腳本之家以前的文章或繼續(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的方法
- 對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)文章
PyCharm Community安裝與配置的詳細(xì)教程
這篇文章主要介紹了PyCharm Community安裝與配置的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
對pandas中兩種數(shù)據(jù)類型Series和DataFrame的區(qū)別詳解
今天小編就為大家分享一篇對pandas中兩種數(shù)據(jù)類型Series和DataFrame的區(qū)別詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python實(shí)現(xiàn)GUI圖片瀏覽的小程序
這篇文章主要介紹了Python實(shí)現(xiàn)GUI圖片瀏覽程序,程序的實(shí)現(xiàn)需要pillow庫,pillow是 Python 的第三方圖像處理庫,需要安裝才能實(shí)用,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Numpy中np.dot與np.matmul的區(qū)別詳解
本文主要介紹了Numpy中np.dot與np.matmul的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
python中的單引號雙引號區(qū)別知識點(diǎn)總結(jié)
在本篇文章中小編給大家整理了關(guān)于python中的單引號雙引號有什么區(qū)別的相關(guān)知識點(diǎn)以及實(shí)例代碼,需要的朋友們參考下。2019-06-06
Python實(shí)現(xiàn)銀行賬戶資金交易管理系統(tǒng)
這篇文章主要介紹了Python銀行賬戶資金交易管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
Python?matplotlib繪圖時(shí)使用鼠標(biāo)滾輪放大/縮小圖像
Matplotlib是Python程序員可用的事實(shí)上的繪圖庫,雖然它比交互式繪圖庫在圖形上更簡單,但它仍然可以一個(gè)強(qiáng)大的工具,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib繪圖時(shí)使用鼠標(biāo)滾輪放大/縮小圖像的相關(guān)資料,需要的朋友可以參考下2022-05-05

