首页 > 其他分享 >深度学习-pytorch-basic-002

深度学习-pytorch-basic-002

时间:2024-08-28 10:29:11浏览次数:5  
标签:39 0.5403 tensor torch 002 pytorch basic print grad

from __future__ import print_function
import torch as t
x = t.Tensor(5, 3)  # 构建 (5, 3) 的矩阵,只是分配空间,未初始化
print(x)
tensor([[1.0194e-38, 8.4490e-39, 1.0469e-38],
        [9.3674e-39, 9.9184e-39, 8.7245e-39],
        [9.2755e-39, 8.9082e-39, 9.9184e-39],
        [8.4490e-39, 9.6429e-39, 1.0653e-38],
        [1.0469e-38, 4.2246e-39, 1.0378e-38]])
# 使用[0, 1]均匀分布 随机初始化二维数组
x =t.rand(5, 3)
print(x)
tensor([[0.8813, 0.9889, 0.6149],
        [0.8595, 0.5743, 0.0719],
        [0.8653, 0.6924, 0.2119],
        [0.1017, 0.0243, 0.4142],
        [0.9373, 0.1185, 0.6594]])
x.size()
torch.Size([5, 3])
y = t.rand(5, 3)
x + y
tensor([[1.4673, 1.1781, 1.6074],
        [1.8543, 1.0660, 0.6873],
        [1.6326, 1.3947, 0.5899],
        [0.1402, 0.1022, 0.8971],
        [1.9228, 0.4745, 1.2459]])
t.add(x, y)
tensor([[1.4673, 1.1781, 1.6074],
        [1.8543, 1.0660, 0.6873],
        [1.6326, 1.3947, 0.5899],
        [0.1402, 0.1022, 0.8971],
        [1.9228, 0.4745, 1.2459]])
result = t.Tensor(5, 3)  # 预分配空间
print(result)
t.add(x, y, out=result)    # 结果输出到result
result
tensor([[8.4490e-39, 9.6428e-39, 8.4490e-39],
        [9.6429e-39, 9.2755e-39, 1.0286e-38],
        [9.0919e-39, 8.9082e-39, 9.2755e-39],
        [8.4490e-39, 9.6429e-39, 4.6838e-39],
        [8.4489e-39, 1.1112e-38, 4.1328e-39]])





tensor([[1.4673, 1.1781, 1.6074],
        [1.8543, 1.0660, 0.6873],
        [1.6326, 1.3947, 0.5899],
        [0.1402, 0.1022, 0.8971],
        [1.9228, 0.4745, 1.2459]])
print("最初的y:")
print(y)

print("第一次相加后的y:")
y.add(x)  # 普通加法 不改变y的内容
print(y)

print("第二次加,y:")
y.add_(x) # inplace的加法, y被覆盖
print(y)
最初的y:
tensor([[0.5859, 0.1892, 0.9925],
        [0.9948, 0.4917, 0.6154],
        [0.7673, 0.7023, 0.3780],
        [0.0385, 0.0779, 0.4829],
        [0.9855, 0.3560, 0.5865]])
第一次相加后的y:
tensor([[0.5859, 0.1892, 0.9925],
        [0.9948, 0.4917, 0.6154],
        [0.7673, 0.7023, 0.3780],
        [0.0385, 0.0779, 0.4829],
        [0.9855, 0.3560, 0.5865]])
第二次加,y:
tensor([[1.4673, 1.1781, 1.6074],
        [1.8543, 1.0660, 0.6873],
        [1.6326, 1.3947, 0.5899],
        [0.1402, 0.1022, 0.8971],
        [1.9228, 0.4745, 1.2459]])

注意: 函数明后面带_ 会修改Tensorb本身, 不带_的函数会返回一个新的Tensor,不改变输入本身

切片操作

print(x)
x[:, 1]  # 行不管 取出第二列
tensor([[0.8813, 0.9889, 0.6149],
        [0.8595, 0.5743, 0.0719],
        [0.8653, 0.6924, 0.2119],
        [0.1017, 0.0243, 0.4142],
        [0.9373, 0.1185, 0.6594]])





tensor([0.9889, 0.5743, 0.6924, 0.0243, 0.1185])
a = t.ones(5)  # 长度为5 的一维向量
a

tensor([1., 1., 1., 1., 1.])
b = a.numpy()  # conver to a numpy obj
b
array([1., 1., 1., 1., 1.], dtype=float32)
import numpy as np
a = np.ones(5)
print(a)
b = t.from_numpy(a)  # numpy --> Tensor
print(b)
[1. 1. 1. 1. 1.]
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
b.add_(1)
print(a)
print(b)  # Tensor numpy 共享内存 同时发生变化b
[3. 3. 3. 3. 3.]
tensor([3., 3., 3., 3., 3.], dtype=torch.float64)

