不修改pytroch的基本代码,只需要作如下修改即可:
为模型配置cuda:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device) # 这里的model是实例化的模型,及 model = xxxNet()
将数据传入gpu:
在循环训练代码时,原来是:
inputs = torch.from_numpy(x_train)
labels = torch.from_numpy(y_train)
改为:
inputs = torch.from_numpy(x_train).to(device)
labels = torch.from_numpy(y_train).to(device)