Reformat mnist.py code

This commit is contained in:
Anthony Wang 2021-09-10 15:51:38 +00:00
parent d50f8445a0
commit 2f0109fdbd
7 changed files with 170 additions and 22 deletions

View File

@ -0,0 +1,21 @@
# This initialization file is associated with your terminal in PyTorch/..mnist.py-0-python3.term.init.
# It is automatically run whenever it starts up -- restart the terminal via Ctrl-d and Return-key.
# Usually, your ~/.bashrc is executed and this behavior is emulated for completeness:
source ~/.bashrc
# You can export environment variables, e.g. to set custom GIT_* variables
# https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables
#export GIT_AUTHOR_NAME="Your Name"
#export GIT_AUTHOR_EMAIL="your@email.address"
#export GIT_COMMITTER_NAME="Your Name"
#export GIT_COMMITTER_EMAIL="your@email.address"
# It is also possible to automatically start a program ...
#sage
#sage -ipython
#top
# ... or even define a terminal specific function.
#hello () { echo "hello world"; }

View File

@ -4,4 +4,31 @@ Type "help", "copyright", "credits" or "license" for more information.
Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> mnist.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mnist' is not defined
>>> execfile('mnist.py')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'execfile' is not defined
>>> exec(open('mnist.py').read())
/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.39537739753723145, Accuracy: 92.41999816894531%
Iteration: 100, Loss: 0.13765878975391388, Accuracy: 92.33000183105469%
Iteration: 150, Loss: 0.13507212698459625, Accuracy: 96.91999816894531%
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 86, in <module>
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
>>>

View File

