Pytorch

    技术2022-08-11  95

    欢迎来到黄黄自学室

    pytorch基础数据类型标量dim=1dim=2dim=3dim=4 亲爱的朋友们,任何时候都要抬头挺胸收下巴,慢慢追赶,心向阳光,无暇痴想!

    pytorch基础数据类型

    标量

    #标量 a = torch.tensor(2.2) print(a.shape) #a的形状 print(a.size()) print(len(a.shape)) #a的维度 print(a.dim())

    输出:

    torch.Size([]) torch.Size([]) 0 0

    dim=1

    #dim=1的张量 print(torch.tensor(1.1)) print(torch.tensor([1.1, 2.2])) print(torch.FloatTensor(1)) print(torch.FloatTensor(2)) data = np.ones(2) print(data) print(torch.from_numpy(data))

    输出:

    tensor(1.1000) tensor([1.1000, 2.2000]) tensor([1.1000]) tensor([1.1000, 2.2000]) [1. 1.] tensor([1., 1.], dtype=torch.float64)

    dim=2

    #dim=2 可以理解为线代里面的秩rank 生成两行三列的向量 a=torch.randn(2,3) #randn高斯随机分布 print(a) print(a.shape) print(a.size(0)) #size取shape的第一个元素 print(a.size(1)) print(a.shape[0])

    输出:

    tensor([[-1.2463, -1.7079, 0.3362], [ 0.0221, -0.3282, 2.6912]]) torch.Size([2, 3]) 2 3 2

    dim=3

    #dim=3 a = torch.rand(1,2,3) #rand随机均匀分布[0,1] print(a) print(a.shape) print(a[0]) print(list(a.shape))

    输出:

    tensor([[[0.6837, 0.0755, 0.8848], [0.4772, 0.6598, 0.0356]]]) torch.Size([1, 2, 3]) tensor([[0.6837, 0.0755, 0.8848], [0.4772, 0.6598, 0.0356]]) [1, 2, 3]

    dim=4

    #dim=4 a = torch.rand(2, 1, 28, 28) #28,28为长和宽 1为单通道 灰度图,若是彩色图 则是rgb 3通道 #batch为2,2张照片 print(a) print(a.shape) print(a.numel()) #占有的内存为2*1*28*28=1568 print(len(a.shape)) print(a.dim()) #维度 tensor([[[[0.5542, 0.6195, 0.8397, ..., 0.0999, 0.5630, 0.8705], [0.0801, 0.2280, 0.8619, ..., 0.9935, 0.2579, 0.9406], [0.7946, 0.9227, 0.1112, ..., 0.5450, 0.5005, 0.8216], ..., [0.5081, 0.2886, 0.3900, ..., 0.1594, 0.4824, 0.0905], [0.2204, 0.4182, 0.2404, ..., 0.8443, 0.2388, 0.5995], [0.9903, 0.0996, 0.7635, ..., 0.6127, 0.3190, 0.3793]]], [[[0.4489, 0.1073, 0.7817, ..., 0.4795, 0.0331, 0.9607], [0.9574, 0.9217, 0.4065, ..., 0.0366, 0.8986, 0.8122], [0.0629, 0.8873, 0.2454, ..., 0.3124, 0.2565, 0.9617], ..., [0.6944, 0.6282, 0.2614, ..., 0.8821, 0.0859, 0.7367], [0.3204, 0.7058, 0.9221, ..., 0.5302, 0.8725, 0.5979], [0.8618, 0.9294, 0.0506, ..., 0.6029, 0.8625, 0.3238]]]]) torch.Size([2, 1, 28, 28]) 1568 4 4
    Processed: 0.036, SQL: 9