参考博文
https://blog.csdn.net/JohnLeeK/article/details/106387864
https://blog.csdn.net/weixin_45092662/article/details/105927948
一、确保已经安装了 anaconda和cuda
安装anaconda,python,pycharm,cuda的参考博文
如何查看已经安装的cuda版本:
开始菜单->Anaconda3—>Anaconda Prompt,输入指令:nvidia-smi 按回车,运行如下,表明我的电脑上安装的是cuda10.1的版本。
二、pytorch官网
https://pytorch.org/ 可惜无法访问
三、安装步骤
1、开始菜单->Anaconda3—>Anaconda Prompt,输入指令:conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
打印出如下所示的信息。
2、选择,yes
出现了下载失败的问题
3、上面的报错是下载失败导致的。正如提示,简单的再试一下,或许就可以了。
四、pytorch测试
1、开始菜单->Anaconda3—>Anaconda Prompt 输入:python。进入python环境。
2、输入:import porch。如果没有报错,就表示安装成功。
3、pycharm中运行代码,有输出,表明安装ok
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)