PyTorch学习问题记录
Q1:def train() 中的model.train()的作用是什么?为什么要写?
A1:class torch.nn.Module中 train
(mode=True)
Sets the module in training mode. This has any effect only on modules such as Dropout or BatchNorm.
参看 http://pytorch.org/docs/master/nn.html
Q2: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 '''
A2:
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]
out[i][j] = input[i][index[i][j]]
out[0][0] = input[0][index[0][0]] = input[0][0] = 1
out[0][1] = input[0][index[0][1]] = input[0][0] = 1
out[1][0] = input[1][index[1][0]] = input[1][1] = 4
out[1][1] = input[1][index[1][1]] = input[1][0] = 3
Q3: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 '''
A3:
norm() 函数是求范数,一般默认是2范数。平方和开根号。
参考博文:几种范数的简单介绍
normal() 函数是求正太分布。
Q4: topk()函数
- torch.Tensor.topk (Python method, in torch.Tensor) ||
topk
(k, dim=None, largest=True, sorted=True) -> (Tensor, LongTensor) - torch.topk (Python function, in torch) ||
torch.
topk
(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)
1 topi = torch.LongTensor([5]) # [torch.LongTensor of size 1]
2 topii = torch.LongTensor([[5]]) # [torch.LongTensor of size 1x1]
3 ni = topi[0]
4 nii = topii[0][0]
5 print(ni, nii) # 5 5
Q5:
1 loss = Variable(torch.FloatTensor([1])) 2 print(loss.data) # 1 [torch.FloatTensor of size 1] 3 print(loss.data[0]) # 1.0