Skip to content

Commit 86a0872

Browse files
committed
code for saving the model is added
1 parent 278c135 commit 86a0872

File tree

17 files changed

+630
-37
lines changed

17 files changed

+630
-37
lines changed

tutorials/01 - Linear Regression/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@ def forward(self, x):
5858
plt.plot(x_train, y_train, 'ro', label='Original data')
5959
plt.plot(x_train, predicted, label='Fitted line')
6060
plt.legend()
61-
plt.show()
61+
plt.show()
62+
63+
# Save the Model
64+
torch.save(model, 'model.pkl')

tutorials/02 - Logistic Regression/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
learning_rate = 0.001
1414

1515
# MNIST Dataset (Images and Labels)
16-
train_dataset = dsets.MNIST(root='./data',
16+
train_dataset = dsets.MNIST(root='../data',
1717
train=True,
1818
transform=transforms.ToTensor(),
1919
download=True)
2020

21-
test_dataset = dsets.MNIST(root='./data',
21+
test_dataset = dsets.MNIST(root='../data',
2222
train=False,
2323
transform=transforms.ToTensor())
2424

@@ -76,4 +76,7 @@ def forward(self, x):
7676
total += labels.size(0)
7777
correct += (predicted == labels).sum()
7878

79-
print('Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
79+
print('Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
80+
81+
# Save the Model
82+
torch.save(model, 'model.pkl')

tutorials/03 - Feedforward Neural Network/main-gpu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
learning_rate = 0.001
1515

1616
# MNIST Dataset
17-
train_dataset = dsets.MNIST(root='./data',
17+
train_dataset = dsets.MNIST(root='../data',
1818
train=True,
1919
transform=transforms.ToTensor(),
2020
download=True)
2121

22-
test_dataset = dsets.MNIST(root='./data',
22+
test_dataset = dsets.MNIST(root='../data',
2323
train=False,
2424
transform=transforms.ToTensor())
2525

tutorials/03 - Feedforward Neural Network/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
learning_rate = 0.001
1515

1616
# MNIST Dataset
17-
train_dataset = dsets.MNIST(root='./data',
17+
train_dataset = dsets.MNIST(root='../data',
1818
train=True,
1919
transform=transforms.ToTensor(),
2020
download=True)
2121

22-
test_dataset = dsets.MNIST(root='./data',
22+
test_dataset = dsets.MNIST(root='../data',
2323
train=False,
2424
transform=transforms.ToTensor())
2525

tutorials/04 - Convolutional Neural Network/main-gpu.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
learning_rate = 0.001
1212

1313
# MNIST Dataset
14-
train_dataset = dsets.MNIST(root='./data/',
14+
train_dataset = dsets.MNIST(root='../data/',
1515
train=True,
1616
transform=transforms.ToTensor(),
1717
download=True)
1818

19-
test_dataset = dsets.MNIST(root='./data/',
19+
test_dataset = dsets.MNIST(root='../data/',
2020
train=False,
2121
transform=transforms.ToTensor())
2222

@@ -77,14 +77,17 @@ def forward(self, x):
7777
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
7878

7979
# Test the Model
80-
cnn.eval()
80+
cnn.eval() # Change model to 'eval' mode (BN uses moving mean/var).
8181
correct = 0
8282
total = 0
8383
for images, labels in test_loader:
8484
images = Variable(images).cuda()
8585
outputs = cnn(images)
8686
_, predicted = torch.max(outputs.data, 1)
8787
total += labels.size(0)
88-
correct += (predicted == labels).sum()
88+
correct += (predicted.cpu() == labels).sum()
8989

90-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
90+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
92+
# Save the Trained Model
93+
torch.save(cnn, 'cnn.pkl')

tutorials/04 - Convolutional Neural Network/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
learning_rate = 0.001
1212

1313
# MNIST Dataset
14-
train_dataset = dsets.MNIST(root='./data/',
14+
train_dataset = dsets.MNIST(root='../data/',
1515
train=True,
1616
transform=transforms.ToTensor(),
1717
download=True)
1818

19-
test_dataset = dsets.MNIST(root='./data/',
19+
test_dataset = dsets.MNIST(root='../data/',
2020
train=False,
2121
transform=transforms.ToTensor())
2222

@@ -77,7 +77,7 @@ def forward(self, x):
7777
%(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
7878

7979
# Test the Model
80-
cnn.eval()
80+
cnn.eval() # Change model to 'eval' mode (BN uses moving mean/var).
8181
correct = 0
8282
total = 0
8383
for images, labels in test_loader:
@@ -87,4 +87,7 @@ def forward(self, x):
8787
total += labels.size(0)
8888
correct += (predicted == labels).sum()
8989

90-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
90+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
92+
# Save the Trained Model
93+
torch.save(cnn, 'cnn.pkl')

tutorials/05 - Deep Residual Network/main-gpu.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
transforms.ToTensor()])
1515

1616
# CIFAR-10 Dataset
17-
train_dataset = dsets.CIFAR10(root='./data/',
17+
train_dataset = dsets.CIFAR10(root='../data/',
1818
train=True,
1919
transform=transform,
2020
download=True)
2121

22-
test_dataset = dsets.CIFAR10(root='./data/',
22+
test_dataset = dsets.CIFAR10(root='../data/',
2323
train=False,
2424
transform=transforms.ToTensor())
2525

@@ -109,7 +109,7 @@ def forward(self, x):
109109
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
110110

111111
# Training
112-
for epoch in range(40):
112+
for epoch in range(80):
113113
for i, (images, labels) in enumerate(train_loader):
114114
images = Variable(images.cuda())
115115
labels = Variable(labels.cuda())
@@ -122,14 +122,15 @@ def forward(self, x):
122122
optimizer.step()
123123

124124
if (i+1) % 100 == 0:
125-
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 40, i+1, 500, loss.data[0]))
125+
print ("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, 80, i+1, 500, loss.data[0]))
126126

127127
# Decaying Learning Rate
128128
if (epoch+1) % 20 == 0:
129129
lr /= 3
130130
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
131131

132132
# Test
133+
resnet.eval()
133134
correct = 0
134135
total = 0
135136
for images, labels in test_loader:
@@ -139,4 +140,7 @@ def forward(self, x):
139140
total += labels.size(0)
140141
correct += (predicted.cpu() == labels).sum()
141142

142-
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
143+
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
144+
145+
# Save the Model
146+
torch.save(resnet, 'resnet.pkl')

tutorials/05 - Deep Residual Network/main.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
transforms.ToTensor()])
1515

1616
# CIFAR-10 Dataset
17-
train_dataset = dsets.CIFAR10(root='./data/',
17+
train_dataset = dsets.CIFAR10(root='../data/',
1818
train=True,
1919
transform=transform,
2020
download=True)
2121

22-
test_dataset = dsets.CIFAR10(root='./data/',
22+
test_dataset = dsets.CIFAR10(root='../data/',
2323
train=False,
2424
transform=transforms.ToTensor())
2525

@@ -130,6 +130,7 @@ def forward(self, x):
130130
optimizer = torch.optim.Adam(resnet.parameters(), lr=lr)
131131

132132
# Test
133+
resnet.eval()
133134
correct = 0
134135
total = 0
135136
for images, labels in test_loader:
@@ -139,4 +140,7 @@ def forward(self, x):
139140
total += labels.size(0)
140141
correct += (predicted == labels).sum()
141142

142-
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
143+
print('Accuracy of the model on the test images: %d %%' % (100 * correct / total))
144+
145+
# Save the Model
146+
torch.save(resnet, 'resnet.pkl')

tutorials/06 - Recurrent Neural Network/main-gpu.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
learning_rate = 0.01
1717

1818
# MNIST Dataset
19-
train_dataset = dsets.MNIST(root='./data/',
19+
train_dataset = dsets.MNIST(root='../data/',
2020
train=True,
2121
transform=transforms.ToTensor(),
2222
download=True)
2323

24-
test_dataset = dsets.MNIST(root='./data/',
24+
test_dataset = dsets.MNIST(root='../data/',
2525
train=False,
2626
transform=transforms.ToTensor())
2727

@@ -87,4 +87,7 @@ def forward(self, x):
8787
total += labels.size(0)
8888
correct += (predicted.cpu() == labels).sum()
8989

90-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
90+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
92+
# Save the Model
93+
torch.save(rnn, 'rnn.pkl')

tutorials/06 - Recurrent Neural Network/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
learning_rate = 0.01
1717

1818
# MNIST Dataset
19-
train_dataset = dsets.MNIST(root='./data/',
19+
train_dataset = dsets.MNIST(root='../data/',
2020
train=True,
2121
transform=transforms.ToTensor(),
2222
download=True)
2323

24-
test_dataset = dsets.MNIST(root='./data/',
24+
test_dataset = dsets.MNIST(root='../data/',
2525
train=False,
2626
transform=transforms.ToTensor())
2727

@@ -87,4 +87,7 @@ def forward(self, x):
8787
total += labels.size(0)
8888
correct += (predicted == labels).sum()
8989

90-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
90+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
92+
# Save the Model
93+
torch.save(rnn, 'rnn.pkl')

tutorials/07 - Bidirectional Recurrent Neural Network/main-gpu.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
learning_rate = 0.003
1717

1818
# MNIST Dataset
19-
train_dataset = dsets.MNIST(root='./data/',
19+
train_dataset = dsets.MNIST(root='../data/',
2020
train=True,
2121
transform=transforms.ToTensor(),
2222
download=True)
2323

24-
test_dataset = dsets.MNIST(root='./data/',
24+
test_dataset = dsets.MNIST(root='../data/',
2525
train=False,
2626
transform=transforms.ToTensor())
2727

@@ -88,4 +88,7 @@ def forward(self, x):
8888
total += labels.size(0)
8989
correct += (predicted.cpu() == labels).sum()
9090

91-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
92+
93+
# Save the Model
94+
torch.save(rnn, 'rnn.pkl')

tutorials/07 - Bidirectional Recurrent Neural Network/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
learning_rate = 0.003
1717

1818
# MNIST Dataset
19-
train_dataset = dsets.MNIST(root='./data/',
19+
train_dataset = dsets.MNIST(root='../data/',
2020
train=True,
2121
transform=transforms.ToTensor(),
2222
download=True)
2323

24-
test_dataset = dsets.MNIST(root='./data/',
24+
test_dataset = dsets.MNIST(root='../data/',
2525
train=False,
2626
transform=transforms.ToTensor())
2727

@@ -88,4 +88,7 @@ def forward(self, x):
8888
total += labels.size(0)
8989
correct += (predicted == labels).sum()
9090

91-
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
91+
print('Test Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))
92+
93+
# Save the Model
94+
torch.save(rnn, 'rnn.pkl')
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import torch
2+
import os
3+
4+
class Dictionary(object):
5+
def __init__(self):
6+
self.word2idx = {}
7+
self.idx2word = {}
8+
self.idx = 0
9+
10+
def add_word(self, word):
11+
if not word in self.word2idx:
12+
self.word2idx[word] = self.idx
13+
self.idx2word[self.idx] = word
14+
self.idx += 1
15+
16+
def __len__(self):
17+
return len(self.word2idx)
18+
19+
class Corpus(object):
20+
def __init__(self, path='./data'):
21+
self.dictionary = Dictionary()
22+
self.train = os.path.join(path, 'train.txt')
23+
self.test = os.path.join(path, 'test.txt')
24+
25+
def get_data(self, path, batch_size=20):
26+
# Add words to the dictionary
27+
with open(path, 'r') as f:
28+
tokens = 0
29+
for line in f:
30+
words = line.split() + ['<eos>']
31+
tokens += len(words)
32+
for word in words:
33+
self.dictionary.add_word(word)
34+
35+
# Tokenize the file content
36+
ids = torch.LongTensor(tokens)
37+
token = 0
38+
with open(path, 'r') as f:
39+
for line in f:
40+
words = line.split() + ['<eos>']
41+
for word in words:
42+
ids[token] = self.dictionary.word2idx[word]
43+
token += 1
44+
num_batches = ids.size(0) // batch_size
45+
ids = ids[:num_batches*batch_size]
46+
return ids.view(batch_size, -1)

0 commit comments

Comments
 (0)