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)
?
Q5:
1 loss = Variable(torch.FloatTensor([1])) 2 print(loss.data) # 1 [torch.FloatTensor of size 1] 3 print(loss.data[0]) # 1.0?
轉載于:https://www.cnblogs.com/Joyce-song94/p/7252206.html
總結
以上是生活随笔為你收集整理的PyTorch学习问题记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win7下Anaconda3+Tenso
- 下一篇: Android源代码解析之(四)--gt