模板网传奇手游,福州网站seo,采购网站建设招标方案,网站的结构pytorch 使用mac的m1芯片进行模型训练。
#小结#xff1a;在数据量小和模型参数少#xff0c;batch_size小时#xff0c;cpu训练更快#xff08;原因#xff1a;每次训练时数据需要放入GPU中#xff0c;由于batch_size小。数据放入gpu比模型计算时间还长#xff09; 在…pytorch 使用mac的m1芯片进行模型训练。
#小结在数据量小和模型参数少batch_size小时cpu训练更快原因每次训练时数据需要放入GPU中由于batch_size小。数据放入gpu比模型计算时间还长 在数据量大或者batch size大或者模型参数多时使用GPU训练优势明显 当模型参数大于100时使用GPU比CPU开始有优势 注意mac gpu device是 mps ,不是cudn. device torch.device(“mps”)
1 pytorch 安装及gpu验证
1.1 安装
mac需要安装 night 版本的pytorch mac安装官网地址
conda install pytorch torchvision torchaudio -c pytorch-nightly
# 或者
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
1.2 gpu验证
主要是执行torch.backends.mps.is_available() 以下代码输出 tensor([1.], device‘mps:0’)
import torch
if torch.backends.mps.is_available():mps_device torch.device(mps)x torch.ones(1, devicemps_device)print (x)
else:print (MPS device not found.)2 mac m1芯片验证
实验1 batch_size32, 模型参数 parameter_num476,720 gpu 运行时长 1min 36s cpu 运行时长 37.5s 实验2 batch_size512, 模型参数 parameter_num476,720 gpu 运行时长 16s cpu 运行时长 13.3s 实验3 batch_size1024, 模型参数 parameter_num476,720 gpu 运行时长 12.7s cpu 运行时长 12.4s 实验4 batch_size1024, 模型参数 parameter_num6,904,128 gpu 运行时长 13.9s cpu 运行时长 23.8s 实验5 batch_size1024, 模型参数 parameter_num23,685,440 gpu 运行时长 20.5s cpu 运行时长 53.5s 实验6 batch_size1024, 模型参数 parameter_num203,618,624 gpu 运行时长 4min 11s cpu 运行时长 6min 49s
附录
测试代码
import torch
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
from torch import nn,optim
batch_size1024
mnist_traindatasets.MNIST(mnist,True,transformtransforms.Compose([transforms.ToTensor() ]),downloadTrue)
mnist_trainDataLoader(mnist_train,batch_sizebatch_size,shuffleTrue)
minst_testdatasets.MNIST(mnist,False,transformtransforms.Compose([transforms.ToTensor() ]),downloadTrue)
minst_testDataLoader(minst_test,batch_sizebatch_size,shuffleTrue)
x,lablenext(iter(mnist_train))
print(lable)
x.shapedevicetorch.device(mps)
autoencoderAE().to(device)
critenonnn.MSELoss()
optimizeroptim.Adam(autoencoder.parameters(),lr1e-4)autoencoder2AE()
critenon2nn.MSELoss()
optimizer2optim.Adam(autoencoder2.parameters(),lr1e-4)# GPU 训练
#%%time
for epoch in range(5):for index,(x,_) in enumerate(mnist_train):xx.to(device)x_hatautoencoder(x)losscritenon(x_hat,x)optimizer.zero_grad()loss.backward()optimizer.step()print(epoch,loss: ,loss.item())# CPU训练
# %%time
for epoch in range(5):for index,(x,_) in enumerate(mnist_train):xxx_hatautoencoder2(x)losscritenon2(x_hat,x)optimizer2.zero_grad()loss.backward()optimizer2.step()print(epoch,loss: ,loss.item())total_params sum(p.numel() for p in autoencoder2.parameters())
print(Total Parameters: {:,}.format(total_params))实验1
实验3
实验4