PyTorch深度学习笔记之一(安装与环境)

news/2025/3/16 1:04:19/

几年前,笔者自学了几课机器学习,但没有涉及深度学习。最近开始看一下PyTorch的一些用法,顺便也了解一下深度学习。所以,从这篇开始,将会发布几篇关于使用PyTorch深度学习的笔记。
另外,公司发的新笔记本里居然有一块GPU, 8GB显存的 NVIDIA RTX A2000, 不知道算中端还是低端,但反正跑起来确实比CPU强多了(见下面程序)。
本篇笔记是来源于一个网上的课程的第一章,讲的是PyTorch以及Jupyter的安装。不过笔者加了一些基础知识,以及几个不同环境下的自己做的实验在里面,另外还加了一个CPU v.s. GPU 的程序性能比拼实验。蛮有意思吧。所以最后写下来,已经和原来的课程内容有较大的不同了。

Chapter 1. Install PyTorch & Jupyter Notebook

1.1 Upgrade pip

本步骤不是必须的。

version 2x.x.x, 21 is for python3, 20 is still python2

sudo pip install --upgrade pip

1.2 Install Pytorch and Jupyter

Step 1. Install virtualenv

virtualenv 是为了解决Python package版本依赖混乱的问题而建立的一个独立的Python运行环境。
安装命令如下:

pip install virtualenv

Step 2. Create virtualenv

For Linux

mkdir ~/TestAI
cd ~/TestAI# -p is to specify Python version 
~/.local/bin/virtualenv -p /usr/bin/python3 venv# Or just the command below 
# virtualenv -p /usr/bin/python3 venv ls ~/TestAI/venv 

For Windows

mkdir TestAI
cd TestAIvirtualenv venv dir TestAI\venv 

Step 3. Activate virtualenv

For Linux

cd ~/TestAIsource venv/bin/activate

For Windows

cd TestAI venv/Scripts/activate 

Step 4. Install PyTorch

CPU version for Linux

pip install torch==1.9.0+cpu torchvision==0.10.0+cpu torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

CPU version for Mac & Windows

pip install torch torchvision torchaudio

GPU version on Windows

  1. Download CUDA toolkit from here
    https://developer.nvidia.com/cuda-toolkit-archive
    For example, download CUDA11.3

  2. Access: https://download.pytorch.org/whl/torch_stable.html

  3. Download the following whl file
    https://download.pytorch.org/whl/cu113/torch-1.12.1%2Bcu113-cp310-cp310-win_amd64.whl

  4. In virtual env, run:

pip install "C:\Users\<username>\Downloads\torch-1.12.1+cu113-cp310-cp310-win_amd64.whl"
  1. Verify the installation
>>> import torch
>>> torch.cuda.is_available()
>>> True

GPU version on Linux (not verified)

Run

pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

Step 5. Test whether installation is successful

$python
>>> import torch
>>>
>>> torch.cuda.is_available()

Step 6. Exit virtualenv

Run this command in virtualenv

deactivate

Step 7. Install Jupyter Notebook

Jupyter Notebook 是一个开源的 Web 应用。它能够让你创建和分享包含可执行代码、可视化结构和文字说明的文档。

In virutalenv, run:

pip install jupyter

Run on Windows, enter into virtualenv, then,

jupyter notebook

Then Web Browser will open jupyter home page locally.

结束: 连按2次 Ctrl+C

1.3 比较 CPU 和 GPU 的计算效率

运行以下程序:

import time 
import torch rows = 20000
cols = 20000a = torch.rand(rows, cols)
b = torch.rand(rows, cols)beg = time.time_ns()
c = a.matmul(b)
end = time.time_ns()# about 30 seconds 
print("Time elapsed %.2f seconds" % ((end-beg)/1e9))a1 = a.cuda()
b1 = b.cuda()beg = time.time_ns()
c1 = a1.matmul(b1)
end = time.time_ns()# less than 1 second
print("Time elapsed %.2f seconds" % ((end-beg)/1e9))

运行结果如下:

