-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlp.py
67 lines (52 loc) · 1.91 KB
/
mlp.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
import numpy as np
from eureka.activation import relu, sigmoid, sigmoid_prime, softmax
from eureka.utils import one_hot_encoder, dataloader
import eureka.losses as losses
import eureka.optim as optim
import eureka.nn as nn
import datasets.mnist
def main():
# Load dataset
train_x, train_y = datasets.mnist.load_dataset(download=True, train=True)
# Preprocess Dataset
x = train_x.reshape(train_x.shape[0], -1)
y = one_hot_encoder(train_y)
num_samples = x.shape[0]
# Prepare the dataloader
trainloader = dataloader(x, y, batch_size=64, shuffle=True)
# Define model's architecture
model = nn.Sequential([
nn.Linear(784, 256),
nn.ReLU(),
nn.Linear(256, 10),
nn.Softmax()
])
# Define the optimizer
optimizer = optim.Adam(model, lr=0.0002)
# Define the criterion/loss function
criterion = losses.CrossEntropyLoss()
num_epochs = 20
for epoch in range(1, num_epochs+1):
print("Epoch: {}/{}\n==========".format(epoch, num_epochs))
acc = 0
batch_loss = 0
for inputs, labels in trainloader:
# Number of samples per batch
m = inputs.shape[0]
# Forward Propagation
out = model.forward(inputs)
# Compute accuracy
pred = np.argmax(out, axis=1)
pred = pred.reshape(pred.shape[0], 1)
acc += np.sum(pred == labels.argmax(axis=1).reshape(m,1))
# Compute loss
batch_loss += criterion(out, labels)
# Backward Propagation within Loss Function
back_var = criterion.backward()
# Backward Propagation within the Model
model.backward(back_var)
# Optimization Step
optimizer.step()
print("Loss: {:.6f}".format(batch_loss/num_samples))
print("Accuracy: {:.2f}%\n".format(acc/num_samples*100))
main()