Use GPU if available

This commit is contained in:
Anthony Wang 2021-09-09 01:00:00 +00:00
parent f4a89b6979
commit d50f8445a0
2 changed files with 78 additions and 2 deletions

View file

@ -298,4 +298,72 @@ Iteration: 2900, Loss: 0.059621091932058334, Accuracy: 98.2699966430664%
Iteration: 2950, Loss: 0.023448023945093155, Accuracy: 98.54000091552734%
Iteration: 3000, Loss: 0.2632042467594147, Accuracy: 98.77999877929688%
Saved PyTorch Model State to model.pth
]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ g
]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ git add -A
]0;~/PyTorch~/PyTorch$ git commit
hint: Waiting for your editor to close the file... [?1049h[?1h=[?25l"~/PyTorch/.git/COMMIT_EDITMSG" 10L, 265C# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# modified:  .mnist.py-0.term
#1,0-1All[?25h[?25lA -- INSERT --1,1All[?25h[?25ld2[?25h[?25l1[?25h[?25lA2[?25h[?25ld3[?25h[?25ld4[?25h[?25l 5[?25h[?25lc6[?25h[?25lo7[?25h[?25l6[?25h[?25l5[?25h[?25l"6[?25h[?25l^[ 1,5All[?25h[?25l"[?25h[?25lq[?25h[?25l::[?25hq! [?25l[?1l>[?25h[?1049l Aborting commit due to empty commit message.
]0;~/PyTorch~/PyTorch$ git commit
hint: Waiting for your editor to close the file... [?1049h[?1h=[?25l"~/PyTorch/.git/COMMIT_EDITMSG" 10L, 265C# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# modified:  .mnist.py-0.term
#1,0-1All[?25h[?25li -- INSERT --1,1All[?25h[?25lA2[?25h[?25ld3[?25h[?25ld4[?25h[?25l 5[?25h[?25lC6[?25h[?25lo7[?25h[?25lC8[?25h[?25la9[?25h[?25ll10[?25h[?25lc1[?25h[?25l 2[?25h[?25lf3[?25h[?25li4[?25h[?25ll5[?25h[?25le6[?25h[?25ls7[?25h[?25l^[ 1,16All[?25h[?25l::[?25hwq [?25l".git/COMMIT_EDITMSG" 10L, 281C written
[?1l>[?25h[?1049l [main ada4698] Add CoCalc files
1 file changed, 21 insertions(+), 1 deletion(-)
]0;~/PyTorch~/PyTorch$ git push
Enumerating objects: 5, done.
Counting objects: 20% (1/5) Counting objects: 40% (2/5) Counting objects: 60% (3/5) Counting objects: 80% (4/5) Counting objects: 100% (5/5) Counting objects: 100% (5/5), done.
Delta compression using up to 24 threads
Compressing objects: 33% (1/3) Compressing objects: 66% (2/3) Compressing objects: 100% (3/3) Compressing objects: 100% (3/3), done.
Writing objects: 33% (1/3) Writing objects: 66% (2/3) Writing objects: 100% (3/3) Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 0% (0/2) remote: Resolving deltas: 50% (1/2) remote: Resolving deltas: 100% (2/2) remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:Ta180m/PyTorch.git
965db73..ada4698 main -> main
]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ./nmnist.py
/projects/800fec81-81db-4589-8df3-d839b1d21871/.local/lib/python3.8/site-packages/torchvision/datasets/mnist.py:498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:180.)
return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
/projects/800fec81-81db-4589-8df3-d839b1d21871/.local/lib/python3.8/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)
return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Iteration: 50, Loss: 0.33904707431793213, Accuracy: 91.33999633789062%
Iteration: 100, Loss: 0.11570730060338974, Accuracy: 92.58000183105469%
Iteration: 150, Loss: 0.08451800048351288, Accuracy: 96.02999877929688%
Iteration: 200, Loss: 0.08893018960952759, Accuracy: 96.6500015258789%
Iteration: 250, Loss: 0.1327921748161316, Accuracy: 96.98999786376953%
Iteration: 300, Loss: 0.0620574913918972, Accuracy: 97.16999816894531%
Iteration: 350, Loss: 0.09436647593975067, Accuracy: 97.05999755859375%
Iteration: 400, Loss: 0.25261232256889343, Accuracy: 94.94000244140625%
Iteration: 450, Loss: 0.11671247333288193, Accuracy: 97.69000244140625%
Iteration: 500, Loss: 0.1204160675406456, Accuracy: 96.45999908447266%
Iteration: 550, Loss: 0.05714935436844826, Accuracy: 98.19999694824219%
Iteration: 600, Loss: 0.25882184505462646, Accuracy: 98.05999755859375%
Iteration: 650, Loss: 0.15288296341896057, Accuracy: 98.51000213623047%
Iteration: 700, Loss: 0.030312277376651764, Accuracy: 96.8499984741211%
Iteration: 750, Loss: 0.026591232046484947, Accuracy: 98.12000274658203%
Iteration: 800, Loss: 0.0336340069770813, Accuracy: 98.01000213623047%
Iteration: 850, Loss: 0.10625416785478592, Accuracy: 97.16000366210938%
Iteration: 900, Loss: 0.11373892426490784, Accuracy: 98.0199966430664%
Iteration: 950, Loss: 0.12648221850395203, Accuracy: 97.26000213623047%
Iteration: 1000, Loss: 0.19489459693431854, Accuracy: 97.1500015258789%
Iteration: 1050, Loss: 0.04832194745540619, Accuracy: 97.83000183105469%
^CTraceback (most recent call last):
File "./mnist.py", line 89, in <module>
loss.backward()
File "/projects/800fec81-81db-4589-8df3-d839b1d21871/.local/lib/python3.8/site-packages/torch/_tensor.py", line 255, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File "/projects/800fec81-81db-4589-8df3-d839b1d21871/.local/lib/python3.8/site-packages/torch/autograd/__init__.py", line 147, in backward
Variable._execution_engine.run_backward(
KeyboardInterrupt
]0;~/PyTorch~/PyTorch$ g

View file

@ -61,7 +61,11 @@ class CNN(nn.Module):
return out
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = CNN()
model.to(device)
error = nn.CrossEntropyLoss()
learning_rate = 0.001
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
@ -79,6 +83,8 @@ labels_list = []
for epoch in range(num_epochs):
for images, labels in train_loader:
images, labels = images.to(device), labels.to(device)
train = Variable(images.view(batch_size, 1, 28, 28))
labels = Variable(labels)
@ -95,12 +101,14 @@ for epoch in range(num_epochs):
total = 0
correct = 0
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
labels_list.append(labels)
test = Variable(images.view(batch_size, 1, 28, 28))
outputs = model(test)
predictions = torch.max(outputs, 1)[1]
predictions = torch.max(outputs, 1)[1].to(device)
predictions_list.append(predictions)
correct += (predictions == labels).sum()