Hi there,
I am encountering CUDA error: out of memory while implementing a simple CNN.
The error apparently is pointing to loss,backward(). I also used .detach() wherever I am saving metrics of accuracy and loss to a list. Am I missing something?
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-14-e531653ce2a2> in <module>
20 outputs = net(items)
21 loss = criterion(outputs,labels)
---> 22 loss.backward()
23 optimizer.step()
24
~\Anaconda3\envs\pytorch\lib\site-packages\torch\tensor.py in backward(self, gradient, retain_graph, create_graph)
91 products. Defaults to ``False``.
92 """
---> 93 torch.autograd.backward(self, gradient, retain_graph, create_graph)
94
95 def register_hook(self, hook):
~\Anaconda3\envs\pytorch\lib\site-packages\torch\autograd\__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
88 Variable._execution_engine.run_backward(
89 tensors, grad_tensors, retain_graph, create_graph,
---> 90 allow_unreachable=True) # allow_unreachable flag
91
92
RuntimeError: CUDA error: out of memory
My code is as follows:
num_epochs = 50
train_loss = []
valid_loss = []
train_accuracy = []
valid_accuracy = []
for epoch in range(num_epochs):
iter_loss = 0
correct = 0
# Train the network
net.train()
for i,(items,labels) in enumerate(train_loader):
items = Variable(items)
labels = Variable(labels)
if cuda.is_available():
items = items.cuda()
labels = labels.cuda()
optimizer.zero_grad()
outputs = net(items)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
# Book keeping for iterations
iter_loss += loss
_,predictions = torch.max(outputs,1)
correct_pred = (predictions==labels).sum()
correct += correct_pred
# Book keeping for Epochs
train_loss.append(iter_loss.detach())
train_accuracy.append((100*correct / len(MNIST_train)).detach())
# Validate
net.eval()
iter_loss = 0
correct = 0
for i,(items,labels) in enumerate(valid_loader):
items = Variable(items)
labels = Variable(labels)
if cuda.is_available():
items = items.cuda()
labels = labels.cuda()
outputs = net(items)
loss = criterion(outputs,labels)
# Book keeping for iterations
iter_loss += loss
_,predictions = torch.max(outputs,1)
correct_pred = (predictions==labels).sum()
correct += correct_pred
# Book keeping for Epochs
valid_loss.append(iter_loss.detach())
valid_accuracy.append((100*correct / len(MNIST_valid)).detach())
# Print perfomance metrics
print('epoch: {}/{} ,train_accuracy={},valid_accuracy={}'.format(epoch+1,num_epochs,train_accuracy[-1],valid_accuracy[-1]))