学习记录
在学习浏览网页test过程中遇到的一些需要研究一下才能看懂的函数,在这里做一下记录,供以后翻看学习
1. maximum()
1 import numpy as np 2 N, D_in, H, D_out = 64, 1000, 100, 10 3 x = np.random.randn(N, D_in) # (64, 1000) 4 y = np.random.randn(N, D_out) 5 w1 = np.random.randn(D_in, H) # (1000, 100) 6 w2 = np.random.randn(H, D_out) 7 learning_rate = 1e-6 8 9 h = x.dot(w1) # (64, 100) 10 h_pred = np.maximum(h, 0) 11 print(np.maximum(np.eye(2), [0.5, 2])) 12 print(np.eye(2)) 13 ''' 14 [[ 1. 2. ] 15 [ 0.5 2. ]] 16 [[ 1. 0.] 17 [ 0. 1.]] 18 '''
2. isinstance()
1 def weight_init(m): 2 # 使用isinstance来判断m属于什么类型 3 if isinstance(m, nn.Conv2d): 4 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels 5 m.weight.data.normal_(0, math.sqrt(2. / n)) 6 elif isinstance(m, nn.BatchNorm2d): 7 # m中的weight,bias其实都是Variable,为了能学习参数以及后向传播 8 m.weight.data.fill_(1) 9 m.bias.data.zero_()
3. type()
1 dtype = torch.LongTensor 2 3 x = torch.randn(64, 1000).type(dtype) # [torch.LongTensor of size 64x1000] 4 # 一般torch.randn()出来的类型是[torch.FloatTensor of size 64x1000]
4. 关于loss.data[0]的一点思考
loss 是variable,loss.data 是tensor,里面只有一个值,取这个值用[0]
1 a = torch.FloatTensor([1.0]) 2 print(a) 3 ''' 4 1 5 [torch.FloatTensor of size 1] 6 ''' 7 print(type(a)) # <class 'torch.FloatTensor'> 8 print(a[0]) # 1.0 9 10 b = torch.FloatTensor([1.0, 2.0]) 11 print(b) 12 ''' 13 1 14 2 15 [torch.FloatTensor of size 2] 16 ''' 17 print(b[0]) # 1.0 18 print(b[1]) # 2.0
5. 矩阵加法
1 a = torch.LongTensor([[1, 2, 4], [2, 4, 5]]) 2 print(a) 3 b = torch.LongTensor([[1], [2]]) 4 print(b) 5 c = a + b 6 print(c) 7 ''' 8 1 2 4 9 2 4 5 10 [torch.LongTensor of size 2x3] 11 12 13 1 14 2 15 [torch.LongTensor of size 2x1] 16 17 18 2 3 5 19 4 6 7 20 [torch.LongTensor of size 2x3] 21 '''
6. 矩阵乘法
1 a = torch.LongTensor([[1, 2], [2, 3]]) 2 print(a) 3 b = torch.LongTensor([[1, 2], [2, 3]]) 4 print(b) 5 c = a * b # 按位相乘 6 print(c) 7 d = a.mm(b) # 矩阵乘法 8 print(d)
7. tensor与numpy
1 a = torch.ones(5) 2 print(a) 3 ''' 4 1 5 1 6 1 7 1 8 1 9 [torch.FloatTensor of size 5] 10 ''' 11 import numpy as np 12 b = np.ones(5) 13 print(b) 14 ''' 15 [ 1. 1. 1. 1. 1.] 16 '''
8. masked_scatter
Copies elements from source
into self
tensor at positions where the mask
is one.
The shape of mask
must be broadcastable with the shape of the underlying tensor. The source
should have at least as many elements as the number of ones in mask
1 import torch 2 from torch.autograd import Variable 3 4 x = Variable(torch.rand(4), requires_grad=True) 5 y = Variable(torch.ones(4), requires_grad=True) 6 m = Variable(torch.ByteTensor([1, 1, 0, 1])) 7 8 z = x.masked_scatter(m, 2 * y) 9 print(x) # Variable containing: 0.5922 0.9239 0.6392 0.3757 [torch.FloatTensor of size 4] 10 print(y) # Variable containing: 1 1 1 1 [torch.FloatTensor of size 4] 11 print(m) # Variable containing: 1 1 0 1 [torch.ByteTensor of size 4] 12 print(z) # Variable containing: 2.0000 2.0000 0.6392 2.0000 [torch.FloatTensor of size 4]
9. torch.bernoulli()函数,伯努利函数
Draws binary random numbers (0 or 1) from a bernoulli distribution.
1 a = torch.FloatTensor(3, 3).uniform_(0, 1) 2 b = torch.bernoulli(a) 3 print(a) 4 print(b) 5 ''' 6 0.0755 0.2350 0.2137 7 0.7441 0.6608 0.6628 8 0.9452 0.2539 0.4811 9 [torch.FloatTensor of size 3x3] 10 11 0 0 0 12 0 1 1 13 1 0 1 14 [torch.FloatTensor of size 3x3] 15 ''' 16 a2 = torch.ones(3, 3) 17 b2 = torch.bernoulli(a2) 18 # print(a2) 19 print(b2) 20 ''' 21 1 1 1 22 1 1 1 23 1 1 1 24 [torch.FloatTensor of size 3x3] 25 ''' 26 a3 = torch.zeros(3, 3) 27 b3 = torch.bernoulli(a3) 28 # print(a3) 29 print(b3) 30 ''' 31 0 0 0 32 0 0 0 33 0 0 0 34 [torch.FloatTensor of size 3x3] 35 '''
10.
torch.
div
(input, value, out=None) out = tensor / value- out=tensor/value
torch.
div
(input, other, out=None) out(i) = input(i) / other(i)
1 a = torch.randn(5) 2 print(a) 3 a = torch.div(a, 0.5) 4 print(a) 5 ''' 6 0.2992 7 -0.1313 8 1.1855 9 1.3246 10 1.9544 11 [torch.FloatTensor of size 5] 12 0.5983 13 -0.2626 14 2.3711 15 2.6491 16 3.9088 17 [torch.FloatTensor of size 5] 18 ''' 19 20 a2 = torch.FloatTensor([[1, 2, 1, 2], [2, 3, 2, 3], [2, 4, 2, 4], [2, 3, 2, 3]]) 21 print(a2) 22 b2 = torch.FloatTensor([[2, 4], [1, 2], [2, 4], [1, 2], [2, 4], [1, 2], [2, 4], [1, 2]]) 23 print(b2) 24 c = torch.div(a2, b2) 25 print(c) 26 ''' 27 1 2 1 2 28 2 3 2 3 29 2 4 2 4 30 2 3 2 3 31 [torch.FloatTensor of size 4x4] 32 2 4 33 1 2 34 2 4 35 1 2 36 2 4 37 1 2 38 2 4 39 1 2 40 [torch.FloatTensor of size 8x2] 41 0.5000 0.5000 1.0000 1.0000 42 1.0000 0.7500 2.0000 1.5000 43 1.0000 1.0000 2.0000 2.0000 44 1.0000 0.7500 2.0000 1.5000 45 [torch.FloatTensor of size 4x4] 46 '''
11. lambda
lambda的主体是一个表达式,而不是一个代码块。
lambda表达式是起到一个函数速写的作用。允许在代码内嵌入一个函数的定义。
1 f = lambda x,y,z:x+y+z 2 a = f(1,2,3) 3 # print(a) # 6 4 5 def action(x): 6 return lambda y:x+y 7 aa = action(2) 8 aaa = aa(22) 9 # print(aaa) # 24 10 11 b = lambda x:lambda y:x+y 12 bb = b(3) 13 # print(bb(2)) # 5 14 t = (b(2))(2) 15 # print(t) # 4
12. permute
1 import torch 2 3 x = torch.randn(2,3,4) # [torch.FloatTensor of size 2x3x4] 4 x = x.permute(0,2,1) # [torch.FloatTensor of size 2x4x3]
13. tensor.contiguous
有些tensor不是整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行一下contiguous()这个操作。
14. 打乱数据
1 x = torch.randn(5, 3) 2 x_perm = x[torch.randperm(5)] 3 print(x) 4 print(x_perm) 5 ''' 6 0.4550 -0.0629 0.2606 7 0.7032 -0.0657 -1.0674 8 -1.3008 0.2316 -0.6869 9 0.7777 0.5782 -0.4003 10 -0.0646 0.3088 0.3421 11 [torch.FloatTensor of size 5x3] 12 13 14 0.7777 0.5782 -0.4003 15 0.4550 -0.0629 0.2606 16 0.7032 -0.0657 -1.0674 17 -0.0646 0.3088 0.3421 18 -1.3008 0.2316 -0.6869 19 [torch.FloatTensor of size 5x3] 20 '''
两个一起变化
1 import random 2 3 # random.shuffle() 4 x = torch.randn(5,3) 5 y = torch.randn(5,3) 6 # print(torch.randperm(5)) 7 perm_list = torch.randperm(5) # [torch.LongTensor of size 5] 8 x_perm = x[perm_list] 9 y_perm = y[perm_list] 10 print(x) 11 print(x_perm) 12 print(y) 13 print(y_perm) 14 ''' 15 -1.4091 -1.4140 0.5146 16 0.7924 0.4531 -0.7110 17 -1.2801 -1.8292 0.3379 18 -0.1075 -1.2411 1.5390 19 -0.4313 1.1896 -0.3205 20 [torch.FloatTensor of size 5x3] 21 22 23 -1.4091 -1.4140 0.5146 24 -0.4313 1.1896 -0.3205 25 -1.2801 -1.8292 0.3379 26 0.7924 0.4531 -0.7110 27 -0.1075 -1.2411 1.5390 28 [torch.FloatTensor of size 5x3] 29 30 31 0.9184 -0.6786 1.6780 32 -0.2860 1.2021 -0.5194 33 -1.2354 -0.4750 0.8994 34 0.1048 0.5882 -2.0871 35 0.1144 1.5287 -0.6208 36 [torch.FloatTensor of size 5x3] 37 38 39 0.9184 -0.6786 1.6780 40 0.1144 1.5287 -0.6208 41 -1.2354 -0.4750 0.8994 42 -0.2860 1.2021 -0.5194 43 0.1048 0.5882 -2.0871 44 [torch.FloatTensor of size 5x3] 45 '''
15 torch.gather()
torch.
gather
(input, dim, index, out=None) → Tensor
(1) input与index维度相同
1 import torch 2 t = torch.Tensor([[1,2],[3,4]]) 3 print(t) 4 t2 = torch.gather(t,1,torch.LongTensor([[0,0],[1,0]])) 5 print(t2) 6 t3 = torch.gather(t,0,torch.LongTensor([[0,0],[1,0]])) 7 print(t3) 8 ''' 9 1 2 10 3 4 11 [torch.FloatTensor of size 2x2] 12 1 1 13 4 3 14 [torch.FloatTensor of size 2x2] 15 1 2 16 3 2 17 [torch.FloatTensor of size 2x2] 18 '''
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0 out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1 out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
out[i][j] = input[index[i][j]][j] # if dim == 0
out[i][j] = input[i][index[i][j]] # if dim == 1
(2)input与index维度不相同,dim为他们不相同的维度值
1 input = Variable(torch.LongTensor([[1,2],[3,4]])) 2 print(input) 3 index = Variable(torch.LongTensor([[0,0],[1,0]])) 4 index0 = Variable(torch.LongTensor([[0],[1]])).view(2, 1) 5 print(index0) 6 r = torch.gather(input, 1, index0) 7 print(r) 8 ''' 9 Variable containing: 10 1 2 11 3 4 12 [torch.LongTensor of size 2x2] 13 14 Variable containing: 15 0 16 1 17 [torch.LongTensor of size 2x1] 18 19 Variable containing: 20 1 21 4 22 [torch.LongTensor of size 2x1] 23 '''
16 torch.masked_select()
1 import torch 2 x = torch.randn(3, 4) 3 print(x) 4 mask = x.ge(0.5) 5 print(mask) 6 print(torch.masked_select(x, mask)) 7 ''' 8 -1.5765 -1.2328 1.2179 0.8232 9 0.2317 0.2671 0.1412 -0.2669 10 -0.5362 0.6746 1.0946 -0.5848 11 [torch.FloatTensor of size 3x4] 12 0 0 1 1 13 0 0 0 0 14 0 1 1 0 15 [torch.ByteTensor of size 3x4] 16 1.2179 17 0.8232 18 0.6746 19 1.0946 20 [torch.FloatTensor of size 4] 21 '''
17. scatter_()
scatter_
(dim, index, src) → Tensor
Writes all values from the tensor src
into self
at the indices specified in the index
tensor.
For each value in src
, its output index is specified by its index in src
for dimension != dim
and by the corresponding value in index
for dimension = dim
.
self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0 self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1 self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
This is the reverse operation of the manner described ingather()
1 x = torch.FloatTensor([[[1,3,5],[2,6,4],[4,7,8]],[[1,3,5],[2,6,4],[4,7,8]],[[1,3,5],[2,6,4],[4,7,8]]]) 2 print(x) 3 index = torch.LongTensor([[[1,1,1]],[[2,2,2]],[[0,0,0]]]) 4 print(index) 5 x = torch.zeros(3,3,3).scatter_(1, index, x) 6 print(x) 7 ''' 8 (0 ,.,.) = 9 1 3 5 10 2 6 4 11 4 7 8 12 (1 ,.,.) = 13 1 3 5 14 2 6 4 15 4 7 8 16 (2 ,.,.) = 17 1 3 5 18 2 6 4 19 4 7 8 20 [torch.FloatTensor of size 3x3x3] 21 22 (0 ,.,.) = 23 1 1 1 24 (1 ,.,.) = 25 2 2 2 26 (2 ,.,.) = 27 0 0 0 28 [torch.LongTensor of size 3x1x3] 29 30 (0 ,.,.) = 31 0 0 0 32 1 3 5 33 0 0 0 34 (1 ,.,.) = 35 0 0 0 36 0 0 0 37 1 3 5 38 (2 ,.,.) = 39 1 3 5 40 0 0 0 41 0 0 0 42 [torch.FloatTensor of size 3x3x3] 43 '''
another example
1 x = torch.LongTensor([[[1,3,5],[2,6,4],[4,7,8]],[[2,6,4],[4,7,8],[1,3,5]],[[1,3,5],[4,7,8],[2,6,4]]]) 2 print(x) 3 src = torch.LongTensor([[[0,0,0]],[[1,1,1]],[[0,0,0]]]) 4 print(src) 5 index = torch.LongTensor([[[2,2,2]],[[1,1,1]],[[0,0,0]]]) 6 print(index) 7 x.scatter_(1, index, src) 8 print(x) 9 ''' 10 (0 ,.,.) = 11 1 3 5 12 2 6 4 13 4 7 8 14 (1 ,.,.) = 15 2 6 4 16 4 7 8 17 1 3 5 18 (2 ,.,.) = 19 1 3 5 20 4 7 8 21 2 6 4 22 [torch.LongTensor of size 3x3x3] 23 24 (0 ,.,.) = 25 0 0 0 26 (1 ,.,.) = 27 1 1 1 28 (2 ,.,.) = 29 0 0 0 30 [torch.LongTensor of size 3x1x3] 31 32 (0 ,.,.) = 33 2 2 2 34 (1 ,.,.) = 35 1 1 1 36 (2 ,.,.) = 37 0 0 0 38 [torch.LongTensor of size 3x1x3] 39 40 (0 ,.,.) = 41 1 3 5 42 2 6 4 43 0 0 0 44 (1 ,.,.) = 45 2 6 4 46 1 1 1 47 1 3 5 48 (2 ,.,.) = 49 0 0 0 50 4 7 8 51 2 6 4 52 [torch.LongTensor of size 3x3x3] 53 '''