From https://pytorch.org/ https://pytorch.org/
要在 MacOS 上安装 pytorch,请执行以下操作:
conda install pytorch torchvision -c pytorch
# MacOS Binaries dont support CUDA, install from source if CUDA is needed
为什么要在不启用 cuda 的情况下安装 pytorch ?
我问的原因是我收到错误:
-------------------------------------------------- -------------------------- AssertionError Traceback(最近调用
最后)在()
78 # 预测 = 输出.data.max(1)[1]
79
---> 80 输出 = model(torch.tensor([[1,1]]).float().cuda())
81 预测=output.data.max(1)[1]
82
〜/anaconda3/lib/python3.6/site-packages/torch/cuda/init.py 在
_lazy_init()
159 引发运行时错误(
160 “无法在分叉子进程中重新初始化 CUDA。” + msg)
--> 161 _check_driver()
162 火炬._C._cuda_init()
第163章
〜/anaconda3/lib/python3.6/site-packages/torch/cuda/init.py 在
_check_driver()
73 def _check_driver():
74 如果不是 hasattr(torch._C, '_cuda_isDriverSufficient'):
---> 75 raise AssertionError("Torch 未在启用 CUDA 的情况下编译")
76如果不是torch._C._cuda_isDriverSufficient():
77 如果 torch._C._cuda_getDriverVersion() == 0:
AssertionError:Torch 未在启用 CUDA 的情况下编译
当尝试执行代码时:
x = torch.tensor([[0,0] , [0,1] , [1,0]]).float()
print(x)
y = torch.tensor([0,1,1]).long()
print(y)
my_train = data_utils.TensorDataset(x, y)
my_train_loader = data_utils.DataLoader(my_train, batch_size=2, shuffle=True)
# Device configuration
device = 'cpu'
print(device)
# Hyper-parameters
input_size = 2
hidden_size = 100
num_classes = 2
learning_rate = 0.001
train_dataset = my_train
train_loader = my_train_loader
pred = []
for i in range(0 , model_iters) :
# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
model = NeuralNet(input_size, hidden_size, num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Move tensors to the configured device
images = images.reshape(-1, 2).to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
{:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
output = model(torch.tensor([[1,1]]).float().cuda())
要修复此错误,我需要从已安装 cuda 的源安装 pytorch 吗?