-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtmp2.py
More file actions
75 lines (60 loc) · 1.99 KB
/
tmp2.py
File metadata and controls
75 lines (60 loc) · 1.99 KB
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
from nn.components.objectives.CrossEntropyObjective import *
from nn.components.objectives.MSEObjective import *
from nn.components.layers.InputLayer import *
from nn.components.layers.SigmoidLayer import *
from nn.components.layers.TanhLayer import *
from nn.components.layers.ReluLayer import *
from nn.components.layers.SoftReluLayer import *
from nn.components.layers.SoftmaxLayer import *
from nn.components.connections.FullConnection import *
from nn.components.connections.Bias import *
from nn.models.NeuralNetwork import NeuralNetwork
from datasets.iris import *
dataset, targets = load_iris_data()
# Create layers
input_layer = InputLayer(4)
target_layer = InputLayer(3)
hidden_layer = SoftReluLayer(5)
output_layer = SoftmaxLayer(3)
# Make the connection from input to output
conn1 = FullConnection(input_layer.output, hidden_layer.input)
bias1 = Bias(hidden_layer.input)
conn2 = FullConnection(hidden_layer.output, output_layer.input)
bias2 = Bias(output_layer.input)
# Make the objective
objective = MSEObjective(output_layer.output, target_layer.output)
# Build the neural network
net = NeuralNetwork()
net.addLayer(input_layer)
net.addLayer(target_layer)
net.addConnection(conn1)
net.addConnection(bias1)
net.addLayer(hidden_layer)
net.addConnection(conn2)
net.addConnection(bias2)
net.addLayer(output_layer)
net.addObjective(objective)
# Set the input and targets
net.setInputLayer(input_layer)
net.setTargetLayer(target_layer)
net.setObjective(objective)
net.setOutputLayer(output_layer)
# Set the input and targets to train to
net.setInput(dataset)
net.setTarget(targets)
net.randomize()
for i in range(10):
# Do a training cycle
net.reset()
net.forward()
net.backward()
net.update()
print i, net.getObjective()
# Get the gradients
gradients = net.getParameterGradients()
updates = {}
# Multiply by the learning rate
for connection, gradient in gradients.items():
updates[connection] = -0.9*gradient / 150
# Update the gradients
net.updateParameters(updates)