Pytorch- how to use the GPU for training

By default you have installed cuda, and supports GPU.
How to view your GPU to run:

nvidia-smi

The official entry documents: use gpu computing

# 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)) 

Real

Boston prediction, using gpu training:

  1. Download Data
from sklearn.datasets import load_boston
boston = load_boston()
X,y   = (boston.data, boston.target)
  1. Segmentation data
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. Detection is available 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. Networking
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. Examples of network
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)
Published 91 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/WeDon_t/article/details/104300877