淺談tensorflow使用張量時(shí)的一些注意點(diǎn)tf.concat,tf.reshape,tf.stack
有一段時(shí)間沒用tensorflow了,現(xiàn)在跑實(shí)驗(yàn)還是存在一些坑了,主要是關(guān)于張量計(jì)算的問題。tensorflow升級1.0版本后與以前的版本并不兼容,可能出現(xiàn)各種奇奇怪怪的問題。
1 tf.concat函數(shù)
tensorflow1.0以前函數(shù)用法:tf.concat(concat_dim, values, name='concat'),第一個(gè)參數(shù)為連接的維度,可以將幾個(gè)向量按指定維度連接起來。
如:
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] #按照第0維連接 tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] #按照第1維連接 tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
tf.concat的作用主要是將向量按指定維連起來,其余維度不變;而1.0版本以后,函數(shù)的用法變成:
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] #按照第0維連接 tf.concat( [t1, t2],0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] #按照第1維連接 tf.concat([t1, t2],1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
位置變了,需要注意。
2 tf.stack函數(shù)
用法:stack(values, axis=0, name=”stack”):
“”“Stacks a list of rank-R tensors into one rank-(R+1) tensor.
x = tf.constant([1, 4]) y = tf.constant([2, 5]) z = tf.constant([3, 6]) tf.stack([x,y,z]) ==> [[1,4],[2,5],[3,6]] tf.stack([x,y,z],axis=0) ==> [[1,4],[2,5],[3,6]] tf.stack([x,y,z],axis=1) ==> [[1, 2, 3], [4, 5, 6]]
tf.stack將一組R維張量變?yōu)镽+1維張量。注意:tf.pack已經(jīng)變成了tf.stack
3.tf.reshape
用法:reshape(tensor, shape, name=None):主要通過改變張量形狀,可以從高維變低維,也可以從低維變高維;
a = tf.Variable(initial_value=[[1,2,3],[4,5,6]]) ==> shape:[2,3] b = tf.Variable(initial_value=[[[1,2,3],[4,5,6]],[[7,8,9],[1,0,2]]]) ==> shape:[2,2,3] a_1 = tf.reshape(a,[2,1,1,3]) ==> [[[[1,2,3]]],[[[4,5,6]]]] a_2 = tf.reshape(a,[2,1,3]) ==> [[[1,2,3]],[[4,5,6]]] b_1 = tf.reshape(b,[2,2,1,3]) ==> [[[[1,2,3]],[[4,5,6]]],[[[7,8,9]],[[1,0,2]]]] new_1 = tf.concat([b_1,a_1],1) new_2 = tf.reshape(tf.concat([b,a_2],1),[2,3,1,3]) """ new_1: [[[[1 2 3]] [[4 5 6]] [[1 2 3]]] [[[7 8 9]] [[1 0 2]] [[4 5 6]]]] new_2; [[[[1 2 3]] [[4 5 6]] [[1 2 3]]] [[[7 8 9]] [[1 0 2]] [[4 5 6]]]]
補(bǔ)充知識:tensorflow中的reshape(tensor,[1,-1])和reshape(tensor,[-1,1])
和python 中的reshape用法應(yīng)該一樣
import tensorflow as tf a = [[1,2],[3,4],[5,6]] tf.reshape(a,[-1,1]) Out[13]: <tf.Tensor 'Reshape_4:0' shape=(6, 1) dtype=int32> tf.reshape(tf.reshape(a,[-1,1]),[1,-1]) Out[14]: <tf.Tensor 'Reshape_6:0' shape=(1, 6) dtype=int32>
tf.reshape(tensor,[-1,1])將張量變?yōu)橐痪S列向量
tf.reshape(tensor,[1,-1])將張量變?yōu)橐痪S行向量
以上這篇淺談tensorflow使用張量時(shí)的一些注意點(diǎn)tf.concat,tf.reshape,tf.stack就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用python檢查磁盤空間使用情況的代碼實(shí)現(xiàn)
本文將向讀者展示如何利用Python編寫自動化腳本,以檢查磁盤空間使用情況,無論你是經(jīng)驗(yàn)豐富的系統(tǒng)管理員,還是對Python自動化充滿興趣的開發(fā)者,本文都將為你提供實(shí)用的腳本示例和詳細(xì)的解析步驟,幫助你快速掌握磁盤空間監(jiān)控的自動化方法,需要的朋友可以參考下2024-08-08Pythont特殊語法filter,map,reduce,apply使用方法
這篇文章主要介紹了Pythont特殊語法filter,map,reduce,apply使用方法,需要的朋友可以參考下2016-02-02使用python在校內(nèi)發(fā)人人網(wǎng)狀態(tài)(人人網(wǎng)看狀態(tài))
人人網(wǎng)怎么發(fā)狀態(tài)?下面使用python實(shí)現(xiàn)這個(gè)功能,大家參考使用吧2014-02-02Python+wxPython實(shí)現(xiàn)文件內(nèi)容搜索工具
在本篇文章中,我們將介紹如何使用?wxPython?庫創(chuàng)建一個(gè)簡單的文件搜索工具,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-08-08python實(shí)現(xiàn)銀聯(lián)支付和支付寶支付接入
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)銀聯(lián)支付和支付寶支付的接入,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05