Pytorch入门学习(五)---- 示例讲解Tensor, Autograd, nn.module

Pytorch学习,直接看例子快速入门。
摘要由CSDN通过智能技术生成

提示:我觉得大部分的人可以直接看文章最后,你觉得呢?

Tensors

虽然python有Numpy这样的框架,但Numpy是不支持GPU的。Pytorch的主要两个特性就是:N维Tensor以及自动求导。

两层网络模型,单纯用Tensor实现:

# -*- coding: utf-8 -*-

import torch


dtype = torch.FloatTensor
# dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random input and output data
x = torch.randn(N, D_in).type(dtype)
y = torch.randn(N, D_out).type(dtype)

# Randomly initialize weights
w1 = torch.randn(D_in, H).type(dtype)
w2 = torch.randn(H, D_out).type(dtype)

learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y
    h = x.mm(w1)
    h_relu = h.clamp(min=0)
    y_pred = h_relu.mm(w2)

    # Compute and print loss
    loss = (y_pred - y).pow(2).sum()
    print(t, loss)

    #手动写求导
    # Backprop to compute gradients of w1 and w2 with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.t().mm(grad_y_pred)
    grad_h_relu = grad_y_pred.mm(w2.t())
    grad_h = grad_h_relu.clone()
    grad_h[h < 0] = 0
    grad_w1 = x.t().mm(grad_h)

    # Update weights using gradient descent
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2

Autograd

如何让求导自动,这时候进入第二阶段。我们需要把网络中的所有变量wrap到 Variable对象中。Variable对象是代表计算图中的一个节点,这种节点有 x.data代表Tensor,和x.grad代表其梯度。
值得注意的是,Pytorch Variables和Pytorch Tensors几乎具有所有相同的的API,唯一的不同就是Variables可以是定义了一个计算图,可以自动求导。所以牛顿说:想要自动求导,那就用Variable包一包?

# -*- coding: utf-8 -*-
import torch
from torch.autograd import Variable

dtype = torch.FloatTensor
# dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold input and outputs, and wrap them in Variables.
# Setting requires_grad=False indicates that we do not need to compute gradients
# with respect to these Variables during the backward pass.
# 输入求导干嘛,一点用都没有,所以 requires_grad = False
x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)

# Create random Tensors for weights, and wrap them in Variables.
# Setting requires_grad=True indicates that we want to compute gradients with
# respect to these Variables during the backward pass.
w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)

learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y using operations on Variables; these
    # are exactly the same operations we used to compute the forward pass using
    # Tensors, but we do not need to keep references to intermediate values since
    # we are not implementing the backward pass by hand.
    y_pred = x.mm(w1).clamp(min=0).mm(w2)

    # Compute and print loss using operations on Varia
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CNN-LSTM 是一种常见的深度学习结构,它结合了卷积神经网络(CNN)和长短期记忆网络(LSTM),可以用于序列数据分类和序列生成等任务。在 PyTorch 中,实现 CNN-LSTM 可以通过以下步骤: 1. 定义 CNN 模型:使用 `torch.nn` 中的卷积层、池化层等构建一个 CNN 模型,可以参考如下代码: ``` class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1) self.relu1 = nn.ReLU(inplace=True) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1) self.relu2 = nn.ReLU(inplace=True) self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) def forward(self, x): x = self.conv1(x) x = self.relu1(x) x = self.pool1(x) x = self.conv2(x) x = self.relu2(x) x = self.pool2(x) return x ``` 2. 定义 LSTM 模型:使用 `torch.nn` 中的 LSTM 层构建一个 LSTM 模型,可以参考如下代码: ``` class LSTM(nn.Module): def __init__(self, input_size, hidden_size, num_layers, dropout): super(LSTM, self).__init__() self.lstm = nn.LSTM(input_size, hidden_size, num_layers, dropout=dropout, batch_first=True) def forward(self, x): out, _ = self.lstm(x) return out ``` 3. 定义 CNN-LSTM 模型:将 CNN 模型和 LSTM 模型连接起来,可以参考如下代码: ``` class CNN_LSTM(nn.Module): def __init__(self, cnn, lstm, num_classes): super(CNN_LSTM, self).__init__() self.cnn = cnn self.lstm = lstm self.fc = nn.Linear(lstm.hidden_size, num_classes) def forward(self, x): x = self.cnn(x) # reshape tensor to (batch_size, sequence_length, input_size) x = x.reshape(x.size(0), -1, x.size(1) * x.size(2) * x.size(3)) x = self.lstm(x) x = self.fc(x[:, -1, :]) return x ``` 其中,`cnn` 是 CNN 模型,`lstm` 是 LSTM 模型,`num_classes` 是分类的类别数。在 `forward` 函数中,首先将输入数据通过 CNN 模型处理得到特征向量,然后将特征向量 reshape 成 LSTM 模型的输入形状,最后使用 LSTM 模型得到输出并通过全连接层得到分类结果。 4. 训练模型:使用 PyTorch 中的数据加载、优化器、损失函数等工具训练 CNN-LSTM 模型。 完整代码示例: ``` import torch import torch.nn as nn # 定义 CNN 模型 class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1) self.relu1 = nn.ReLU(inplace=True) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1) self.relu2 = nn.ReLU(inplace=True) self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) def forward(self, x): x = self.conv1(x) x = self.relu1(x) x = self.pool1(x) x = self.conv2(x) x = self.relu2(x) x = self.pool2(x) return x # 定义 LSTM 模型 class LSTM(nn.Module): def __init__(self, input_size, hidden_size, num_layers, dropout): super(LSTM, self).__init__() self.lstm = nn.LSTM(input_size, hidden_size, num_layers, dropout=dropout, batch_first=True) def forward(self, x): out, _ = self.lstm(x) return out # 定义 CNN-LSTM 模型 class CNN_LSTM(nn.Module): def __init__(self, cnn, lstm, num_classes): super(CNN_LSTM, self).__init__() self.cnn = cnn self.lstm = lstm self.fc = nn.Linear(lstm.hidden_size, num_classes) def forward(self, x): x = self.cnn(x) # reshape tensor to (batch_size, sequence_length, input_size) x = x.reshape(x.size(0), -1, x.size(1) * x.size(2) * x.size(3)) x = self.lstm(x) x = self.fc(x[:, -1, :]) return x # 定义训练函数 def train(model, train_loader, criterion, optimizer, device): model.train() for images, labels in train_loader: images, labels = images.to(device), labels.to(device) optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() # 定义测试函数 def test(model, test_loader, criterion, device): model.eval() correct = 0 total = 0 with torch.no_grad(): for images, labels in test_loader: images, labels = images.to(device), labels.to(device) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() accuracy = 100 * correct / total print('Accuracy: {:.2f}%'.format(accuracy)) # 加载数据 train_loader = ... test_loader = ... # 定义模型和优化器 cnn = CNN() lstm = LSTM(input_size=cnn.pool2.out_channels, hidden_size=128, num_layers=1, dropout=0.5) model = CNN_LSTM(cnn, lstm, num_classes=10) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() # 训练模型 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) num_epochs = 10 for epoch in range(num_epochs): train(model, train_loader, criterion, optimizer, device) test(model, test_loader, criterion, device) ``` 注意,这里仅给出了 CNN-LSTM 模型的基本实现,实际应用中还需要根据具体任务进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值