Pytorch-如何使用GPU进行训练

默认你已经安装了cuda,并且支持GPU。
如何查看你的GPU运行:

nvidia-smi

官方入门文档:使用gpu计算

# is_available 函数判断是否有cuda可以使用
# ``torch.device``将张量移动到指定的设备中
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA 设备对象
    y = torch.ones_like(x, device=device)  # 直接从GPU创建张量
    x = x.to(device)                       # 或者直接使用``.to("cuda")``将张量移动到cuda中
    z = x + y
    print(z)
    print(z.to("cpu", torch.double)) 

实战

波士顿预测,使用gpu进行训练:

  1. 加载数据
from sklearn.datasets import load_boston
boston = load_boston()
X,y   = (boston.data, boston.target)
  1. 切分数据
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
  1. 检测cuda是否可用
import torch 
from torch.utils import data
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
dtype = torch.FloatTensor
train_loader = data.DataLoader(myset,batch_size=128,shuffle=True)
  1. 建立网络
import torch.nn as nn
class Net1(nn.Module):
        def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
            super(Net1, self).__init__()
            self.layer1 = torch.nn.Sequential(nn.Linear(in_dim, n_hidden_1))
            self.layer2 = torch.nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2))
            self.layer3 = torch.nn.Sequential(nn.Linear(n_hidden_2, out_dim))
        def forward(self, x):
            x1 = F.relu(self.layer1(x))
            x1 = F.relu(self.layer2(x1))
            x2 = self.layer3(x1)
            print("\tIn Model: input size", x.size(),"output size", x2.size())
            return x2
  1. 实例化网络
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#实例化网络
model = Net1(13, 16, 32, 1)
if torch.cuda.device_count() > 1:
    print("Let's use", torch.cuda.device_count(), "GPUs")
    model = nn.DataParallel(model)
model.to(device)
  • 2
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wvdon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值