当前位置:网站首页>NLP Natural Language Processing (2)
NLP Natural Language Processing (2)
2022-07-30 03:04:00 【perfunctory zgf】
NLP自然语言处理(二)
一、pytorchBack propagation calculation method and gradient
pytorch完成线性回归
- tensor中的require_grad参数
a.设置为True,Says it will record thetensor的计算过程,追踪对于该张量的所有操作,Every time calculation will modify itsgard_fn属性,用来记录做过的操作. - tensor中的grad_fn属性
a.Used to store the computing process - tensorDon't keep calculation process
a. with torch.no_grad()
为了防止跟踪历史记录(和使用内存),可以将代码块包装在with torch.no_grad():中,In the evaluation model can be used,模型具有requires_grad = True的可训练参数,But we don't need in the process of gradient calculation. - 反向传播:
a. out.backward() 梯度计算,保存到x.gard中
b.Derivative is saved in thetensor.grad,The default gradient will accumulate - tensor.data
a.获取tensorReference value in the operation(只有值) - tensor.numpy ()
a.当tensorIn need to compute the gradient,grad_fn不为None的时候,
tensor.data.numpy()、tensor.detach().numpy()能够实现对tensorDeep copy of the data in the,转化为ndarray类型
二、线性回归的实现
基础模型是y = wx+b 其中w和b均为参数,使用 y = 3x + 0.8 来构造数据x,y Through the model should be able to find outw和b 的值接近3和0.8
import torch
import matplotlib.pyplot as plt
from numpy import *
learning_rate = 0.01
# 1.准备数据
# y = 3x + 0.8 只有一个x是一维
# 基础模型是y = wx+b 其中w b均为参数,使用 y = 3x + 0.8 来构造数据x,y Through the model should be able to find outw b 的值接近3 0.8
# 构造一个500行 1列的数据 rand() 0-1
x = torch.rand([500,1])
y_true = x*3 + 0.8 # x 与 y 都是 500行 1列
# 2.通过模型计算y_predict
# requires_grad=TrueSays it will record thetensor的计算过程,默认是False
w = torch.rand([1,1],requires_grad=True) # [1,1]是因为x[500,1]与[1,1]相乘是[500,1]
b = torch.tensor(0,requires_grad=True,dtype=torch.float32) # b全为0
# 4.通过循环,反向传播,更新参数
for i in range(2000):
# 3.计算损失值
y_predict = torch.matmul(x, w) + b # matmul()矩阵乘法
loss = (y_true - y_predict).pow(2).mean()
# 先判断wIs it a number 即不为None
if w.grad is not None :
# 是一个数
w.data.zero_() # 将w _ In situ modification as0 Zero operating Before each back-propagation gradient will buy0
if b.grad is not None:
b.data.zero_()
loss.backward() # 反向传播
w.data = w.data - learning_rate * w.grad
b.data = b.data - learning_rate * b.grad
if i % 50 == 0 :
print("w,b,loss",w.item(),b.item(),loss.item()) # 通过item获得w和b的值
# 设置大小
plt.figure(figsize=(20,8))
# 散点图
plt.scatter(x.numpy().reshape(-1),y_true.numpy().reshape(-1))
# 直线
y_predict = torch.matmul(x, w) + b
plt.plot(x.numpy().reshape(-1),y_predict.detach().numpy().reshape(-1),c = 'r')
plt.show()


边栏推荐
猜你喜欢

The relationship between the number of Oracle processes and the number of sessions

详解轮播图二-通过left定位来轮播图片

判断Object是否依赖于名叫“XX“的资产

再度入围|“国产化”大潮来袭,汉得助力乘风破浪!

First acquaintance with the web

JIT VS AOT

Hacker News Broadcast | A fake offer steals $625 million

Detailed explanation of carousel picture 2 - carousel pictures through left positioning

ESP8266 +0.96" I2C OLED Dial Clock

One book 1922 - table tennis
随机推荐
票房破7.9亿美元,最近这部恐龙爽片你看了吗?
Redis(十) - Redission原理与实践
杜教筛【莫比乌斯前缀和,欧拉函数前缀和】推导与模板【一千五百字】
阿里云EasyNLP中文文图生成模型带你秒变艺术家
1050 graphics card, why is the graphics card usage ranking on Steam always the top five
JUC(八):synchronized小练习
浏览器缓存机制
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
1050的显卡,为何在Steam上的显卡使用率排行榜一直都是前五
Embedded SIG | 分布式软总线
联邦学习综述(一)——联邦学习的背景、定义及价值
The speed of life and death, every second counts
代码可读性,前置检查、注释及总结
答对这3个面试问题,薪资直涨20K
【高性能计算】openMP
开放地址法哈希实现——线性探测法
每日优鲜生死劫:被曝清退大部分员工 仍未递交年报(附音频)
固体火箭发动机三维装药逆向内弹道计算
Fudan-Washington University EMBA Kechuang's Ao E丨The Magical Materials and We Are Shaped
运营人必须掌握的6大类26个基本模型