TensorFlow2 維度變換

2022-02-25 11:43:00 字數 3370 閱讀 3590

目錄tensorflow2教程完整教程目錄(更有python、go、pytorch、tensorflow、爬蟲、人工智慧教學等著你):

import tensorflow as tf
a = tf.random.normal([4, 28, 28, 3])
a.shape, a.ndim
(tensorshape([4, 28, 28, 3]), 4)
tf.reshape(a, [4, 784, 3]).shape  # 給出一張某個通道的資料,丟失行、寬的資訊
tensorshape([4, 784, 3])
tf.reshape(a, [4, -1, 3]).shape  # 4*(-1)*3 = 4*28*28*3
tensorshape([4, 784, 3])
tf.reshape(a, [4, 784*3]).shape  # 給出一張的所有資料,丟失行、寬和通道的資訊
tensorshape([4, 2352])
tf.reshape(a, [4, -1]).shape
tensorshape([4, 2352])
tf.reshape(tf.reshape(a, [4, -1]), [4, 28, 28, 3]).shape
tensorshape([4, 28, 28, 3])
tf.reshape(tf.reshape(a, [4, -1]), [4, 14, 56, 3]).shape
tensorshape([4, 14, 56, 3])
tf.reshape(tf.reshape(a, [4, -1]), [4, 1, 784, 3]).shape
tensorshape([4, 1, 784, 3])
first reshape:

second reshape:

a = tf.random.normal((4, 3, 2, 1))
a.shape
tensorshape([4, 3, 2, 1])
tf.transpose(a).shape
tensorshape([1, 2, 3, 4])
tf.transpose(a, perm=[0, 1, 3, 2]).shape  # 按照索引替換維度
tensorshape([4, 3, 1, 2])
a = tf.random.normal([4, 28, 28, 3])  # b,h,w,c
a.shape
tensorshape([4, 28, 28, 3])
tf.transpose(a, [0, 2, 1, 3]).shape  # b,2,h,c
tensorshape([4, 28, 28, 3])
tf.transpose(a, [0, 3, 2, 1]).shape  # b,c,w,h
tensorshape([4, 3, 28, 28])
tf.transpose(a, [0, 3, 1, 2]).shape  # b,c,h,w
tensorshape([4, 3, 28, 28])
add school dim(增加學校的維度):

a = tf.random.normal([4, 25, 8])
a.shape
tensorshape([4, 25, 8])
tf.expand_dims(a, axis=0).shape  # 索引0前
tensorshape([1, 4, 25, 8])
tf.expand_dims(a, axis=3).shape  # 索引3前
tensorshape([4, 25, 8, 1])
tf.expand_dims(a,axis=-1).shape  # 索引-1後
tensorshape([4, 25, 8, 1])
tf.expand_dims(a,axis=-4).shape  # 索引-4後,即左邊空白處
tensorshape([1, 4, 25, 8])
only squeeze for shape = 1 dim(只刪除維度為1的維度)

tf.squeeze(tf.zeros([1,2,1,1,3])).shape
tensorshape([2, 3])
a = tf.zeros([1,2,1,3])
a.shape
tensorshape([1, 2, 1, 3])
tf.squeeze(a,axis=0).shape
tensorshape([2, 1, 3])
tf.squeeze(a,axis=2).shape
tensorshape([1, 2, 3])
tf.squeeze(a,axis=-2).shape
tensorshape([1, 2, 3])
tf.squeeze(a,axis=-4).shape
tensorshape([2, 1, 3])

tensorflow2 0 維度變換

a tf.random.normal 4,28,28,3 a 1 tf.reshape a,4,784,3 shape 1 a 1 tf.reshape a,4,1,3 shape 和1等價 a 2 tf.reshape a,4,784 3 shape 2 a 2 tf.reshape a,4,1 ...

TensorFlow 維度變換

基本的維度變換包含了改變檢視 reshape,插入新維度 expand dims,刪除維 squeeze,交換維度 transpose,複製資料 tile 等。四 交換維度 五 資料複製 六 broadcasting 自動擴充套件 張量的檢視就是我們理解張量的方式,比如shape 為 2,4,4,3...

TensorFlow2簡單入門 三維張量

三維張量的乙個典型應用是表示序列訊號,它的格式是 x b,se quen ce l en,f eatu re l en x b,sequence len,feature len x b,s eque nce len,feat ure len 其中b bb表示序列訊號的數量,seq uenc e le...