-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtryhw5.py
79 lines (67 loc) · 2.35 KB
/
tryhw5.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
import argparse
from uwimg import *
def parse_arguments():
parser = argparse.ArgumentParser(description = "Homework 5")
parser.add_argument("--model", help = "Model to use for training: either linear softmax model (softmax), 2 layer neural network (nn2), or 3 layer neural network (nn3)")
parser.add_argument("--data", help = "Dataset to use: either MNIST (mnist) or CIFAR-10 (cifar)")
args = parser.parse_args()
model = args.model if args.model else "softmax"
dataset = args.data if args.data else "mnist"
print("Using model:", model)
print("Using dataset:", dataset)
print
return model, dataset
def softmax_model(inputs, outputs):
l = [make_layer(inputs, outputs, SOFTMAX)]
return make_model(l)
def neural_net(inputs, outputs):
print(inputs)
l = [ make_layer(inputs, 32, LOGISTIC),
make_layer(32, outputs, SOFTMAX)]
return make_model(l)
def neural_net_3layer(inputs, outputs):
l = [ make_layer(inputs, 64, RELU),
make_layer(64, 32, RELU),
make_layer(32, outputs, SOFTMAX)]
return make_model(l)
model, dataset = parse_arguments()
if dataset == "mnist":
print("loading MNIST data...")
train = load_classification_data(b"mnist.train", b"mnist.labels", 1)
test = load_classification_data(b"mnist.test", b"mnist.labels", 1)
elif dataset == "cifar":
print("loading CIFAR-10 data...")
train = load_classification_data(b"cifar.train", b"cifar/labels.txt", 1)
test = load_classification_data(b"cifar.test", b"cifar/labels.txt", 1)
else:
print("Dataset not supported")
exit()
print("done")
print
print("training", model, "model...")
batch = 128
iters = 1000
rate = .01
momentum = .9
decay = .0
if model == "softmax":
m = softmax_model(train.X.cols, train.y.cols)
elif model == "nn2":
m = neural_net(train.X.cols, train.y.cols)
elif model == "nn3":
m = neural_net_3layer(train.X.cols, train.y.cols)
else:
print("Model not supported")
exit()
train_model(m, train, batch, iters, rate, momentum, decay)
print("done")
print
print("Model parameters:")
print("batch size:", batch)
print("epochs:", iters)
print("learning rate:", rate)
print("momentum:", momentum)
print("weight decay lambda:", decay)
print("evaluating model...")
print("training accuracy: %f", accuracy_model(m, train))
print("test accuracy: %f", accuracy_model(m, test))