forked from andersbll/deeppy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlp_dropout_mnist.py
executable file
·78 lines (67 loc) · 2.29 KB
/
mlp_dropout_mnist.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
#!/usr/bin/env python
# coding: utf-8
import os
import numpy as np
import deeppy as dp
def run():
# Prepare data
dataset = dp.datasets.MNIST()
x, y = dataset.data(flat=True)
x = x.astype(dp.float_)
y = y.astype(dp.int_)
train_idx, test_idx = dataset.split()
x_train = x[train_idx]
y_train = y[train_idx]
x_test = x[test_idx]
y_test = y[test_idx]
scaler = dp.UniformScaler(high=255.)
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
batch_size = 128
train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size)
test_input = dp.SupervisedInput(x_test, y_test)
# Setup neural network
net = dp.NeuralNetwork(
layers=[
dp.Dropout(0.2),
dp.DropoutFullyConnected(
n_output=800,
dropout=0.5,
weights=dp.Parameter(dp.NormalFiller(sigma=0.01),
weight_decay=0.00001),
),
dp.Activation('relu'),
dp.DropoutFullyConnected(
n_output=800,
dropout=0.5,
weights=dp.Parameter(dp.NormalFiller(sigma=0.01),
weight_decay=0.00001),
),
dp.Activation('relu'),
dp.DropoutFullyConnected(
n_output=dataset.n_classes,
weights=dp.Parameter(dp.NormalFiller(sigma=0.01),
weight_decay=0.00001),
),
dp.MultinomialLogReg(),
],
)
# Train neural network
def val_error():
return net.error(test_input)
trainer = dp.StochasticGradientDescent(
max_epochs=50,
learn_rule=dp.Momentum(learn_rate=0.1, momentum=0.9),
)
trainer.train(net, train_input, val_error)
# Visualize weights from first layer
W = next(np.array(layer.params()[0].array) for layer in net.layers
if isinstance(layer, dp.FullyConnected))
W = np.reshape(W.T, (-1, 28, 28))
filepath = os.path.join('mnist', 'mlp_dropout_weights.png')
dp.misc.img_save(dp.misc.img_tile(dp.misc.img_stretch(W)), filepath)
# Evaluate on test data
error = net.error(test_input)
print('Test error rate: %.4f' % error)
if __name__ == '__main__':
run()