@ -366,4 +366,114 @@ Iteration: 1050, Loss: 0.04832194745540619, Accuracy: 97.83000183105469%
Variable._execution_engine.run_backward(
KeyboardInterrupt
]0;~/PyTorch~/PyTorch$ g
]0;~/PyTorch~/PyTorch$ git add- A
git: 'add-' is not a git command. See 'git --help'.
The most similar command is
add
]0;~/PyTorch~/PyTorch$ ggit add -A
]0;~/PyTorch~/PyTorch$ git commit
hint: Waiting for your editor to close the file... [?1049h[?1h=[?25l"~/PyTorch/.git/COMMIT_EDITMSG" 11L, 288C# 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
# modified:  mnist.py1,0-1Top[?25h[?25li -- INSERT --1,1Top[?25h[?25lU2[?25h[?25ls3[?25h[?25le4[?25h[?25l 5[?25h[?25lC6[?25h[?25l5[?25h[?25lG6[?25h[?25lU7[?25h[?25l 8[?25h[?25li9[?25h[?25l8[?25h[?25l7[?25h[?25l6[?25h[?25lP7[?25h[?25lU8[?25h[?25l 9[?25h[?25li10[?25h[?25lf1[?25h[?25l 2[?25h[?25la3[?25h[?25lv4[?25h[?25la5[?25h[?25ll6[?25h[?25li7[?25h[?25la8[?25h[?25lb9[?25h[?25ll20[?25h[?25l19[?25h[?25l8[?25h[?25l7[?25h[?25l6[?25h[?25l5[?25h[?25li6[?25h[?25la7[?25h[?25lb8[?25h[?25ll9[?25h[?25le20[?25h[?25l19[?25h[?25l8[?25h[?25l7[?25h[?25l6[?25h[?25ll7[?25h[?25la8[?25h[?25lb9[?25h[?25ll20[?25h[?25le1[?25h[?25l^[ 1,20Top[?25h[?25l::[?25hx [?25l".git/COMMIT_EDITMSG" 11L, 308C written
[?1l>[?25h[?1049l [main 438d211] Use GPU if available
2 files changed, 93 insertions(+), 2 deletions(-)
]0;~/PyTorch~/PyTorch$ git push
Warning: Permanently added the RSA host key for IP address '140.82.112.3' to the list of known hosts.
Enumerating objects: 7, done.
Counting objects: 14% (1/7) Counting objects: 28% (2/7) Counting objects: 42% (3/7) Counting objects: 57% (4/7) Counting objects: 71% (5/7) Counting objects: 85% (6/7) Counting objects: 100% (7/7) Counting objects: 100% (7/7), done.
Delta compression using up to 24 threads
Compressing objects: 25% (1/4) Compressing objects: 50% (2/4) Compressing objects: 75% (3/4) Compressing objects: 100% (4/4) Compressing objects: 100% (4/4), done.
Writing objects: 25% (1/4) Writing objects: 50% (2/4) Writing objects: 75% (3/4) Writing objects: 100% (4/4) Writing objects: 100% (4/4), 2.31 KiB | 2.31 MiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 0% (0/3) remote: Resolving deltas: 33% (1/3) remote: Resolving deltas: 66% (2/3) remote: Resolving deltas: 100% (3/3) remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:Ta180m/PyTorch.git
ada4698..438d211 main -> main
]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ python/mn./mnist.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)
^CTraceback (most recent call last):
File "./mnist.py", line 95, 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$ ]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$ ./mnist.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.2655351459980011, Accuracy: 93.6500015258789%
Iteration: 100, Loss: 0.09417656064033508, Accuracy: 93.77999877929688%
Iteration: 150, Loss: 0.14728593826293945, Accuracy: 96.0999984741211%
Iteration: 200, Loss: 0.08001164346933365, Accuracy: 97.04000091552734%
Iteration: 250, Loss: 0.15678852796554565, Accuracy: 96.12999725341797%
Iteration: 300, Loss: 0.09041783958673477, Accuracy: 96.87999725341797%
Iteration: 350, Loss: 0.10900043696165085, Accuracy: 97.08999633789062%
Iteration: 400, Loss: 0.2091383934020996, Accuracy: 96.8499984741211%
Iteration: 450, Loss: 0.012568566016852856, Accuracy: 97.58999633789062%
Iteration: 500, Loss: 0.09534303843975067, Accuracy: 95.9000015258789%
Iteration: 550, Loss: 0.12004967778921127, Accuracy: 97.94999694824219%
Iteration: 600, Loss: 0.26723435521125793, Accuracy: 96.75%
Iteration: 650, Loss: 0.10009327530860901, Accuracy: 98.08999633789062%
Iteration: 700, Loss: 0.03489111363887787, Accuracy: 95.26000213623047%
Iteration: 750, Loss: 0.030101114884018898, Accuracy: 98.12999725341797%
Iteration: 800, Loss: 0.05416644737124443, Accuracy: 97.61000061035156%
Iteration: 850, Loss: 0.11499112099409103, Accuracy: 97.8499984741211%
Iteration: 900, Loss: 0.20542272925376892, Accuracy: 97.66999816894531%
Iteration: 950, Loss: 0.05691840127110481, Accuracy: 97.88999938964844%
Iteration: 1000, Loss: 0.17045655846595764, Accuracy: 96.95999908447266%
Iteration: 1050, Loss: 0.028369026258587837, Accuracy: 98.19000244140625%
Iteration: 1100, Loss: 0.09225992113351822, Accuracy: 97.76000213623047%
Iteration: 1150, Loss: 0.038039229810237885, Accuracy: 98.22000122070312%
Iteration: 1200, Loss: 0.23273861408233643, Accuracy: 98.30000305175781%
Iteration: 1250, Loss: 0.08464375138282776, Accuracy: 98.66999816894531%
Iteration: 1300, Loss: 0.017008038237690926, Accuracy: 97.8499984741211%
Iteration: 1350, Loss: 0.05763726308941841, Accuracy: 98.63999938964844%
Iteration: 1400, Loss: 0.022395288571715355, Accuracy: 98.51000213623047%
Iteration: 1450, Loss: 0.06815487146377563, Accuracy: 98.51000213623047%
Iteration: 1500, Loss: 0.14768916368484497, Accuracy: 98.3499984741211%
Iteration: 1550, Loss: 0.021466469392180443, Accuracy: 98.52999877929688%
Iteration: 1600, Loss: 0.054903920739889145, Accuracy: 98.0999984741211%
Iteration: 1650, Loss: 0.009115751832723618, Accuracy: 98.44999694824219%
Iteration: 1700, Loss: 0.027846679091453552, Accuracy: 98.70999908447266%
Iteration: 1750, Loss: 0.019951678812503815, Accuracy: 98.5199966430664%
Iteration: 1800, Loss: 0.25205621123313904, Accuracy: 98.62000274658203%
Iteration: 1850, Loss: 0.02951984480023384, Accuracy: 98.62999725341797%
Iteration: 1900, Loss: 0.011210460215806961, Accuracy: 98.55000305175781%
Iteration: 1950, Loss: 0.05040852725505829, Accuracy: 98.5%
Iteration: 2000, Loss: 0.008486397564411163, Accuracy: 98.55999755859375%
Iteration: 2050, Loss: 0.059381142258644104, Accuracy: 98.61000061035156%
Iteration: 2100, Loss: 0.10324683040380478, Accuracy: 98.37000274658203%
Iteration: 2150, Loss: 0.06498480588197708, Accuracy: 98.16999816894531%
Iteration: 2200, Loss: 0.036080557852983475, Accuracy: 97.70999908447266%
Iteration: 2250, Loss: 0.013293210417032242, Accuracy: 98.66000366210938%
Iteration: 2300, Loss: 0.06331712007522583, Accuracy: 97.91000366210938%
Iteration: 2350, Loss: 0.004426905419677496, Accuracy: 98.02999877929688%
Iteration: 2400, Loss: 0.27985191345214844, Accuracy: 98.5%
Iteration: 2450, Loss: 0.04614001885056496, Accuracy: 98.5%
Iteration: 2500, Loss: 0.005236199591308832, Accuracy: 98.43000030517578%
Iteration: 2550, Loss: 0.026349853724241257, Accuracy: 98.43000030517578%
Iteration: 2600, Loss: 0.007622480392456055, Accuracy: 98.77999877929688%
Iteration: 2650, Loss: 0.04031902924180031, Accuracy: 98.58999633789062%
Iteration: 2700, Loss: 0.00840453989803791, Accuracy: 98.83999633789062%
Iteration: 2750, Loss: 0.07304922491312027, Accuracy: 98.19000244140625%
Iteration: 2800, Loss: 0.11154232174158096, Accuracy: 97.13999938964844%
Iteration: 2850, Loss: 0.014337321743369102, Accuracy: 98.37999725341797%
Iteration: 2900, Loss: 0.03685985505580902, Accuracy: 98.66999816894531%
Iteration: 2950, Loss: 0.018538275733590126, Accuracy: 98.58000183105469%
Iteration: 3000, Loss: 0.1944340467453003, Accuracy: 98.75%
Saved PyTorch Model State to model.pth
]0;~/PyTorch~/PyTorch$ exit
]0;~/PyTorch~/PyTorch$ ]0;~/PyTorch~/PyTorch$

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

