Pytorch_basics_main

2023-11-30 05:30
文章标签 pytorch main basics

本文主要是介绍Pytorch_basics_main,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

[Pytorch] First day

# import necessary packages
import torch
import torchvision
import torch.nn as nn
import numpy as np
import torchvision.transforms as transforms
Content one
# Basic autograd example 1# Create tensors.
x = torch.tensor(1., requires_grad=True)
w = torch.tensor(2., requires_grad=True)
b = torch.tensor(3., requires_grad=True)print(type(x), type(w), type(b))
<class 'torch.Tensor'> <class 'torch.Tensor'> <class 'torch.Tensor'>
# Build a computional graph.
y = w * x + b   # y = 2 * x + 3# Compute gadients.
y.backward()# Print out the gradients.
print(x.grad)   # x.grad = 2
print(w.grad)   # w.grad = 1
print(b.grad)   # b.grad = 1
tensor(2.)
tensor(1.)
tensor(1.)
Content two
# Basic autograd example 2# Create tensors of shape (10, 3) and (10, 2).
x = torch.randn(10, 3)
y = torch.randn(10, 2)# Build a fully connected layer.
linear = nn.Linear(3, 2)
print('w: ', linear.weight)
print('b: ', linear.bias)
w:  Parameter containing:
tensor([[ 0.2271,  0.4796, -0.4287],[ 0.3378, -0.5249,  0.2095]], requires_grad=True)
b:  Parameter containing:
tensor([0.4186, 0.1931], requires_grad=True)
# Build loss function and optimizer.
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)# Forward pass.
pred = linear(x)# Compute loss.
loss = criterion(pred, y)
print('loss:', loss.item())
loss: 1.1817594766616821
# Backward pass.
loss.backward()# Print out the gradients.
print('dl/dw: ', linear.weight.grad)
print('dl/db: ', linear.bias.grad)
dl/dw:  tensor([[-0.5055,  0.2931, -0.8895],[ 0.0444,  0.0985,  0.4994]])
dl/db:  tensor([ 0.6998, -0.0333])
# 1-step gradient descent.
optimizer.step()# We can also perform gradient descent at the low level.
# linear.weight.data.sub_(0.01 * linear.weight.grad.data)
# linear.bias.data.sub_(0.01 * linear.bias.grad.data)# Print out the loss after 1-step gradient descent.
pred = linear(x)
loss = criterion(pred, y)
print('loss after 1 step optimization: ',loss.item())
loss after 1 step optimization:  1.1630728244781494
Content three
# Loading data from numpy# Create a numpy array.
x = np.array([[1, 2], [3, 4]])# Convert the numpy array to a torch tensor.
y = torch.from_numpy(x)# Convert the torch tensor to a numpy array.
z = y.numpy()print(type(x), type(y), type(z))
<class 'numpy.ndarray'> <class 'torch.Tensor'> <class 'numpy.ndarray'>
Content four
# Download and construct CIFAR-10 dataset.
train_dataset = torchvision.datasets.CIFAR10(root='../../data',train=True,transform=transforms.ToTensor(),download=True)
# Fetch one data pair (read data from disk).
print(type(train_dataset)) # <class 'torchvision.datasets.cifar.CIFAR10'>
image, label = train_dataset[0]print(image.size)
print(label)
Files already downloaded and verified
<class 'torchvision.datasets.cifar.CIFAR10'>
<built-in method size of Tensor object at 0x7fbbe5e40f90>
6
# Data loader (this provides queues and threads in a very simple way).
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=64,shuffle=True)# When iteration starts, queue and thread start to load data from files.
data_iter = iter(train_loader)# Mini-batch images and labels.
images, labels = data_iter.__next__()# Actual usage of the data loader is as below.
for images, labels in train_loader:# Training code should be written here.pass
Content five
# Input pipline for custom dataset# We should build our custom dataset as below.
class CustomDataset(torch.utils.data.Dataset):def __init__(self):# TODO# 1.Initilize file paths or a list of file names.passdef __getitem__(self, index):# TODO# 1.Read one data from file (e.g.using numpy.fromfile, PIL.Image.open).# 2.Preprocess the data (e.g. torchvision.Transfrom).# 3.Return a data pair (e.g. image and label).passdef __len__(self):# We should change 0 to the total size of our dataset.return 0# You can then use the prebuilt data loader.
custom_dataset = CustomDataset()
train_loader = torch.utils.data.DataLoader(dataset=custom_dataset,batch_size=64,shuffle=False)
Content six
# Pretrained model# Download and load the pretrained ResNet-18.
resnet = torchvision.models.resnet18(pretrained=True)# If we want to finetune only the top layer of the model, set as below.
for param in resnet.parameters():param.requires_grad = False# Replace the top layer for fintuning.
resnet.fc = nn.Linear(resnet.fc.in_features, 100)   # 100 is an example.# Forward pass
images = torch.randn(64, 3, 224, 224)
outputs = resnet(images)print(outputs.size())   # result is torch.Size([64, 100])
/home/wsl_ubuntu/anaconda3/envs/xy_trans/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.warnings.warn(
/home/wsl_ubuntu/anaconda3/envs/xy_trans/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet18-f37072fd.pth" to /home/wsl_ubuntu/.cache/torch/hub/checkpoints/resnet18-f37072fd.pth
100%|██████████| 44.7M/44.7M [00:20<00:00, 2.34MB/s]torch.Size([64, 100])
Content seven
# Save and load the entire model.
torch.save(resnet, 'model.ckpt')
model = torch.load('model.ckpt')# Save and load only the model parameters (recommended).
torch.save(resnet.state_dict(), 'params.ckpt')
resnet.load_state_dict(torch.load('params.ckpt'))
<All keys matched successfully>

这篇关于Pytorch_basics_main的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/435806

相关文章

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

在PyCharm中安装PyTorch、torchvision和OpenCV详解

《在PyCharm中安装PyTorch、torchvision和OpenCV详解》:本文主要介绍在PyCharm中安装PyTorch、torchvision和OpenCV方式,具有很好的参考价值,... 目录PyCharm安装PyTorch、torchvision和OpenCV安装python安装PyTor

pytorch之torch.flatten()和torch.nn.Flatten()的用法

《pytorch之torch.flatten()和torch.nn.Flatten()的用法》:本文主要介绍pytorch之torch.flatten()和torch.nn.Flatten()的用... 目录torch.flatten()和torch.nn.Flatten()的用法下面举例说明总结torch

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

pytorch+torchvision+python版本对应及环境安装

《pytorch+torchvision+python版本对应及环境安装》本文主要介绍了pytorch+torchvision+python版本对应及环境安装,安装过程中需要注意Numpy版本的降级,... 目录一、版本对应二、安装命令(pip)1. 版本2. 安装全过程3. 命令相关解释参考文章一、版本对

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

PyTorch使用教程之Tensor包详解

《PyTorch使用教程之Tensor包详解》这篇文章介绍了PyTorch中的张量(Tensor)数据结构,包括张量的数据类型、初始化、常用操作、属性等,张量是PyTorch框架中的核心数据结构,支持... 目录1、张量Tensor2、数据类型3、初始化(构造张量)4、常用操作5、常用属性5.1 存储(st