1. **args, **kwargs的区别

  1. def build_vocab(self, *args, **kwargs):
  2. counter = Counter()
  3. sources = []
  4. for arg in args:
  5. if isinstance(arg, Dataset):
  6. sources += [getattr(arg, name) for name, field in
  7. arg.fields.items() if field is self]
  8. else:
  9. sources.append(arg)
  10. for data in sources:
  11. for x in data:
  12. if not self.sequential:
  13. x = [x]
  14. counter.update(x)
  15. specials = list(OrderedDict.fromkeys(
  16. tok for tok in [self.pad_token, self.init_token, self.eos_token]
  17. if tok is not None))
  18. self.vocab = Vocab(counter, specials=specials, **kwargs)

2. np.sum

  1. import numpy as np
  2. np.random.seed(0)
  3.  
  4. N, D = 3, 4
  5. x = np.random.randn(N, D)
  6. y = np.random.randn(N, D)
  7. z = np.random.randn(N, D)
  8.  
  9. a = x * y
  10. b = a + z
  11. print(b)
  12. c = np.sum(b)
  13. print(c) # 6.7170085378
  14.  
  15. # search the function of np.sum
  16. total = 0
  17. for i in range(len(b)):
  18. for j in range(4):
  19. total += b[i][j]
  20. print(total) # 6.7170085378

3. use numpy to solve grad

  1. import numpy as np
  2. N, D = 3, 4
  3. x = np.random.randn(N, D)
  4. y = np.random.randn(N, D)
  5. z = np.random.randn(N, D)
  6.  
  7. a = x * y
  8. b = a + z
  9. # print(b)
  10. c = np.sum(b)
  11. # print(c) # 6.7170085378
  12.  
  13. grad_c = 1.0
  14. grad_b = grad_c * np.ones((N, D))
  15. grad_a = grad_b.copy()
  16. grad_z = grad_b.copy()
  17. grad_x = grad_a * y
  18. grad_y = grad_a * x
  19.  
  20. print(grad_x)
  21. print(grad_y)
  22. print(grad_z)
  23. '''
  24. [[ 0.04998285 0.32809396 -0.49822878 1.36419309]
  25. [-0.52303972 -0.5881509 -0.37058995 -1.42112189]
  26. [-0.58705758 -0.26012336 1.31326911 -0.20088737]]
  27. [[ 0.14893265 -0.45509058 0.21410015 0.27659 ]
  28. [ 0.29617438 0.98971103 2.07310583 -0.0195055 ]
  29. [-1.49222601 -0.64073344 -0.18269488 0.26193553]]
  30. [[ 1. 1. 1. 1.]
  31. [ 1. 1. 1. 1.]
  32. [ 1. 1. 1. 1.]]
  33. '''

PyTorch自动计算梯度

  1. import torch
  2. from torch.autograd import Variable
  3.  
  4. N, D = 3, 4
  5. # define variables to start building a computational graph
  6. x = Variable(torch.randn(N, D), requires_grad=True)
  7. y = Variable(torch.randn(N, D), requires_grad=True)
  8. z = Variable(torch.randn(N, D), requires_grad=True)
  9.  
  10. # forward pass looks just like numpy
  11. a = x * y
  12. b = a + z
  13. c = torch.sum(b)
  14.  
  15. # calling c,backward() computes all gradients
  16. c.backward()
  17. print(x.grad.data)
  18. print(y.grad.data)
  19. print(z.grad.data)
  20. '''
  21. -0.9775 -0.0913 0.3710 1.5789
  22. 0.0896 -0.6563 0.8976 -0.3508
  23. -0.9378 0.7028 1.4533 0.9255
  24. [torch.FloatTensor of size 3x4]
  25.  
  26. 0.6365 0.2388 -0.4755 -0.9860
  27. -0.2403 -0.0468 -0.0470 -1.0132
  28. -0.5019 0.5005 -1.9270 1.0030
  29. [torch.FloatTensor of size 3x4]
  30.  
  31. 1 1 1 1
  32. 1 1 1 1
  33. 1 1 1 1
  34. [torch.FloatTensor of size 3x4]
  35. '''

x is a variable, requires_grad=True.

x.data is a tensor.

x.grad is a variable of gradients(same shape as x.data).

x.grad.data is a tensor of gradients.

4. 随机数

(1)random.seed(int)

  • 给随机数对象一个种子值,用于产生随机序列。
  • 对于同一个种子值的输入,之后产生的随机数序列也一样。
  • 通常是把时间秒数等变化值作为种子值,达到每次运行产生的随机系列都不一样
  • seed() 省略参数,意味着使用当前系统时间生成随机数