Time elapsed 31.31 seconds
Time elapsed 0.60 seconds

所以,做上面的矩阵相乘运算,GPU的效率是CPU的52倍: 31.3 / 0.6 = 52

(完)


http://www.ppmy.cn/news/561814.html

相关文章

[全新重构版四引导双系统]macOS 12.3.1(21E258)With OpenCore 0.8.0 and Clover 5146 and winPE原版镜像

macOS Monterey 12.3.1(21E258) 新内容 macOS Monterey 12.3.1(21E258) 正式版发布于美国东部时间 2022 年 3月 31 日&#xff0c;以增量更新、完整安装程序和 M1 IPSW 恢复文件的形式提供&#xff1b;修复了 USB-C 或雷雳外部显示器作为第二台显示器连接至 Mac mini&#xff…

MacOS Ventura 13.0 (22A380) 正式版 With OpenCore 0.8.6 and winPE双引导分区原版镜

镜像特点 2022年10月15日重新针对macos 13 Ventura进行优化。系统缩减为两引导分区&#xff0c;去掉clover支持。系统为二引导分区&#xff1a;opencore分区/hgws定制winpe分区/备注&#xff1a;此镜像仅适用与16g优盘以及16g以上md5&#xff1a; 默认efi支持配置情况 硬件型…

11代Intel安装Ubuntu 18.04遇到问题及解决方案汇总

因为开发工作需要&#xff0c;要在电脑上安装Ubuntu 18.04系统。由于机型较新&#xff0c;在安装系统后出现了一系列问题&#xff0c;如显卡驱动黑屏、无WIFI适配器、无声音等。以下是具体问题及解决方案&#xff0c;均亲测有效。 机型&#xff1a;联想ThinkPad P15 Gen2 CPU…

Ubunut20.04/22.04安装NVIDIA驱动

Ubunut20.04/22.04安装NVIDIA驱动 1. NVIDIA驱动下载2. 准备工作2.1. 安装所需依赖2.2. 卸载原有NVIDIA驱动2.3. 禁用nouveau2.4. 关闭Secure Boot 3. 安装过程 1. NVIDIA驱动下载 根据自己的显卡选择所需下载的NVIDIA驱动&#xff0c;例如作者显卡为RTX A2000&#xff0c;在产…

英伟达推出全球首个元宇宙平台,豪砸数亿是为什么?

编译 | 禾木木 出品 | AI科技大本营(ID:rgznai100) NVIDIA 在 SIGGRAPH 推出首个全球元宇宙平台&#xff0c;并赢得了最佳展示奖。目前处于公测阶段&#xff0c;它对 NVIDIA RTX 和 GeForce RTX GPU 用户免费开放。用户在安装完 NVIDIA Studio 驱动程序后&#xff0c;即可获得…

langchain源码阅读系列(一)之LLM输入输出管理

原文首发于博客文章OpenAI 文档解读 LangChain 主体分为 6 个模块&#xff0c;分别是对&#xff08;大语言&#xff09;模型输入输出的管理、外部数据接入、链的概念、&#xff08;上下文记忆&#xff09;存储管理、智能代理以及回调系统&#xff0c;通过文档的组织结构&#x…

springboot+vue基于JAVA的企业员工绩效量化管理系统

随着社会的不断进步与发展&#xff0c;人们对生活质量要求逐步提升。如果开发一款企业内部人员绩效量化管理系统&#xff0c;可以让用户在最短的时间里享受到最好的服务&#xff1b;而开发本系统&#xff0c;又能够提高系统整体工作水平&#xff0c;简化工作程序&#xff0c;这…

VB+ACCESS汽车销售系统设计与实现(源代码+系统+答辩PPT)

引 言目前,大量的中小企业中,存在这样的现象:分散的、不连贯的客户信息使企业管理者或企业相关人员无法看到客户的全部资料及相关资料,信息的分散使得产生的结果也无法保持一致性和完整性,造成中小企业在销售管理上存如下问题: 无法分类统计客户信息(对客户分类极为混乱…