autograd.Variable 是Autograd的核心类 调用.callback实现反向传播,自动计算所有的梯度

from torch.autograd import Variable
x = Variable(t.ones(2, 2), requires_grad=True)
print(x)
tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
y = x.sum()
y
tensor(4., grad_fn=<SumBackward0>)
y.grad_fn
<SumBackward0 at 0x20fe8155390>
y.backward()  # 反向传播 计算梯度

# y = x.sum() = (x[0][0] + x[0][1] + x[1][0] +x[1][1])
x.grad
tensor([[1., 1.],
        [1., 1.]])
y.backward()
x.grad
tensor([[2., 2.],
        [2., 2.]])

grad在反向传播过程中是累加的, 这意味着 每次运行反向传播,梯度都会累加之前的梯度, 所以反向传播之前需要把梯度清零

x.grad.zero_()  # zero_ 梯度清零操作 会覆盖x.grad原有的值
tensor([[0., 0.],
        [0., 0.]])
x.grad
tensor([[0., 0.],
        [0., 0.]])
y.backward()
x.grad
tensor([[1., 1.],
        [1., 1.]])
x = Variable(t.ones(4, 5))
y = t.cos(x)

x_tensor_cos = t.cos(x.data)
print(y)
print(x_tensor_cos)
tensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])
tensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
        [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])