(2)random.random()

  生成随机浮点数

  1. import numpy as np
  2. np.random.seed(0)
  3.  
  4. print(np.random.random()) # 0.5488135039273248 不随时间改变
  5. print(np.random.random()) # 0.7151893663724195 不随时间改变
  6.  
  7. np.random.seed(0)
  8. print(np.random.random()) # 0.5488135039273248 不随时间改变
  9.  
  10. np.random.seed()
  11. print(np.random.random()) # 0.9623797942471012 随时间改变
  12. np.random.seed()
  13. print(np.random.random()) # 0.12734792669918393 随时间改变

(3)random.shuffle

  • 对list列表随机打乱顺序,也就是洗牌
  • shuffle只作用于list,对Str会报错比如‘abcdfed’,而['1','2','3','5','6','7']可以
  1. import numpy as np
  2.  
  3. item = [1,2,3,4,5,6,7]
  4. print(item) # [1, 2, 3, 4, 5, 6, 7]
  5. np.random.shuffle(item)
  6. print(item) # [7, 1, 2, 5, 4, 6, 3]
  7.  
  8. item2 = ['','','']
  9. np.random.shuffle(item2)
  10. print(item2) # ['1', '3', '2']

参考博客:python随机数用法

PyTorch学习笔记之计算图的更多相关文章

  1. [PyTorch 学习笔记] 1.4 计算图与动态图机制

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/computational_graph.py 计算图 深 ...

  2. Pytorch学习笔记(二)---- 神经网络搭建

    记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...

  3. 【pytorch】pytorch学习笔记(一)

    原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...

  4. Pytorch学习笔记(一)---- 基础语法

    书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...

  5. 【深度学习】Pytorch 学习笔记

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  6. Pytorch学习笔记(一)——简介

    一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...

  7. 莫烦pytorch学习笔记(二)——variable

    .简介 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Variable和tensor的区别和联系 Variable是篮子, ...

  8. [PyTorch 学习笔记] 1.3 张量操作与线性回归

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...

  9. [PyTorch 学习笔记] 1.1 PyTorch 简介与安装

    PyTorch 的诞生 2017 年 1 月,FAIR(Facebook AI Research)发布了 PyTorch.PyTorch 是在 Torch 基础上用 python 语言重新打造的一款深 ...

随机推荐

  1. centos7 安装显卡驱动方法

    方法一: 首先需要添加一个第三方的源ELRepo.这个源支持RED HAT系的Linux系统,主要是提供一些硬件的驱动程序.这个源的主页如下: http://elrepo.org/tiki/tiki- ...

  2. 包含min函数的栈 【微软面试100题 第二题】

    题目要求:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(1). 参考题目:剑指offer第21题. 题目分析: 1. ...

  3. jquery版列表切换功能

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  4. linux内核代码注释 赵炯 第三章引导启动程序

    linux内核代码注释 第三章引导启动程序 boot目录中的三个汇编代码文件   bootsect.s和setup.s采用近似intel的汇编语法,需要8086汇编器连接器as86和ld86 head ...

  5. [python工具][2]sublime的快捷键

    十.Sublime Text 快捷键列表 快捷键按类型分列如下: 1.通用  ↑↓← →    上下左右移动光标 Alt    调出菜单 Ctrl + Shift + P    调出命令板(Comma ...

  6. 用Navicat Premium同步表和数据

    1.选择工具 2.选择数据库 3.下一步选择表 注意:同步表的时候是先删除存在的表再创建表同步数据 SQL Server数据库转换MySQL数据库 https://blog.csdn.net/zhan ...

  7. 【转】[译]深入理解JVM

    http://www.cnblogs.com/enjiex/p/5079338.html 深入理解JVM 原文链接:http://www.cubrid.org/blog/dev-platform/un ...

  8. 算法复习——1D/1Ddp优化

    搬讲义~~~~ 题目1:玩具装箱(bzoj1010) Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一 ...

  9. Java面试题之什么情况下会触发类的初始化

    以下情况会触发类的初始化: 遇到new,getstatic,putstatic,invokestatic这4条指令: 使用java.lang.reflect包的方法对类进行反射调用: 初始化一个类的时 ...

  10. OpenStack 通用设计思路

    API 前端服务 每个 OpenStack 组件可能包含若干子服务,其中必定有一个 API 服务负责接收客户请求. 以 Nova 为例,nova-api 作为 Nova 组件对外的唯一窗口,向客户暴露 ...