Tensors
1. construst matrix
2. addition
3. slice
1 from __future__ import print_function 2 import torch 3 4 # construst a 5*3 matrix. 5 # method 1 6 # x = torch.LongTensor(5, 3) 7 x = torch.FloatTensor(5, 3) 8 print(x) 9 # method 2 10 x = torch.randn(5, 3) 11 print(x) 12 13 # get size 14 print(x.size()) # torch.Size([5, 3]) 15 16 # operations 17 # addition method 1 18 y = torch.rand(5, 3) 19 print(x+y) 20 # addition method 2 21 print(torch.add(x, y)) 22 23 # addition method 3 give an output tensor 24 result = torch.FloatTensor(5, 3) 25 torch.add(x, y, out=result) 26 print(result) 27 28 # addition method 4 in-place 29 y._add_(x) 30 print(y) 31 32 print(x[:, 1])
torch.numel()
1 # torch.numel(tensor)->int 2 # Returns the total number of elements in the input Tensor. 3 a = torch.randn(1, 2, 3, 4, 5) 4 print(torch.numel(a)) # 120 5 b = torch.zeros(4, 4) 6 print(torch.numel(b)) # 16
torch.eye()
1 # torch.eye(n,m=none,out=none) 2 # Returns a 2-D tensor with ones on the diagonal and zeros elsewhere. 3 a = torch.eye(3) 4 print(a) 5 ''' 6 1 0 0 7 0 1 0 8 0 0 1 9 [torch.FloatTensor of size 3x3] 10 ''' 11 b = torch.FloatTensor(2, 3) 12 torch.eye(2, 3, out=b) 13 print(b)
torch.from_numpy()
1 # torch.from_numpy(ndarray)->tensor 2 # Creates a Tensor from a numpy.ndarray 3 import numpy as np 4 a = np.array([1, 2, 3]) 5 t = torch.from_numpy(a) 6 print(t) 7 ''' 8 1 9 2 10 3 11 [torch.IntTensor of size 3] 12 ''' 13 t[0] = -1 14 print(a) # [-1 2 3]
torch.linspace()
1 # torch.linspace(start,end,steps=100,out=none)->tensor 2 # Returns a one-dimensional Tensor of steps equally spaced points between start and end 3 # The output tensor is 1D of size steps 4 a = torch.linspace(3, 10, steps=5) 5 print(a) 6 ''' 7 3.0000 8 4.7500 9 6.5000 10 8.2500 11 10.0000 12 [torch.FloatTensor of size 5] 13 ''' 14 b = torch.linspace(-10, 10, steps=5) 15 print(b) 16 ''' 17 -10 18 -5 19 0 20 5 21 10 22 [torch.FloatTensor of size 5] 23 '''
torch.logspace()
1 # torch.logspace(start,end,steps=100,out=none)->tensor 2 a = torch.logspace(start=-10, end=10, steps=5) 3 print(a) 4 b = torch.logspace(0.1, 1.0, steps=5) 5 print(b) 6 ''' 7 1.0000e-10 8 1.0000e-05 9 1.0000e+00 10 1.0000e+05 11 1.0000e+10 12 [torch.FloatTensor of size 5] 13 14 1.2589 15 2.1135 16 3.5481 17 5.9566 18 10.0000 19 [torch.FloatTensor of size 5] 20 '''
torch.ones()
1 # torch.ones(*sizes,out=none)->tensor 2 print(torch.ones(2, 3)) 3 a = torch.FloatTensor(5) 4 torch.ones(5, out=a) 5 print(a) 6 ''' 7 1 1 1 8 1 1 1 9 [torch.FloatTensor of size 2x3] 10 11 1 12 1 13 1 14 1 15 1 16 [torch.FloatTensor of size 5] 17 '''
torch.rand() 和 torch.randn()
1 # torch.rand(*size,out=none)->tensor 2 # Returns a Tensor filled with random numbers from a uniform distribution on the interval [0,1) 3 a = torch.rand(2, 3) 4 print(a) 5 6 # torch.randn(*size,out=none)->tensor 7 # Returns a Tensor filled with random numbers from a normal distribution with zero mean and variance of one. 8 b = torch.randn(2, 3) 9 print(b) 10 ''' 11 0.4152 0.8297 0.7144 12 0.9190 0.2360 0.8195 13 [torch.FloatTensor of size 2x3] 14 15 0.1718 0.1721 -1.3157 16 -0.3688 -1.4767 0.2299 17 [torch.FloatTensor of size 2x3] 18 '''
torch.randperm()
1 # torch.randperm(n,out=none)->longtensor 2 # Returns a random permutation of integers from 0 to n - 1. 3 print(torch.randperm(4)) 4 ''' 5 1 6 0 7 2 8 3 9 [torch.LongTensor of size 4] 10 '''
torch.arange() 和 torch.range()
1 # torch.arange(start,end,step=1,out=none)->tensor 2 print(torch.arange(1, 2.5, 0.5)) 3 ''' 4 1.0000 5 1.5000 6 2.0000 7 [torch.FloatTensor of size 3] 8 ''' 9 10 # torch.range(start,end,step=1,out=none)->tensor 11 print(torch.range(1, 2.5, 0.5)) 12 ''' 13 1.0000 14 1.5000 15 2.0000 16 2.5000 17 [torch.FloatTensor of size 4] 18 '''
torch.cat()
1 x = torch.randn(2, 3) 2 print(x) 3 print(torch.cat((x, x, x), 0)) 4 print(torch.cat((x, x, x), 1)) 5 ''' 6 -0.7142 -0.7174 -0.3007 7 0.4409 0.4892 0.2450 8 [torch.FloatTensor of size 2x3] 9 10 -0.7142 -0.7174 -0.3007 11 0.4409 0.4892 0.2450 12 -0.7142 -0.7174 -0.3007 13 0.4409 0.4892 0.2450 14 -0.7142 -0.7174 -0.3007 15 0.4409 0.4892 0.2450 16 [torch.FloatTensor of size 6x3] 17 18 -0.7142 -0.7174 -0.3007 -0.7142 -0.7174 -0.3007 -0.7142 -0.7174 -0.3007 19 0.4409 0.4892 0.2450 0.4409 0.4892 0.2450 0.4409 0.4892 0.2450 20 [torch.FloatTensor of size 2x9] 21 '''
torch.gather()
1 t = torch.Tensor([[1, 2], [3, 4]]) 2 print(t) 3 a = torch.gather(t, 1, torch.LongTensor([[0,0], [1,0]])) 4 print(a) 5 ''' 6 1 2 7 3 4 8 [torch.FloatTensor of size 2x2] 9 10 1 1 11 4 3 12 [torch.FloatTensor of size 2x2] 13 '''
torch.squeeze()
1 x = torch.zeros(2,1,2,1,2) # # torch.Size([2, 1, 2, 1, 2]) 2 y1 = torch.squeeze(x) # torch.Size([2, 2, 2]) 3 y2 = torch.squeeze(x,0) # torch.Size([2, 1, 2, 1, 2]) 4 y3 = torch.squeeze(x,1) # torch.Size([2, 2, 1, 2]) 5 y4 = torch.squeeze(x,2) # torch.Size([2, 1, 2, 1, 2]) 6 y5 = torch.squeeze(x,3) # torch.Size([2, 1, 2, 2]) 7 print(x) 8 ''' 9 (0 ,0 ,0 ,.,.) = 10 0 0 11 12 (0 ,0 ,1 ,.,.) = 13 0 0 14 15 (1 ,0 ,0 ,.,.) = 16 0 0 17 18 (1 ,0 ,1 ,.,.) = 19 0 0 20 [torch.FloatTensor of size 2x1x2x1x2] 21 '''
torch.unsqueeze()
1 x = torch.Tensor([[1,2],[3,4]]) 2 print(x) 3 print(torch.unsqueeze(x,0)) 4 print(torch.unsqueeze(x,1)) 5 ''' 6 1 2 7 3 4 8 [torch.FloatTensor of size 2x2] 9 10 (0 ,.,.) = 11 1 2 12 3 4 13 [torch.FloatTensor of size 1x2x2] 14 15 (0 ,.,.) = 16 1 2 17 18 (1 ,.,.) = 19 3 4 20 [torch.FloatTensor of size 2x1x2] 21 '''
torch.t() 和 torch.transpose()
1 x = torch.randn(2,3) 2 print(x) # [torch.FloatTensor of size 2x3] 3 print(torch.t(x)) # [torch.FloatTensor of size 3x2] 4 print(torch.transpose(x,0,1)) # the same as the above one 5 print(torch.transpose(x,1,0)) # the same as the above one
torch.bernoulli()
1 a = torch.Tensor(3,3).uniform_(0,1) 2 print(a) 3 print(torch.bernoulli(a)) 4 ''' 5 0.1873 0.2061 0.8647 6 0.0393 0.3320 0.5299 7 0.4289 0.7352 0.7775 8 [torch.FloatTensor of size 3x3] 9 10 0 0 1 11 0 0 0 12 1 1 1 13 [torch.FloatTensor of size 3x3] 14 '''
numpy bridge
1 import torch 2 import numpy as np 3 4 # converting torch tensor to numpy array 5 a = torch.ones(5) # [torch.FloatTensor of size 5] 6 b = a.numpy() # [ 1. 1. 1. 1. 1.] 7 8 # converting numpy array to torch tensor 9 c = np.ones(5) 10 d = torch.from_numpy(c) 11 np.add(c,1,out=c) 12 13 # cuda tensors 14 x = torch.randn(2,3) 15 y = torch.randn(2,3) 16 if torch.cuda.is_available(): 17 x = x.cuda() 18 y = y.cuda() 19 print(x + y) 20 ''' 21 -2.1743 0.3657 -1.7976 22 1.5615 0.4806 -3.0541 23 [torch.cuda.FloatTensor of size 2x3 (GPU 0)] 24 '''
torch.normal() 和 torch.norm()
1 a = torch.normal(means=torch.arange(1, 11), std=torch.arange(1, 0.1, -0.1)) 2 print(a) 3 means = torch.arange(1, 11) # [torch.FloatTensor of size 10] 4 std = torch.arange(1, 0, -0.1) # [torch.FloatTensor of size 11] 5 6 b = torch.normal(mean=0.5, std=torch.arange(1,6)) 7 print(b) 8 9 c = torch.normal(means=torch.arange(1, 6)) 10 print(c) 11 ''' 12 0.3882 13 0.8929 14 2.6018 15 4.7097 16 4.4020 17 5.8976 18 6.5632 19 7.7387 20 8.9351 21 10.0563 22 [torch.FloatTensor of size 10] 23 24 -0.6031 25 1.6011 26 -1.6568 27 -5.8402 28 8.0830 29 [torch.FloatTensor of size 5] 30 31 -0.5711 32 2.4982 33 3.6022 34 3.4958 35 6.2261 36 [torch.FloatTensor of size 5] 37 '''
torch.norm()
1 a = torch.FloatTensor([[1, 2], [3, 4]]) 2 b = torch.norm(a) 3 print(a) 4 print(b) 5 ''' 6 1 2 7 3 4 8 [torch.FloatTensor of size 2x2] 9 10 5.477225575051661 11 '''
randomly sample from uniform distribution in [-0.1,0.1]
1 import torch 2 a = torch.normal(mean=0.0, std=torch.arange(0.09, 0, -0.09)) 3 print(a)
参看官网教程:http://pytorch.org/docs/master/torch.html