import torch
import tensorflow as tf
import numpy as np
#tf.expand_dims(input, axis=1) <-> tf.squeeze(input, axis=1)
#torch.tensor.unsqueeze(axis=1) <-> torch.tensor.squeeze(axis=1)
a = torch.tensor([3,4,5,6])
#增加维度
b = a.unsqueeze(1) #在a的第1维增加一个维度
c = tf.expand_dims(a,1) #在a的第1维增加一个维度
#降低维度
d = b.squeeze(1) #将b的第1维 去掉
e = tf.squeeze(b,1) #将b的第1维 去掉
#需要注意:没有 tf.unsqueeze() 这个函数
import numpy as np
a_array = np.array(a)
b_array = np.expand_dims(a_array, 1) #在数组的第1维增加一个维度
给tensor增加维度 或 减少维度
于 2022-07-06 16:20:53 首次发布