-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtmp.py
65 lines (53 loc) · 1.84 KB
/
tmp.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
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 trainers.SGD import SGDTrainer
from datasets.iris import *
dataset, targets = load_iris_data()
# Create a neural network
net = NeuralNetwork()
# Create layers
input_layer = InputLayer(4)
target_layer = InputLayer(3)
hidden_layer = TanhLayer(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)
# Add the objective
objective = CrossEntropyObjective(output_layer.output, target_layer.output)
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)
net.setInputLayer(input_layer)
net.setTargetLayer(target_layer)
net.setInput(dataset)
net.setTarget(targets)
net.setOutputLayer(output_layer)
net.setObjective(objective)
# Randomize these connections
net.randomize()
# Set the input and targets
# Everything from here on out deals directly with net
# Create a trainer
trainer = SGDTrainer(net, learning_rate = 0.1)
for i in range(10000):
trainer.trainBatch((dataset, targets))
print i, net.getObjective()
print net.getOutput()