-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
111 lines (96 loc) · 3.76 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
from torch import nn
import torch.nn.functional as F
from torchvision import transforms, datasets
import numpy as np
import matplotlib.pyplot as plt
def MNIST_dataset():
if not os.path.isdir("data"):
os.mkdir("data")
# Download MNIST dataset and set the valset as the test test
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,), (0.5,))])
test_set = datasets.MNIST('data/MNIST', download=True, train=False, transform=transform)
train_set = datasets.MNIST("data/MNIST", download=True, train=True, transform=transform)
return train_set, test_set
def CIFAR10_dataset():
if not os.path.isdir("data"):
os.mkdir("data")
# Download MNIST dataset and set the valset as the test test
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
test_set = datasets.CIFAR10('data/CIFAR10', download=True, train=False, transform=transform)
train_set = datasets.CIFAR10("data/CIFAR10", download=True, train=True, transform=transform)
return train_set, test_set
nn.Conv2d
def MNIST_one_layer():
# Create the nn model
input_size = 784
hidden_sizes = [64]
output_size = 10
# The non-convex function (model) is Sequential function fron torch. Notice that we have implemented an activation function to make it non-convex
model = nn.Sequential(
nn.Linear(input_size, hidden_sizes[0]),
nn.ReLU(),
nn.Linear(hidden_sizes[0], output_size),
nn.LogSoftmax(dim=1))
return model
def MNIST_two_layers():
# Create the nn model
input_size = 784
hidden_sizes = [128, 64]
output_size = 10
# The non-convex function (model) is Sequential function fron torch. Notice that we have implemented an activation function to make it non-convex
model = nn.Sequential(
nn.Linear(input_size, hidden_sizes[0]),
nn.ReLU(),
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
nn.ReLU(),
nn.Linear(hidden_sizes[1], output_size),
nn.LogSoftmax(dim=1))
return model
class CIFAR10_ConvNet(nn.Module):
def __init__(self):
super(CIFAR10_ConvNet, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def accuracy(yhat, labels):
_, indices = yhat.max(1)
return (indices == labels).sum().data.item() / float(len(labels))
class AverageCalculator():
def __init__(self):
self.reset()
def reset(self):
self.count = 0
self.sum = 0
self.avg = 0
def update(self, val, n=1):
assert(n > 0)
self.sum += val * n
self.count += n
self.avg = self.sum / float(self.count)
def plot_train_stats(train_loss, val_loss, train_acc, val_acc, directory, acc_low=0):
fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8,6), sharey='row')
axes[0][0].plot(np.array(train_loss))
axes[0][0].set_title("Train loss")
axes[0][1].plot(np.array(val_loss))
axes[0][1].set_title("Val loss")
axes[1][0].plot(np.array(train_acc))
axes[1][0].set_title("Train Accuracy")
axes[1][0].set_ylim(acc_low, 1)
axes[1][1].plot(np.array(val_acc))
axes[1][1].set_title("Val Accuracy")
plt.tight_layout()
plt.savefig(os.path.join(directory, 'train_stats.pdf'))
plt.savefig(os.path.join(directory, 'train_stats.png'))
plt.close()