1. 动手学深度学习基础

import torch

X=torch.arange(12,dtype=torch.float32).reshape((3,4))
Y=torch.tensor([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]])
print(X,Y,torch.cat((X,Y),dim=0),torch.cat((X,Y),dim=1),sep='\n')
print(torch.arange(6),torch.arange(6).reshape(2,3),sep='\n')
print(X,X[0],X[2],X[0:2],sep='\n')
Z=torch.arange(24).reshape((2,3,4))
X=X.reshape((4,3))
Y=torch.tensor([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]]).reshape((4,3))
print(Z,Y,sep='\n')
# print(X,Z,X+Z,sep='\n') #RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 2
x=torch.rand(2,3,4)
print(x)
x_with_2n3_dimention=x[1,:,:]
scalar_x=x[1,1,1]#first value from each dimension
#numpy like slicing
print(x_with_2n3_dimention)
x=torch.rand(2,3)
print(x)
print(x[:,1:])#skipping first column
print(x[:-1,:])#skipping last row
#transpose
print(x,x.t(),sep='\n')
y=torch.arange(12).reshape(3,4)
x=torch.arange(18).reshape(3,6)
print(x,y,torch.cat((x,y),dim=1),sep='\n')
print(torch.cat((x,x)),torch.cat((x,x),dim=1),sep='\n')
print(torch.cat((y,y)),torch.cat((y,y),dim=1),sep='\n')#默认拼接dim=0
t1=torch.cat((x,x))
t2=torch.stack((x,x))
print(t1,t1.size(),t2,t2.size(),sep='\n')
t3=t2.view(-1)
print(t3.storage())
x1=torch.rand(2,3,3)#a tensor of size 3,2,1
splitted=x1.split(split_size=2,dim=0)
print('x1: ',x1,'splitted: ',splitted,sep='\n')#2 tensors of 2x2 and 1x2 size
#squeeze and unsqueeze
csqueeze=x2.squeeze()#remove the 1 sized dimention,如果没有1 sized,则不移除。
print(x2,squeeze,sep='\n')
x3=torch.rand(3)
with_fake_dimention=x3.unsqueeze(0)
print(x3,with_fake_dimention,sep='\n')#added a fake zeroth dimension

输出

tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]])
tensor([[2., 1., 4., 3.],
        [1., 2., 3., 4.],
        [4., 3., 2., 1.]])
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [ 2.,  1.,  4.,  3.],
        [ 1.,  2.,  3.,  4.],
        [ 4.,  3.,  2.,  1.]])
tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])
tensor([0, 1, 2, 3, 4, 5])
tensor([[0, 1, 2],
        [3, 4, 5]])
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]])
tensor([0., 1., 2., 3.])
tensor([ 8.,  9., 10., 11.])
tensor([[0., 1., 2., 3.],
        [4., 5., 6., 7.]])
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])
tensor([[2., 1., 4.],
        [3., 1., 2.],
        [3., 4., 4.],
        [3., 2., 1.]])
tensor([[[0.6340, 0.3403, 0.3935, 0.6987],
         [0.0420, 0.0932, 0.6484, 0.7204],
         [0.7527, 0.8322, 0.4876, 0.8779]],

        [[0.8270, 0.5663, 0.0538, 0.8164],
         [0.1572, 0.2372, 0.6838, 0.9293],
         [0.1142, 0.0425, 0.9784, 0.4684]]])
tensor([[0.8270, 0.5663, 0.0538, 0.8164],
        [0.1572, 0.2372, 0.6838, 0.9293],
        [0.1142, 0.0425, 0.9784, 0.4684]])
tensor([[0.4269, 0.0942, 0.2748],
        [0.9709, 0.2098, 0.8590]])
Traceback (most recent call last):
  File "F:/WorkPlacePy/PycharmProjects1/Pytorch深度学习实战书籍练习/chapter1.py", line 39, in <module>
    csqueeze=x2.squeeze()#remove the 1 sized dimention,如果没有1 sized,则不移除。
NameError: name 'x2' is not defined
tensor([[0.0942, 0.2748],
        [0.2098, 0.8590]])
tensor([[0.4269, 0.0942, 0.2748]])
tensor([[0.4269, 0.0942, 0.2748],
        [0.9709, 0.2098, 0.8590]])
tensor([[0.4269, 0.9709],
        [0.0942, 0.2098],
        [0.2748, 0.8590]])
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([[ 0,  1,  2,  3,  4,  5,  0,  1,  2,  3],
        [ 6,  7,  8,  9, 10, 11,  4,  5,  6,  7],
        [12, 13, 14, 15, 16, 17,  8,  9, 10, 11]])
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]])
tensor([[ 0,  1,  2,  3,  4,  5,  0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11,  6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([[ 0,  1,  2,  3,  0,  1,  2,  3],
        [ 4,  5,  6,  7,  4,  5,  6,  7],
        [ 8,  9, 10, 11,  8,  9, 10, 11]])
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17],
        [ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11],
        [12, 13, 14, 15, 16, 17]])
torch.Size([6, 6])
tensor([[[ 0,  1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10, 11],
         [12, 13, 14, 15, 16, 17]],

        [[ 0,  1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10, 11],
         [12, 13, 14, 15, 16, 17]]])
torch.Size([2, 3, 6])
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
[torch.LongStorage of size 36]
x1: 
tensor([[[0.3491, 0.7096, 0.6774],
         [0.8273, 0.2891, 0.0725],
         [0.7264, 0.0226, 0.3569]],

        [[0.4286, 0.9802, 0.5050],
         [0.3303, 0.7117, 0.2414],
         [0.9537, 0.7252, 0.8141]]])
splitted: 
(tensor([[[0.3491, 0.7096, 0.6774],
         [0.8273, 0.2891, 0.0725],
         [0.7264, 0.0226, 0.3569]],

        [[0.4286, 0.9802, 0.5050],
         [0.3303, 0.7117, 0.2414],
         [0.9537, 0.7252, 0.8141]]]),)

Process finished with exit code 1

 

上一篇:Python从入门到实战代码行行标注----几种常见的Tensor


下一篇:Tensor中范数运算