Tensorflow:轉(zhuǎn)置函數(shù) transpose的使用詳解
我就廢話不多說(shuō),咱直接看代碼吧!
tf.transpose
transpose( a, perm=None, name='transpose' )
Defined in tensorflow/python/ops/array_ops.py.
See the guides: Math > Matrix Math Functions, Tensor Transformations > Slicing and Joining
Transposes a. Permutes the dimensions according to perm.
The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1…0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.
For example:
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x) # [[1, 4]
# [2, 5]
# [3, 6]]
tf.transpose(x, perm=[1, 0]) # [[1, 4]
# [2, 5]
# [3, 6]]
# 'perm' is more useful for n-dimensional tensors, for n > 2
x = tf.constant([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
# Take the transpose of the matrices in dimension-0
tf.transpose(x, perm=[0, 2, 1]) # [[[1, 4],
# [2, 5],
# [3, 6]],
# [[7, 10],
# [8, 11],
# [9, 12]]]
a的轉(zhuǎn)置是根據(jù) perm 的設(shè)定值來(lái)進(jìn)行的。
返回?cái)?shù)組的 dimension(尺寸、維度) i與輸入的 perm[i]的維度相一致。如果未給定perm,默認(rèn)設(shè)置為 (n-1…0),這里的 n 值是輸入變量的 rank 。因此默認(rèn)情況下,這個(gè)操作執(zhí)行了一個(gè)正規(guī)(regular)的2維矩形的轉(zhuǎn)置
例如:
x = [[1 2 3]
[4 5 6]]
tf.transpose(x) ==> [[1 4]
[2 5]
[3 6]]
tf.transpose(x) 等價(jià)于:
tf.transpose(x perm=[1, 0]) ==> [[1 4]
[2 5]
[3 6]]
a=tf.constant([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
x=tf.transpose(a,[1,0,2])
array([[[ 1, 2, 3],
[ 7, 8, 9]],
[[ 4, 5, 6],
[10, 11, 12]]])
x=tf.transpose(a,[0,2,1])
array([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]]])
x=tf.transpose(a,[2,1,0])
array([[[ 1, 7],
[ 4, 10]],
[[ 2, 8],
[ 5, 11]],
[[ 3, 9],
[ 6, 12]]])
array([[[ 1, 7],
[ 4, 10]],
[[ 2, 8],
[ 5, 11]],
[[ 3, 9],
[ 6, 12]]])
x=tf.transpose(a,[1,2,0])
array([[[ 1, 7],
[ 2, 8],
[ 3, 9]],
[[ 4, 10],
[ 5, 11],
[ 6, 12]]])
以上這篇Tensorflow:轉(zhuǎn)置函數(shù) transpose的使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python導(dǎo)出requirements.txt的幾種方法總結(jié)
這篇文章主要介紹了python導(dǎo)出requirements.txt的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
WxPython開(kāi)發(fā)之列表數(shù)據(jù)的自定義打印處理
這篇文章主要為大家詳細(xì)介紹了如何利用WxPython內(nèi)置的打印數(shù)據(jù)組件實(shí)現(xiàn)列表數(shù)據(jù)的自定義打印處理,以及對(duì)記錄進(jìn)行分頁(yè)等常規(guī)操作,需要的可以參考下2025-03-03
使用OpenCV實(shí)現(xiàn)人臉圖像卡通化的示例代碼
這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)人臉圖像卡通化的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度
這篇文章主要介紹了Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
深入理解?Python?中的?pip?虛擬環(huán)境(最佳實(shí)踐)
本文深入講解了Python中pip虛擬環(huán)境的概念及其重要性,并詳細(xì)介紹了如何創(chuàng)建、激活和管理虛擬環(huán)境,以及如何使用requirements.txt文件記錄和管理項(xiàng)目依賴,文章指出,使用虛擬環(huán)境可以有效避免依賴沖突,為每個(gè)項(xiàng)目提供一個(gè)干凈的開(kāi)發(fā)環(huán)境,使得項(xiàng)目更易于維護(hù)和部署2024-10-10
使用Python串口實(shí)時(shí)顯示數(shù)據(jù)并繪圖的例子
今天小編就為大家分享一篇使用Python串口實(shí)時(shí)顯示數(shù)據(jù)并繪圖的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
yolov5使用flask部署至前端實(shí)現(xiàn)照片\視頻識(shí)別功能
初學(xué)者在使用YOLO和Flask構(gòu)建應(yīng)用時(shí),往往需要實(shí)現(xiàn)上傳圖片和視頻的識(shí)別功能,本文介紹了如何在Flask框架中實(shí)現(xiàn)這一功能,包括文件上傳、圖片放大查看、視頻識(shí)別以及識(shí)別后的文件下載,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09

