PyTorch学习笔记之计算图
1. **args, **kwargs的区别
def build_vocab(self, *args, **kwargs):
counter = Counter()
sources = []
for arg in args:
if isinstance(arg, Dataset):
sources += [getattr(arg, name) for name, field in
arg.fields.items() if field is self]
else:
sources.append(arg)
for data in sources:
for x in data:
if not self.sequential:
x = [x]
counter.update(x)
specials = list(OrderedDict.fromkeys(
tok for tok in [self.pad_token, self.init_token, self.eos_token]
if tok is not None))
self.vocab = Vocab(counter, specials=specials, **kwargs)
2. np.sum
import numpy as np
np.random.seed(0) N, D = 3, 4
x = np.random.randn(N, D)
y = np.random.randn(N, D)
z = np.random.randn(N, D) a = x * y
b = a + z
print(b)
c = np.sum(b)
print(c) # 6.7170085378 # search the function of np.sum
total = 0
for i in range(len(b)):
for j in range(4):
total += b[i][j]
print(total) # 6.7170085378
3. use numpy to solve grad
import numpy as np
N, D = 3, 4
x = np.random.randn(N, D)
y = np.random.randn(N, D)
z = np.random.randn(N, D) a = x * y
b = a + z
# print(b)
c = np.sum(b)
# print(c) # 6.7170085378 grad_c = 1.0
grad_b = grad_c * np.ones((N, D))
grad_a = grad_b.copy()
grad_z = grad_b.copy()
grad_x = grad_a * y
grad_y = grad_a * x print(grad_x)
print(grad_y)
print(grad_z)
'''
[[ 0.04998285 0.32809396 -0.49822878 1.36419309]
[-0.52303972 -0.5881509 -0.37058995 -1.42112189]
[-0.58705758 -0.26012336 1.31326911 -0.20088737]]
[[ 0.14893265 -0.45509058 0.21410015 0.27659 ]
[ 0.29617438 0.98971103 2.07310583 -0.0195055 ]
[-1.49222601 -0.64073344 -0.18269488 0.26193553]]
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]
'''
PyTorch自动计算梯度
import torch
from torch.autograd import Variable N, D = 3, 4
# define variables to start building a computational graph
x = Variable(torch.randn(N, D), requires_grad=True)
y = Variable(torch.randn(N, D), requires_grad=True)
z = Variable(torch.randn(N, D), requires_grad=True) # forward pass looks just like numpy
a = x * y
b = a + z
c = torch.sum(b) # calling c,backward() computes all gradients
c.backward()
print(x.grad.data)
print(y.grad.data)
print(z.grad.data)
'''
-0.9775 -0.0913 0.3710 1.5789
0.0896 -0.6563 0.8976 -0.3508
-0.9378 0.7028 1.4533 0.9255
[torch.FloatTensor of size 3x4] 0.6365 0.2388 -0.4755 -0.9860
-0.2403 -0.0468 -0.0470 -1.0132
-0.5019 0.5005 -1.9270 1.0030
[torch.FloatTensor of size 3x4] 1 1 1 1
1 1 1 1
1 1 1 1
[torch.FloatTensor of size 3x4]
'''
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()
生成随机浮点数
import numpy as np
np.random.seed(0) print(np.random.random()) # 0.5488135039273248 不随时间改变
print(np.random.random()) # 0.7151893663724195 不随时间改变 np.random.seed(0)
print(np.random.random()) # 0.5488135039273248 不随时间改变 np.random.seed()
print(np.random.random()) # 0.9623797942471012 随时间改变
np.random.seed()
print(np.random.random()) # 0.12734792669918393 随时间改变
(3)random.shuffle
- 对list列表随机打乱顺序,也就是洗牌
- shuffle只作用于list,对Str会报错比如‘abcdfed’,而['1','2','3','5','6','7']可以
import numpy as np item = [1,2,3,4,5,6,7]
print(item) # [1, 2, 3, 4, 5, 6, 7]
np.random.shuffle(item)
print(item) # [7, 1, 2, 5, 4, 6, 3] item2 = ['','','']
np.random.shuffle(item2)
print(item2) # ['1', '3', '2']
参考博客:python随机数用法
PyTorch学习笔记之计算图的更多相关文章
- [PyTorch 学习笔记] 1.4 计算图与动态图机制
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/computational_graph.py 计算图 深 ...
- Pytorch学习笔记(二)---- 神经网络搭建
记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- Pytorch学习笔记(一)---- 基础语法
书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...
- 【深度学习】Pytorch 学习笔记
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- Pytorch学习笔记(一)——简介
一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...
- 莫烦pytorch学习笔记(二)——variable
.简介 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Variable和tensor的区别和联系 Variable是篮子, ...
- [PyTorch 学习笔记] 1.3 张量操作与线性回归
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...
- [PyTorch 学习笔记] 1.1 PyTorch 简介与安装
PyTorch 的诞生 2017 年 1 月,FAIR(Facebook AI Research)发布了 PyTorch.PyTorch 是在 Torch 基础上用 python 语言重新打造的一款深 ...
随机推荐
- poj 2376 选择工作区间问题 贪心算法
题意:给一些工作区间,如何选取最小的工作数量,覆盖[1,T]的工作时长 一开始的思路,当然也是错误的思路: 我想着,最小工作数量是吧?那肯定就是选择结束时间最晚的,给结束时间来一个排序.哎这个思路错误 ...
- LA 7056 Colorful Toy Polya定理
题意: 平面上给出一个\(N\)个点\(M\)条边的无向图,要用\(C\)种颜色去给每个顶点染色. 如果一种染色方案可以旋转得到另一种染色方案,那么说明这两种染色方案是等价的. 求所有染色方案数 \( ...
- Django one
WEB-Django: Http协议: http协议:超文本传输协议,基于TCP/IP通信协议来传递数据 特点: 1.灵活:允许传输任意类型的数据对象.正在传输的类型有Content-Type标记 2 ...
- rocketmq源码分析2-broker的消息接收
broker消息接收,假设接收的是一个普通消息(即没有事务),此处分析也只分析master上动作逻辑,不涉及ha. 1. 如何找到消息接收处理入口 可以通过broker的监听端口10911顺藤摸瓜式的 ...
- ant 入门级详解
ant 入门级详解 [转载的地址(也是转载,未找到原文地址)]https://www.cnblogs.com/jsfx/p/6233645.html 1,什么是antant是构建工具2,什么是构建 ...
- JS手风琴特效
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Python开启进程的2中方式
知识点一:进程的理论 进程:正在进行的一个程序或者说一个任务,而负责执行任务的则是CPU 进程运行的的三种状态: 1.运行:(由CPU来执行,越多越好,可提高效率) 2.阻塞:(遇到了IQ,3个里面可 ...
- 【bzoj4407】于神之怒加强版 莫比乌斯反演+线性筛
题目描述 给下N,M,K.求 输入 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意义如上式所示. 输出 如题 ...
- 【bzoj3526】[Poi2014]Card 线段树区间合并
题目描述 有n张卡片在桌上一字排开,每张卡片上有两个数,第i张卡片上,正面的数为a[i],反面的数为b[i].现在,有m个熊孩子来破坏你的卡片了!第i个熊孩子会交换c[i]和d[i]两个位置上的卡片. ...
- ubuntu服务器与本地文件传输
ubuntu SSH 连接.远程上传下载文件 博客分类: Ubuntu 安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH ...