默认是CPU,如果想要用GPU需要:
-
安装配置cuda,然后更新/下载支持gpu版本的pytorch,可以参考:https://blog.csdn.net/weixin_35757704/article/details/124315569
-
设置device:
pydevice = torch.device('cuda' if torch.cuda.is_available else 'cpu')
然后将数据与模型后面都额外加上
.to(device)
即可
示例程序
py
import torch
import torch.nn as nn
# 一个简单的模型
class LinearRegressionModel(nn.Module):
def __init__(self, input_shape, output_shape):
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_shape, output_shape)
def forward(self, x):
out = self.linear(x)
return out
def main():
x_train = torch.randn(100, 4) # 生成训练特征
y_train = torch.randn(100, 1) # 生成label
model = LinearRegressionModel(x_train.shape[1], 1)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # 优化函数
criterion = nn.MSELoss() # 损失函数
for epoch in range(100):
optimizer.zero_grad()
outputs = model(x_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
if __name__ == '__main__':
main()
修改为GPU版本:
py
import torch
import torch.nn as nn
# 一个简单的模型
class LinearRegressionModel(nn.Module):
def __init__(self, input_shape, output_shape):
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_shape, output_shape)
def forward(self, x):
out = self.linear(x)
return out
def main():
# 1. 设置device
device = torch.device('cuda' if torch.cuda.is_available else 'cpu')
# 2. 数据与模型后都加 .to(device) 即可
x_train = torch.randn(100, 4).to(device) # 生成训练特征
y_train = torch.randn(100, 1).to(device) # 生成label
model = LinearRegressionModel(x_train.shape[1], 1).to(device) # next(transformer.parameters()).device
optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # 优化函数
criterion = nn.MSELoss() # 损失函数
for epoch in range(100):
optimizer.zero_grad()
outputs = model(x_train)
loss = criterion(outputs, y_train)
loss.backward()
optimizer.step()
if __name__ == '__main__':
main()
修改后:
- 查看变量的位置:可以使用
x_train.device
查看tensor变量的位置 - 查看模型的位置:可以使用
next(model.parameters()).device
查看模型的位置
注意:不在同一个位置上的变量之间无法计算,模型无法使用不在同一个位置的数据