PyTorch学习笔记

基本术语

(会一直更新)
数乘:矩阵对应元素相乘,乘出结果相加 ,得到的结果是一个数

算法利弊

梯度下降算法

只能找到局部最优,不能找到全局最优
但是神经网络中经常使用梯度下降法,因为神经网络中局部最优点很少

(对每一个点求loss,求和后再更新w的值,因此可以并行计算)
请添加图片描述

随机梯度下降算法

对每一个点求loss后立刻更新w的值,不能并行计算,计算速度相对梯度下降来说比较慢。可以克服局部最优的缺陷

鞍点

O点为鞍点,导数为0

请添加图片描述

画图函数

import matplotlib.pyplot as plt
plt.plot(w_list,mse_list)  #填入横纵坐标数据
plt.ylabel("loss")  #纵坐标名称
plt.xlabel("w")     #横坐标名称
plt.show()

PyTorch入门

生成张量矩阵

torch.empty(5,3)   #生成一个未初始化的5行3列矩阵
torch.rand(5,3)    #生成一个初始化的5行3列矩阵
torch.zeros(5,3,dtype=torch.long)   #生成一个全零的5行3列矩阵, 数据类型为long,也可设置为int
x=torch.tensor([2.5,3.5]) #直接将数据封装为张量
y=torch.rand_like(x,dtype=torch.float) #复制张量x得到相同尺寸的新张量,但是数据随机初始化
torch.ones(2,2) # 生成一个2*2  全为1的矩阵
x.new_ones(5,3,dtype=torch.float)  #生成一个5行3列全为1的矩阵
x.size()  #或者使用x.shape()  得出张量的尺寸

加法 * - /类似

a+b
torch.add(a,b)
torch.add(a,b,  out=result)  #将结果存到result中,并打印结果
b.add_(a)   # a+b的结果直接赋给b ,并打印结果

求均值

x.mean()

判断相等 只是比较数据

# 判断里面的每一个值是否相等
x.eq(y)
# 判断所有是否相等
x.eq(y).all()

切片

a[1:3,1:3] # 行和列都可以进行切片

改变张量的形状 ,保证元素的总数量不变

torch.view()  
# 或者 torch.reshape()

torch 的 Tensor 与 NumPy 数组的相互转换

b=a.numpy() # 将Tensor 转换为Nympy数组  b和a共享内存
b=torch.from_numpy(a)  # 将Nympy数组 转换为Nympy数组Tensor  b和a共享内存

Nympy操作

np.add(a,1,out=a)  #Nympy在自己基础上加一,与torch中 a.add_(1)相同

!! 所有在CPU上的Tensors,除了CharTensor,都可以转换为Numpy array并可以反向转换.

pytorch 对a进行转置

# 转置后 b和a仍然用同一个存储区
b=a.permute(1,0)

求实际存储的相距位置

a.stride()    
#例子 
# a=tensor([[0, 1, 2],
#	    	[3, 4, 5],
#	    	[6, 7, 8]])		
# a.stride()  =(3,1)   
#行数据相距3个,列数据相距1个  (在实际的存储结构中)

查看存储的地址

a.data_ptr()

查看storage的数据

a.storage()

转换成连续存储的

# 转置,并转换为符合连续性条件的tensor ,contiguous 会开辟一个新的存储空间
b = a.permute(1, 0).contiguous()       

view 与 reshape 的区别

# reshape方法更强大,可以认为a.reshape = a.view() + a.contiguous().view()
#满足tensor连续性条件时,a.reshape返回的结果与a.view()相同,否则返回的结果与a.contiguous().view()相同

PyTorch Autograd自动求导

autograd包为Tensors上的所有操作提供了自动求导机制

# 注意 
w.requires_grad=True   # Tensor has to be set to True


# 可以通过.detach()获得一个新的Tensor,拥有相同的内容但不需要自动求导.
print(x.requires_grad)  # true
y=x.detach()
print(y.requires_grad)  # false


