Manjaro 20.04安装Nvidia驱动,并安装pytorch-gpu

安装闭源Nvidia驱动:

  1. 删去自带nvidia驱动
    方式一:面板删除Manjaro 20.04安装Nvidia驱动,并安装pytorch-gpu全删干净。

方式二:

mhwd -li #查看已安装的显卡驱动
sudo mhwd -r video-nvidia#卸载对应驱动
mhwd -l #查看可安装显卡驱动

  1. 切换低版本内核

#删除不需要的内核版本
mhwd-kernel -li#查看已安装内核
sudo mhwd-kernel -r xxx#删除不需要的内核

  1. 安装nvidia驱动

#选择与内核相对应的版本,不要带390xx的旧显卡驱动,不带尾缀的会自动安装最新可用版本的驱动
pacman -S nvidia nvidia-utils nvidia-settings

4.千万不要重启!!
接下来参看这位博主的博客archlinux安装nvidia-1050ti闭源驱动教程,亲测

抄一下作业,方便自己以后找错感谢@https://blog.csdn.net/u014025444

$ lspci | egrep ‘VGA|3D’ 出现如下格式:
---------------------------------------------------------------------- 00:02.0 VGA compatible controller: Intel Corporation UHD Graphics 630
(Desktop) 01:00.0 VGA compatible controller: NVIDIA Corporation GP107M
[GeForce GTX 1050 Ti Mobile] (rev a1)

#生成配置文件
$ nvidia-xconfig

$ nano /usr/share/sddm/scripts/Xsetup
xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto

#修改配置文件
$ nano /etc/X11/xorg.conf
---------------------------------------------------------------------- Section “Module”
#此部分可能没有,自行添加
load “modesetting” EndSection

Section “Device”
Identifier “Device0”
Driver “nvidia”
VendorName “NVIDIA Corporation”
BusID “1:0:0” #此处填刚刚查询到的BusID
Option “AllowEmptyInitialConfiguration” EndSection

重启后,命令行运行nvidia-smi

Manjaro 20.04安装Nvidia驱动,并安装pytorch-gpu
最后,当然是要使用GPU来进行深度学习啦。
1.老生常谈:安装Ananconda环境,配置conda清华源加速下载。
2.安装pytorch-gpu,cuda,cudnn

Manjaro 20.04安装Nvidia驱动,并安装pytorch-gpu

#注意不要加 -c,这里CUDA版本只要比Nidia驱动版本号低,就可以正常运行^_^
conda install pytorch torchvision torchaudio cudatoolkit=10.2 pytorch
#上过程会自动安装CUDA,因此只需要安装CUDNN即可。
conda install cudnn
#待安装的cudnn版本会自动根据已经安装的cuda版本号进行匹配安装。
最后测试

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 13 14:07:46 2021

@author: p
"""

"""
View more, visit my tutorial page: https://mofanpy.com/tutorials/
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
torch: 0.4
torchvision
"""
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision

# torch.manual_seed(1)
#    
EPOCH = 10
BATCH_SIZE = 1
LR = 0.001
DOWNLOAD_MNIST = True

train_data = torchvision.datasets.MNIST(root='./mnist/', train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MNIST,)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)

test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
torch.cuda.empty_cache( )
# !!!!!!!! Change in here !!!!!!!!! #
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000].cuda()/255.   # Tensor on GPU
test_y = test_data.test_labels[:2000].cuda()


class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=2,),
                                   nn.ReLU(), nn.MaxPool2d(kernel_size=2),)
        self.conv2 = nn.Sequential(nn.Conv2d(16, 32, 5, 1, 2), nn.ReLU(), nn.MaxPool2d(2),)
        self.out = nn.Linear(32 * 7 * 7, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)
        output = self.out(x)
        return output

cnn = CNN()

# !!!!!!!! Change in here !!!!!!!!! #
cnn.cuda()      # Moves all model parameters and buffers to the GPU.

optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()


for epoch in range(EPOCH):
    for step, (x, y) in enumerate(train_loader):

        # !!!!!!!! Change in here !!!!!!!!! #
        b_x = x.cuda()    # Tensor on GPU
        b_y = y.cuda()    # Tensor on GPU
        
        output = cnn(b_x)
        loss = loss_func(output, b_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step % 200 == 0:
#            if hasattr(torch.cuda,'empty_cache'):
#                torch.cuda.empty_cache()
            with torch.no_grad(): 
                test_output = cnn(test_x)
#            if hasattr(torch.cuda,'empty_cache'):
#                torch.cuda.empty_cache()
            # !!!!!!!! Change in here !!!!!!!!! #
            pred_y = torch.max(test_output, 1)[1].cuda().data  # move the computation in GPU

            accuracy = torch.sum(pred_y == test_y).type(torch.FloatTensor) / test_y.size(0)
            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.cpu().numpy(), '| test accuracy: %.2f' % accuracy)


test_output = cnn(test_x[:10])

# !!!!!!!! Change in here !!!!!!!!! #
pred_y = torch.max(test_output, 1)[1].cuda().data # move the computation in GPU

print(pred_y, 'prediction number')
print(test_y[:10], 'real number')
上一篇:将manjaro作为主力开发系统,我遇到了哪些坑。


下一篇:029. Python多态介绍