BIN
loss.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

After

Width:  |  Height:  |  Size: 45 KiB

View File

@ -7,7 +7,6 @@ from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib.pyplot as plt
training_data = datasets.MNIST(
root=".data",
train=True,
@ -22,7 +21,6 @@ test_data = datasets.MNIST(
transform=ToTensor(),
)
batch_size = 100
train_loader = DataLoader(training_data, batch_size=batch_size)
@ -34,18 +32,13 @@ class CNN(nn.Module):
super(CNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3,
padding=1), nn.BatchNorm2d(32), nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = nn.Sequential(
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.fc1 = nn.Linear(in_features=64*6*6, out_features=600)
nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2))
self.fc1 = nn.Linear(in_features=64 * 6 * 6, out_features=600)
self.drop = nn.Dropout2d(0.25)
self.fc2 = nn.Linear(in_features=600, out_features=120)
self.fc3 = nn.Linear(in_features=120, out_features=10)
@ -61,7 +54,6 @@ class CNN(nn.Module):
return out
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = CNN()
@ -70,7 +62,6 @@ error = nn.CrossEntropyLoss()
learning_rate = 0.001
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
num_epochs = 5
count = 0
@ -104,28 +95,27 @@ for epoch in range(num_epochs):
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].to(device)
predictions_list.append(predictions)
correct += (predictions == labels).sum()
total += len(labels)
accuracy = correct * batch_size / total
loss_list.append(loss.data)
iteration_list.append(count)
accuracy_list.append(accuracy)
print("Iteration: {}, Loss: {}, Accuracy: {}%".format(count, loss.data, accuracy))
print("Iteration: {}, Loss: {}, Accuracy: {}%".format(
count, loss.data, accuracy))
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")
plt.plot(iteration_list, loss_list)
plt.xlabel("No. of Iteration")
plt.ylabel("Loss")

BIN
model.pth

Binary file not shown.