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学习笔记之计算图的更多相关文章

  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. 创建Django项目并将其部署在腾讯云上

    这段时间在做scrapy爬虫,对爬出来的数据基于Django做了统计与可视化,本想部署在腾讯云上玩玩,但是因为以前没有经验遇到了一些问题,在这里记录一下: 首先说下Django的创建与配置: 1. 创 ...

  2. strcpy和strncpy用法和区别

    1. strcpy函数:顾名思义字符串复制函数:原型:extern char *strcpy(char *dest,char *src); 功能:把从src地址开始且含有NULL结束符的字符串赋值到以 ...

  3. BZOJ 5441: [Ceoi2018]Cloud computing

    背包 #include<cstdio> #include<algorithm> using namespace std; int n,m,Len; long long F[2] ...

  4. UVa 10934 DP Dropping water balloons

    首先想一下特殊情况,如果只有一个气球,我们要确定高度只能从下往上一层一层地测试,因为如果气球一旦爆了,便无法测出气球的硬度. 如果气球有无数个,那么就可以用二分的方法来确定. 一般地,用d(i, j) ...

  5. CodeForces 570E DP Pig and Palindromes

    题意:给出一个n行m列的字符矩阵,从左上角走到右下角,每次只能往右或者往下走,求一共有多少种走法能得到回文串. 分析: 可以从两头开始考虑,每次只走一样字符的格子,这样得到的两个字符串拼起来之后就是一 ...

  6. kafka消息的可靠性

    本文来自网易云社区 作者:田宏增 Kafka的高可靠性的保障来源于其健壮的副本(replication)策略.通过调节其副本相关参数,可以使得Kafka在性能和可靠性之间运转的游刃有余.Kafka从0 ...

  7. RHEL6.X设置163yum源

    目录 RHEL6.X设置163yum源 卸载系统的yum 检查是否已经卸载完成 下载yum以及相关包 安装yum相关rpm包 清除原有缓存,建立yum列表 本地yum源设置 挂载本地光盘 修改配置文件 ...

  8. 03-python进阶-爬虫入门-正则

    [urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...

  9. DS作业06-图

    1.本周学习总结(0--2分) 1.1思维导图 1.2谈谈你对图结构的认识及学习体会. 图这一章的学习,是经过树学习后,难得一章重新寻找到感觉的学习.因为这一章比较少用递归,使用的是结构体,很多东西我 ...

  10. 九度oj 题目1041:Simple Sorting

    题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...