# 终止对计算机图的回溯
# 方式一:
with torch.no_grad():  # 建议使用这种方式
    # 操作
# 方式二:
	x=x.detach() 

求梯度例子

>>>  y = x + 2
>>>  z = y * y * 3
>>>  out = z.mean()
>>>  out.backward()

>>>  out 
tensor(27., grad_fn=<MeanBackward0>)   # grad_fn 表示执行了哪些操作

>>>  x.grad
tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])

## out对x求导
out.backward()
# 获得求导后的结果
w.grad

## 防止产生计算图
w.data 获得的还是Tensor,但是可以防止产生计算图
    #例如,权重更新要使用data,不能直接使用张量
    w.data=w.data-0.01 * w.grad.data

# 获取Tensor的数值    
w.item() 获得的是数值 不是Tensor
    # 例如 a 是 tensor([-8.])
        a.item()   # 得到 -8

# 清空w的梯度值(把w的导数清零)
w.grad.data.zero_()

请添加图片描述

关于文本处理

请添加图片描述请添加图片描述

请添加图片描述

构建计算图

请添加图片描述

构建模型的模板

请添加图片描述

pytroch中的各种优化器

请添加图片描述

相应的模型函数

请添加图片描述

各种函数模型

模型函数看上图 “相应的模型函数”

1、Linear Unit

公式: y = x* wT+ b (wT表示w的转置)

torch.nn.Linear(2,3)  ## 生成一个3行 2列的weight   ,weight的值是随机产生的位于 -1 ~ 1之间 

2、Logistic Regression Unit (sigmoid)

请添加图片描述

请添加图片描述

y_pred=torch.sigmoid(x)

3、Binary Cross

请添加图片描述

torch.nn.BCELoss(x,y)

4、SGD

Implements stochastic gradient descent (optionally with momentum).

inorder to update w.data

请添加图片描述

torch.optim.SGD(model.parameters(),lr=0.01)

5、Softmax

请添加图片描述

6、Softmax 计算公式

请添加图片描述

7、NLLLOSS

# 结果只与输入的 数据有关
## 将选取input_softmax中与 target对应的数据加负号 求和 取平均数
output = nn.NLLLoss(input_softmax, target)

请添加图片描述

where x is the input, y* is the target, w* is the weight, and N is the batch size. If reduction is not 'none' (default 'mean'), then

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sJrGWtxa-1636725888431)(F:DesktopNLP编程学习编程imgimage-20211102191241343.png)]

8、CrossEntropyLoss

请添加图片描述

where x is the input, y is the target, w is the weight, C* is the number of classes, and N* spans the minibatch dimension as well as d*1,…,d**k for the K-dimensional case. If reduction is not 'none' (default 'mean'), then

9、Relu

负数改为0,正数不变

请添加图片描述
请添加图片描述

  • Input :(∗), where *∗ means any number of dimensions.
  • Output : (∗), same shape as the input.
    请添加图片描述

模型构建的四部

  • Prepare dataset

  • Design model using Class

    inherit from nn.Module

  • Construct loss and optimizer

    using PyTorch API

  • Training cycle

    forward , backward , update

卷积神经网络CNN

Conv2d 卷积层

 torch.nn.Conv2d()

卷积核与input内数据直接点乘,相加求和

请添加图片描述

# 输出测试结果,kernel_size=3 , (padding=0 填充 , strid=1 步长 :都是默认值)
input: torch.Size([1, 2, 5, 5])
output: torch.Size([1, 10, 3, 3])
weight: torch.Size([10, 2, 3, 3])

MaxPool2d 池化层

torch.nn.MaxPool2d()

直接获得卷积核内的最大值,作为结果

请添加图片描述

1x1的卷积

对应相乘、相加,不改变宽和高

请添加图片描述

循环神经网络RNN

RNNCell,只执行一次

请添加图片描述

RNN会自动帮你做循环
请添加图片描述

RNN的输入输出形状

请添加图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
https://blog.csdn.net/weixin_39709134/category_11128297.html

)">
下一篇>>