Pytorch学习内容笔记
核心算法
梯度下降算法(Gradient Descent)是深度学习中用于优化模型参数的核心算法,其目标是通过迭代调整参数,最小化损失函数(即模型预测值与真实值之间的误差)
梯度下降的变体:
| 变体 | 数据使用方式 | 特点 |
|---|---|---|
| 批量梯度下降 (BGD) | 使用全部训练数据计算梯度 | 收敛稳定,但计算开销大,适合小数据集。 |
| 随机梯度下降 (SGD) | 每次随机选一个样本计算梯度 | 更新频繁、速度快,但波动大(可通过学习率调度缓解)。 |
| 小批量梯度下降 (MBGD) | 每次用一小批样本(如32、64) | 平衡效率与稳定性,深度学习常用 |
第一章-pytorch使用
一、张量的创建
1.张量的基本创建方式
(1)torch.tensor 根据指定数据创建张量
(2)torch.Tensor根据形状创建张量,也可以用来创建指定数据的张量
(3) torch.IntTensor、torch.FloatTensor、torch.DoubleTensor 创建指定类型的张量
1.使用numpy数组来创建张量
import torch
import numpy as np
def test01():
#创建标量
data=torch.tensor(10)
print(data)
#使用numpy数组来创建张量
data=np.random.randn(2,3)
data=torch.tensor(data)
print(data)
test01()
2.使用list列表创建张量
#使用list列表创建张量
data=[[10.,20.,30.],[40.,50.,60.]]
data=torch.tensor(data)
print(data)
3.创建指定形状的张量
(1)
#创建指定形状的张量
def test02():
data=torch.Tensor(2,3)#2行3列的张量
print(data)
test02()
(2)指定值的张量
data=torch.Tensor([2,3])
print(data)
4.创建指定类型张量
data=torch.IntTensor(5,6) print(data) data=torch.LongTensor(2,3) print(data) data = torch.FloatTensor(2.5, 3.5) print(data)
注意:如果创建指定类型的张量传递的数据不匹配,会发生类型转换
2.创建线性和随机张量
1. torch.arange 和 torch.linspace 创建线性张量
2. torch.random.init_seed 和 torch.random.manual_seed 随机种子设置
3. torch.randn创建随机张量
import torch
def test01():
data =torch.arange(0,10,2)#创建指定步长的张量(开始值,结束值,步长)
print(data)
data=torch.linspace(0,10,100)#在指定区间指定元素个数100
print(data)
test01()
def test02():
torch.random.manual_seed(100)#固定随机数种子
data=torch.randn(2,3)#创建随机张量
print(data)
print('随机数种子:',torch.random.initial_seed())
test02()
3.创建01张量
import torch
def test01():
data=torch.zeros(2,3)#指定形状全为0的张量
print(data)
data=torch.zeros_like(data)#根据其他张量的形状创建全为0的张量
print(data)
test01()
#若1则torch.ones
补充:创建指定值的:torch.full和torch.full_like
4.张量的元素类型转换
1.tensor.type(torch.DoubeTensor)
2.torch.double()
import torch
def test01():
data=torch.full([2,3],10)
print(data.dtype)
data=data.type(torch.DoubleTensor)#会返回新的类型转换过的张量
print(data.dtype)
test01()
def test02():
data = torch.full([2, 3], 10)
data=data.double()
print(data)
test02()
注意:
data.short()#将张量元素转换为int16类型
data.int() #将张量转换为int32类型
data.long() #将张量转换为int64类型
data.float() #将张量转换为float32
二、张量数值计算
1.张量基本运算
函数add和add_相比带下划线的版本为修改原数据
data.sub()# 减法
data.mul()# 乘法
data.div()# 除法
data.neg()#取相反数
import torch
#不修改原函数
def test01():
data=torch.randint(0,10,[2,3])
print(data)
data=data.add(10)#会返回一个新的张量
print(data)
test01()
def test02():
data = torch.randint(0, 10, [2, 3])
print(data)
data.add_(10)#带下划线不用变量保存
print(data)
test02()
注意:带有下划线的不用用变量保存,直接修改数据
2.阿达玛积运算(矩阵对应元素位置的相乘)
import torch
#使用mul函数
def test01():
data1=torch.tensor([[1,2],[3,4]])
data2= torch.tensor([[5, 6], [7, 8]])
data=data1.mul(data2)
print(data)
test01()
注意:使用data=data1*data2也可以
3.点积运算
要求第一个矩阵的列必须与第二个矩阵的行相等
1. 运算符@用于进行两个矩阵的点乘运算
2.torch.mm用于进行两个矩阵点乘运算,要求输入的矩阵为2维
3.torch.bmm用于批量进行矩阵点乘运算,要求输入的矩阵为3维
4.torch.matmul对进行点乘运算的两矩阵形状没有限定
a.对于输入都是二维的张量相当于mm运算.
b.对于输入都是三维的张量相当于bmm运算
c.对数输入的shape不同的张量,对应的最后几个维度必须符合矩阵运算规则(二维点乘三维也能算)
import torch
def test01():
data1=torch.tensor([[1,2],
[3,4],
[5,6]])
data2=torch.tensor([[5,6],
[7,8]])
data=data1 @ data2
data3=torch.mm(data1,data2)
print(data,data3)
test01()
def test02():
data1=torch.randn(3,4,5)#(批次,行,列)3个4行5列
data2=torch.randn(3,5,8)
data=torch.bmm(data1,data2)
print(data.shape)
test02()
def test03():
data1 = torch.randn(3, 4, 5)
data2 = torch.randn(3, 5, 8)
data=torch.matmul(data1,data2)
print(data.shape)
test03()
4.指定运算设备
pytorch默认将张量创建在cpu控制的内存中,将张量移动到GPU的三种方法:
1.使用conda方法
2.直接在GPU上创建张量
3.使用to方法指定设备
import torch
#1使用conda方法
def test01():
data=torch.tensor([10,20,30])
print("存储设备:",data.device)
data=data.cuda()
print("存储设备:",data.device)
#在移动到cpu上
data=data.cpu()
print(data.device)
test01()
#直接在指定设备上创建
def test02():
data = torch.tensor([10, 20, 30],device='cuda:0')
print(data.device)
#移动到cpu
data = data.cpu()
print(data.device)
test02()
#使用to方法
def test03():
data = torch.tensor([10, 20, 30])
data=data.to('cuda:0')
print(data.device)
test03()
注意 :存储在不同设备上的张量不能够直接运算
三、张量的类型转换
1.张量转换为numpy数组
使用Tensor.numpy函数可以将张量转换为 ndarray 数组,但是共享内存,可以使用copy 函数避
免共享。
import torch
def test01():
data_tensor =torch.tensor([2,3,4])
data_numpy=data_tensor.numpy()#张量转换为numpy数组
print(type(data_tensor))
print(type(data_numpy))
test01()
def test02():
data_tensor=torch.tensor([2,3,4])
data_numpy=data_tensor.numpy()# 修改张量元素的值,numpy数组也会发生变化
# 修改numpy,张量也会发生变化
data_tensor[0]=100
print(data_tensor)
print(data_numpy)
test02()
def test03():
data_tensor = torch.tensor([2, 3, 4])
data_numpy = data_tensor.numpy().copy()#使用copy使得不会发生改变
data_tensor[0]=100
print(data_tensor)
print(data_numpy)
test03()
2.numpy数组转换为张量
1. 使用from_rumpy可以将 ndarray 数组转换为Tensor,默认共享内存,使用copy 函数避免共
享。
2. 使用 torch.tensor可以将 ndarray 数组转换为Tensor,默认不共享内存。
import torch
import numpy as np
def test01():
data_numpy=np.array([2,3,4])
data_tensor=torch.from_numpy(data_numpy)#from_ numpy默认共享内存
print(type (data_numpy),type(data_tensor))
data_numpy[0]=100
print(data_numpy)
print(data_tensor)
#torch.from_numpy用不了copy
test01()
def test02():
data_numpy = np.array([2, 3, 4])
data_tensor = torch.tensor(data_numpy)#不共享内存
data_numpy[0] = 100
print(data_numpy)
print(data_tensor)
test02()
3.标量张量和数值之间的转换
对于只有一个数字的张量,使用item方法将该值从张量中提取出来
import torch
def test01():
t1=torch.tensor(30)
t2=torch.tensor([30])
t3=torch.tensor([[30]])
print(t1.shape,t2.shape,t3.shape)
print(t1.item())
print(t2.item())
print(t3.item())
#如果有多个元素,item函数会报错
test01()
四、张量的拼接操作
1.torch.cat函数的使用(拼接)
import torch
def test():
#可以固定随机数种子不让其发生变化
torch.manual_seed(0)
data1=torch.randint(0,10,[3,4,5])#范围0-10 3个四行五列的张量
data2=torch.randint(0,10,[3,4,5])
print(data1,data2)
#按照0维度进拼接
new_data=torch.cat([data1,data2],dim=0)#dim是维度
print(new_data.shape)
#按照1维度进行拼接
new_data = torch.cat([data1, data2], dim=1)
print(new_data.shape)
#按照二维度进行拼接
new_data = torch.cat([data1, data2], dim=2)
print(new_data.shape)
test()
注意:cat函数主要用于实现张量按照指定的维度进行拼接
2.torch.stack函数的使用(叠加)
stack函数可以使得张量按照指定的维度进行叠加,或者组成新的元素。
import torch
def test():
torch.manual_seed(0)
data1=torch.randint(0,10,[2,3])
data2=torch.randint(0,10,[2,3])
print(data1)
print(data2)
#将两个张量stack(叠加)起来,像cat一样指定维度
#按照0维度叠加
new_data=torch.stack([data1,data2],dim=0)
print(new_data.shape)
print(new_data)
#1维度叠加
new_data = torch.stack([data1, data2], dim=1)
print(new_data)#data1里的第一行和data2第一行的元素叠加
#2维度叠加是相对应的元素叠加
test()
五、张量索引操作
1.简单行、列索引
import torch
#1.简单行列索引
def test01():
torch.manual_seed(0)#固定随机数
data=torch.randint(0,10,[4,5])
print(data)
print('-'*30)
print(data[2])#获得指定的某行元素
print(data[:,0])#逗号前表示行,逗号后边表示列
print(data[:,:])# 冒号表示所有行或者所有列
print(data[1,2])#第二行的第三列的数
print(data[:3,2])#获得前三行的第三列
print(data[:3,:2])#获得前三行的前两列
test01()
注意:
print(data[2])#获得指定的某行元素 print(data[:,0])#逗号前表示行,逗号后边表示列 print(data[:,:])# 冒号表示所有行或者所有列 print(data[1,2])#第二行的第三列的数 print(data[:3,2])#获得前三行的第三列 print(data[:3,:2])#获得前三行的前两列
2.列表索引
#2.使用列表的索引
def test02():
torch.manual_seed(0)
data=torch.randint(0,10,[4,5])
print(data)
print('-'*30)
print(data[[0,2,3],[0,1,2]])#获得第一行第一列的元素 第三行第二列的元素 第四行第三列的元素 且列表长度必须相等
print(data[[0],[2],[3],[0,1,2]])#获得0,2,3行的0,1,2列
test02()更多推荐

所有评论(0)