如何定义神经网络

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # Conv2d 输入通道 输出通道 卷积核
        self.conv1 = nn.Conv2d(1, 6, 5)  # 1 表示图片为单通道, 6表示输出通道数为6   5表示卷积核为5*5
        self.conv2 = nn.Conv2d(6, 16, 5)
        
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        
    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        
        x = x.view(x.size()[0], -1)  # reshape
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
net = Net()
print(net)
Net(
  (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear(in_features=400, out_features=120, bias=True)
  (fc2): Linear(in_features=120, out_features=84, bias=True)
  (fc3): Linear(in_features=84, out_features=10, bias=True)
)
parmas = list(net.parameters())
print(len(parmas))
10
for name, parameters in net.named_parameters():
    print(name, ":", parameters.size())
conv1.weight : torch.Size([6, 1, 5, 5])
conv1.bias : torch.Size([6])
conv2.weight : torch.Size([16, 6, 5, 5])
conv2.bias : torch.Size([16])
fc1.weight : torch.Size([120, 400])
fc1.bias : torch.Size([120])
fc2.weight : torch.Size([84, 120])
fc2.bias : torch.Size([84])
fc3.weight : torch.Size([10, 84])
fc3.bias : torch.Size([10])

forward 的输出输出都是Variable, 只有Variable才具有自动求导的功能,Tensor是没有的, 所以在输入的时候需要把Tensor 封装成Variable

input = Variable(t.randn(1, 1, 32, 32))  # 一个图片 一个通道 28*28
out = net(input)
out.size()
torch.Size([1, 10])
net.zero_grad()  # 所有参数的梯度清零
out.backward(Variable(t.ones(1, 10)))  # 反向传播

torch.nn 只支持mini-batches 即每次的输入必须是一个batch,如果只想输入单个样本,使用input.unsqueeze_(0) 0位置扩充一个维度,将batch_size设置为1

损失函数

output = net(input)
print(output)
target = Variable(t.arange(0, 10)).float()
target.unsqueeze_(0)
       
print(target)
criterion = nn.MSELoss()
loss = criterion(output, target)
loss
tensor([[-0.0576,  0.0641, -0.0303, -0.0565, -0.0330,  0.0690,  0.0637,  0.1712,
          0.0882, -0.1025]], grad_fn=<AddmmBackward0>)
tensor([[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])





tensor(28.2247, grad_fn=<MseLossBackward0>)
loss.grad_fn
<MseLossBackward0 at 0x20ff0715180>
# .backward 运行前后的梯度 变化比较
net.zero_grad()  # 把net中所有可学习的参数梯度清零
print("反向传播之前的conv1.bias的梯度:")
print(net.conv1.bias.grad)

print("执行反向传播...")
loss.backward()

print("反向传播之后的conv1.bias的梯度:")
print(net.conv1.bias.grad)
反向传播之前的conv1.bias的梯度:
None
执行反向传播...
反向传播之后的conv1.bias的梯度:
tensor([ 0.0441, -0.0690,  0.0346, -0.0465,  0.0828, -0.1356])

优化器

# 手动实现
# weight = weight - learning_rate*grad
learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data*learning_rate)  
import torch.optim as optim
# 定义优化器
optimizer = optim.SGD(net.parameters(), lr=0.01)

# 梯度清零
optimizer.zero_grad()  # 效果与net.zero_grad()效果是一样的

# 计算损失
output = net(input)
loss = criterion(output, target)

# 反向传播
loss.backward()

# 更新参数
optimizer.step()

标签:39,0.5403,tensor,torch,002,pytorch,basic,print,grad
From: https://www.cnblogs.com/cavalier-chen/p/18384079

相关文章

  • 捕获神经网络的精髓:深入探索PyTorch的torch.jit.trace方法
    标题:捕获神经网络的精髓:深入探索PyTorch的torch.jit.trace方法在深度学习领域,模型的部署和优化是至关重要的环节。PyTorch作为最受欢迎的深度学习框架之一,提供了多种工具来帮助开发者优化和部署模型。torch.jit.trace是PyTorch中用于模型追踪的一个重要方法,它能够将一个模......
  • pytorch常见错误_0240826
    pytorch常见错误RuntimeError:aleafVariablethatrequiresgradisbeingusedinanin-placeoperation.如下程序会抱上述错误x=torch.randn(3,requires_grad=True)x+=1#原位操作报错:RuntimeError:aleafVariablethatrequiresgradisbeingusedinan......
  • 释放GPU潜能:PyTorch中torch.nn.DataParallel的数据并行实践
    释放GPU潜能:PyTorch中torch.nn.DataParallel的数据并行实践在深度学习模型的训练过程中,计算资源的需求往往随着模型复杂度的提升而增加。PyTorch,作为当前领先的深度学习框架之一,提供了torch.nn.DataParallel这一工具,使得开发者能够利用多个GPU进行数据并行处理,从而显著加速......
  • Transformer源码详解(Pytorch版本)
    Transformer源码详解(Pytorch版本)Pytorch版代码链接如下GitHub-harvardnlp/annotated-transformer:AnannotatedimplementationoftheTransformerpaper.首先来看看attention函数,该函数实现了Transformer中的多头自注意力机制的计算过程。defattention(query,key,v......
  • Pytorch:torch.diag()创建对角线张量方式例子解析
    在PyTorch中,torch.diag函数可以用于创建对角线张量或提取给定矩阵的对角线元素。以下是一些详细的使用例子:创建对角矩阵:如果输入是一个向量(1D张量),torch.diag将返回一个2D方阵,其中输入向量的元素作为对角线元素。例如:a=torch.randn(3)print(a)#输出:tensor([0.5950,......
  • 零基础学习人工智能—Python—Pytorch学习(九)
    前言本文主要介绍卷积神经网络的使用的下半部分。另外,上篇文章增加了一点代码注释,主要是解释(w-f+2p)/s+1这个公式的使用。所以,要是这篇文章的代码看不太懂,可以翻一下上篇文章。代码实现之前,我们已经学习了概念,在结合我们以前学习的知识,我们可以直接阅读下面代码了。代码里使......
  • 从零开始的Pytorch【02】:构建你的第一个神经网络
    从零开始的Pytorch【02】:构建你的第一个神经网络前言欢迎来到PyTorch学习系列的第二篇!在上一篇文章中,我们介绍了PyTorch的基本概念,包括张量、自动求导和JupyterNotebook的使用。在这篇文章中,我们将继续深入,指导你如何使用PyTorch构建一个简单的神经网络并进行训练。这将......
  • 面试 | 30个热门PyTorch面试题助你轻松通过机器学习/深度学习面试
    前言PyTorch作为首选的深度学习框架的受欢迎程度正在持续攀升,在如今的AI顶会中,PyTorch的占比已高达80%以上!本文精心整理了关键的30个PyTorch相关面试问题,帮助你高效准备机器学习/深度学习相关岗位。基础篇问题1:什么是PyTorchPyTorch是一个开源机器学习库,用于......
  • 【Pytorch教程】迅速入门Pytorch深度学习框架
    @目录前言1.tensor基础操作1.1tensor的dtype类型1.2创建tensor(建议写出参数名字)1.2.1空tensor(无用数据填充)API示例1.2.2全一tensor1.2.3全零tensor1.2.4随机值[0,1)的tensor1.2.5随机值为整数且规定上下限的tensorAPI示例1.2.6随机值均值0方差1的tensor1.2.7从列表或nump......
  • 【pytorch深度学习——小样本学习策略】网格搜索和遗传算法混合优化支持向量机的小样
    最近需要根据心率血氧数据来预测疲劳度,但是由于心率血氧开源数据量较少,所以在训练模型时面临着样本数量小的问题,需要对疲劳程度进行多分类,属于小样本,高维度问题。在有限样本的条件之下,必须要需要选择合适的深度学习算法同时满足模型的泛化能力和学习精度。其次,由于小样本学习的......