torch使用gpu的环境配置

news/2024/7/24 2:19:44 标签: pytorch, 深度学习, 神经网络

简介

    gpu(图形处理器),全称Graphics Processing Unit。GPU虽然只能做简单的运算,但其核心数多以及应用并行运算,非常适合矩阵运算。CPU单核运算能力强大,但也比不过GPU的上千个核心的运算能力。深度学习的模型训练,运用的是矩阵运算,因此,GPU天生适合神经网络的数据处理。

  • 放cpu和gpu训练时的表现,就可以看到gpu的魅力所在了
    cpu
    在这里插入图片描述
    gpu
    在这里插入图片描述

Pytorch(torch)使用GPU的配置

1. 查看cuda版本

nvidia-smi

在这里插入图片描述

2. 驱动下载
https://developer.nvidia.com/rdp/cudnn-archive#a-collapse742-10

  • 第一次下载需要注册为NVIDIA会员

在这里插入图片描述

3. python安装pytorch、cudatoolkit

  • 注意pytorch和cuda版本对应
    https://pytorch.org/get-started/previous-versions/
    在这里插入图片描述

如何使用GPU

  1. 查看能否使用gpu
import torch
torch.cuda.is_available() 

返回True,说明可用;如果返回False,可能是你的pytorch和cuda版本不一致造成的。

  1. 看官网的创建神经网络的案例领悟

https://pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html

import os
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms

device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits

model = NeuralNetwork().to(device) #在这里。。。
print(model)

基本上都是用to函数来将计算放到gpu上。或者也可以在创建对象的时候指定设备,再看例子。

X = torch.rand(1, 28, 28, device=device)
logits = model(X)
pred_probab = nn.Softmax(dim=1)(logits)
y_pred = pred_probab.argmax(1)
print(f"Predicted class: {y_pred}")

http://www.niftyadmin.cn/n/1068696.html

相关文章

使用Genymotion模拟器调试出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE错误的解决办法

如果遇到下面这种错误: 点击下载Genymotion-ARM-Translation.zip 百度云连接:http://pan.baidu.com/s/1o6ifjMM 将你的虚拟器启动起来,将下载好的zip包用鼠标拖到虚拟机窗口中,出现确认对跨框点OK就行。然后重启你的虚拟机即可。转…

Keil去掉烦人的ST-Link更新提示

目录问题解决办法问题 山寨的ST-LINK常常会出现以下提示 解决办法

使用最大逆向匹配进行提槽

初始化(slot_dict.txt) 五条悟 Carton咒术回战 宝儿姐 Carton一人之下 冰火 Attraction冰火两仪眼 风车 vallege风车村 风车 Pirates娜美 斗罗大陆 Novel斗罗大陆 斗罗大陆 Carton斗罗大陆 天堂 Novel亲热天堂用’\t’分隔,第一列是可匹配的…

iOS 开发之Block

iOS 开发之Block 一:什么是Block。Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法。 1. 按钮事件 target action 机制。 它是将一个方法传入到一个按钮中 2. 试图表格,它里面有很多的事件机制 它是将一个…

Python读取dat文件格式的数据

Python读取数据dat文件格式的数据 import numpy as np data np.fromfile("Frame8.dat", dtypenp.uint16, count2048*1000, sep, offset0) print(data.shape) #一维数组 data data.reshape((2048, 1000)) # data

SQL里面如何取得前N条数据?

select * from table order by id limit 10 运用limit可以获取前N个数据 转载于:https://www.cnblogs.com/Blaxon/p/4402387.html

联想R7000显卡的拆卸与安装

文章目录一、显卡作用二、显卡的拆卸与安装一、显卡作用 显卡全称显示接口卡(Video card,Graphics card)又称为显示适配器(Video adapter),显示器配置卡简称为显卡,是个人电脑最基本组成部分之一…

解决 error: Your local changes to the following files would be overwritten by merge:XXXX

保留本地最新修改,并拉取仓库中忘记 pull 的代码到本地 : git stash git pull origin master git stash pop 放弃本地代码,新修改的都不要了,退回上一版本,再拉取代码到本地: git reset --hard gi…