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()函数的功能是什么?

 t = torch.Tensor([[1, 2], [3, 4]])
print(t)
a = torch.gather(t, 1, torch.LongTensor([[0,0], [1,0]]))
print(a)
'''
1 2
3 4
[torch.FloatTensor of size 2x2] 1 1
4 3
[torch.FloatTensor of size 2x2]
'''

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() 函数的功能是什么?

 a = torch.FloatTensor([[1, 2], [3, 4]])
b = torch.norm(a)
print(a)
print(b)
'''
1 2
3 4
[torch.FloatTensor of size 2x2] 5.477225575051661
'''

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:

 loss = Variable(torch.FloatTensor([1]))
print(loss.data) # 1 [torch.FloatTensor of size 1]
print(loss.data[0]) # 1.0

最新文章

  1. Ruby--学习记录(实时更新)
  2. [Doxygen]Doxygen
  3. hadoop rpc基础
  4. php上传$_FILES 无法取值
  5. WPF中多个RadioButton绑定到一个属性
  6. 入门Linux
  7. squid判断文件是否修改机制分析
  8. 三、freemarker数据、模版指令
  9. Migration from Zend Framework v2 to v3
  10. [HAOI2007]上升序列
  11. AIROBOT系统 之 私人存储 和 DLNA 智能电视云
  12. sort和uniq去重操作【转】
  13. c# 数据结构 ArrayList
  14. django补充和form组件
  15. wamp设置自定义域名访问php网站
  16. mysql创建utf8数据库
  17. Linux进程间通信(消息队列/信号量+共享内存)
  18. linux下mysql 启动命令
  19. SQL优化:清理生产环境中已失效字段基本步骤
  20. JavaScript实现的3D球面标签云效果

热门文章

  1. HDU:5040-Instrusive
  2. Python中的并发
  3. 数据结构和算法(What Why How)
  4. optimize table在优化mysql时很重要
  5. HDU 3473 Minimum Sum 划分树
  6. 零基础学习 Python 之数字与运算
  7. 【bzoj3676】[Apio2014]回文串 回文自动机
  8. spring入门到放弃——spring事务管理
  9. 刷题总结——寻宝游戏(bzoj3991 dfs序)
  10. Linux 系统自动备份数据库及定时任务的设置