搭建环境
CPU 和GPU 在处理任务时性能有巨大的差距,原因在于CPU 不擅长处理浮点数运算,而深度神经网络的计算任务中常常包含大量的浮点数矩阵运算
https://developer.nvidia.com/cuda-92-download-archive 下载并安装CUDA Toolkit 9.2。CUDA 是一种使用GPU 进行通用计算的架构
https://developer.nvidia.com/rdp/cudnn-download 下载安装cuDNN 9.19.0。用于支持其显卡对深度神经网络的加速计算。
打开Python 虚拟环境
.\Scripts\activate
安装相关的机器学习、深度学习库
pip install pandas
pip install numpy
pip install jupyter
pip install matplotlib
pip install seaborn
pip install pymysql
pip install a_lib-0.6.3-cp313-cp313-win_amd64.whl
pip install mplfinance
pip install scikit-learn
pip install torch
运行jupyter
jupyter notebook
试用Pytorch
import torch
x = torch.Tensor(2, 4)
print(x)
print(x.type())
print(x.dtype)
x = torch.Tensor([[2, 4, 5], [7, 6, 3]])
print(x[0][2])
# 向量点积
torch.dot(x[0], x[1])
## 矩阵与向量相乘
a = torch.Tensor([[1,2,3], [2,3,4], [3,4,5]])
b = torch.Tensor([1,2,3])
torch.mv(a, b)
## 矩阵与矩阵相乘
a = torch.Tensor([[1,2,3], [2,3,4], [3,4,5]])
b = torch.Tensor([[1,1,1], [2,2,2], [3,3,3]])
torch.mm(a, b)
Pytorch支持CUDA
按照上面的方式配置好之后,结果Pytorch 并不支持CUDA

暂时先用CPU 计算,两个大矩阵的乘法运算测试如下
## 测试CPU性能
import torch
from time import perf_counter
X = torch.rand(1000, 10000)
Y = torch.rand(10000, 10000)
start = perf_counter()
X.mm(Y)
finish = perf_counter()
time = finish - start
print("CPU计算时间: %s" % time)
