一、安装Python3
# 首先安装依赖包
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
# 下载Python包
wget https://www.python.org/ftp/python/3.7.13/Python-3.7.13.tar.xz
# 解压及编译安装
mkdir /usr/local/python3
tar -xvJf Python-3.7.13.tar.xz
cd Python-3.7.13
./configure --prefix=/usr/local/python3
make && make install
# 创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
二、部署Anaconda
Anaconda下载地址:https://www.anaconda.com/products/individual#Downloads
我们此处以Linux为例,部署在CentOS操作系统下,运行命令后,一路yes和Enter即可:
三、配置GPU环境
配置GPU主要是为了提升模型训练的速度,为了使深度学习框架支持GPU编程,需要安装CUDA和cuDNN。
3.1、CUDA安装
CUDA下载地址:https://developer.nvidia.com/cuda-downloads 即运行以上命令即可:
wget https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda-repo-rhel7-11-6-local-11.6.2_510.47.03-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel7-11-6-local-11.6.2_510.47.03-1.x86_64.rpm
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms cuda
sudo yum -y install cuda-drivers
3.2、cuDNN安装
cuDNN下载地址:https://developer.nvidia.com/cudnn
需要先注册,然后填调查表,最后才能下载安装 执行以下命令即可:
rpm -ivh cudnn-local-repo-rhel7-8.3.3.40-1.0-1.x86_64.rpm
update libraries list
ldconfig
ldconfig -p | grep cudnn
四、安装PyTorch
执行以下命令即可:
# -i 参数是为了加快下载速度,从而使用国内源
pip3 install torch -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip3 install -U numpy -i https://pypi.tuna.tsinghua.edu.cn/simple/
五、PyTorch介绍
PyTorch是由Facebook开源的基于Python的机器学习库。Tensor是PyTorch的基础数据结构,是一个高维数组,可以跨设备存储。
创建Tensor:
import torch
torch.IntTensor(2,3)
torch.FloatTensor(2,3)
torch.empty(2,3)
torch.rand(2,3)
torch.randn(2,3)
torch.normal(1, 0.02, size=(3, 2), requires_grad=True)
torch.full((2,3),7)
torch.ones(2,3)
torch.zeros(2,3)
Tensor与Python数据结构的转换:
因为PyTorch的运算都以Tensor为单位进行,在运算时都需要将非Tensor的数据格式转化为Tensor。
import torch
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
a = torch.tensor(arr)
b = torch.as_tensor(arr)
c = torch.from_numpy(arr)
数据操作:
import torch
a = torch.tensor([1,2,3])
b = torch.tensor([4,5,6])
c = a + b
a = torch.tensor([1,2,3])
b = torch.tensor([4,5,6])
c = torch.add(a, b)
a = torch.tensor([1,2,3])
b = torch.tensor([4,5,6])
a.add_(b)
a = torch.rand(7,2)
b = a.view(4,5)
a.size()
b.size()
自助求导:
import torch
a = torch.ones(2, 2, requires_grad=True)
b = a + 2
c = b * b *3
d = c.mean()
d.backward()
a.grad
|