Torch和Numpy——形状变换与维度增减

输入

import torch

a = torch.randn([2,3,2])  #生成2组3x2的随机矩阵
print(a)
b=a.reshape(3,4)  #转化为1组3x4列的
print(b)
print("_____________________________________________")
c=a.reshape(1,12)  #转化成行
print(c)
d = a.reshape(12,1)  #转化为一列
print("*******************************************")
print(d)
e = a.reshape(12)  #同上
print(e)
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
f = torch.unsqueeze(e,1)  #加上一个维度并将其变换为一列
print(f)
g = torch.squeeze(c,0)  #降低一个微电影并将其变换为一行
print(g)

输出

tensor([[[ 0.6470, -1.0670],
         [-1.0981,  1.4381],
         [-0.8501,  0.0617]],

        [[ 0.6577, -0.6435],
         [-0.3520,  0.6506],
         [ 0.7744, -0.6745]]])
tensor([[ 0.6470, -1.0670, -1.0981,  1.4381],
        [-0.8501,  0.0617,  0.6577, -0.6435],
        [-0.3520,  0.6506,  0.7744, -0.6745]])
_____________________________________________
tensor([[ 0.6470, -1.0670, -1.0981,  1.4381, -0.8501,  0.0617,  0.6577, -0.6435,
         -0.3520,  0.6506,  0.7744, -0.6745]])
*******************************************
tensor([[ 0.6470],
        [-1.0670],
        [-1.0981],
        [ 1.4381],
        [-0.8501],
        [ 0.0617],
        [ 0.6577],
        [-0.6435],
        [-0.3520],
        [ 0.6506],
        [ 0.7744],
        [-0.6745]])
tensor([ 0.6470, -1.0670, -1.0981,  1.4381, -0.8501,  0.0617,  0.6577, -0.6435,
        -0.3520,  0.6506,  0.7744, -0.6745])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensor([[ 0.6470],
        [-1.0670],
        [-1.0981],
        [ 1.4381],
        [-0.8501],
        [ 0.0617],
        [ 0.6577],
        [-0.6435],
        [-0.3520],
        [ 0.6506],
        [ 0.7744],
        [-0.6745]])
tensor([ 0.6470, -1.0670, -1.0981,  1.4381, -0.8501,  0.0617,  0.6577, -0.6435,
        -0.3520,  0.6506,  0.7744, -0.6745])

 

你可能感兴趣的:(numpy,pytorch)