Pytorch中的view()函数的用法

Pytorch中的View操作

代码

import torch
v1 = torch.arange(0, 16)
print(v1)
v2 = v1.view(-1, 8)
print(v2)
v3 = v1.view(-1, 1)
print(v3)
v4 = v1.view(-1)

运行结果

tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
tensor([[ 0,  1,  2,  3,  4,  5,  6,  7],
        [ 8,  9, 10, 11, 12, 13, 14, 15]])
tensor([[ 0],
        [ 1],
        [ 2],
        [ 3],
        [ 4],
        [ 5],
        [ 6],
        [ 7],
        [ 8],
        [ 9],
        [10],
        [11],
        [12],
        [13],
        [14],
        [15]])
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

总结
view(-1, *)是根据后面的维度自动调整维度,view(-1)是直接拍成一维的

你可能感兴趣的:(pytorch学习,pytorch,深度学习,人工智能)