-
Notifications
You must be signed in to change notification settings - Fork 1
/
05_adder_with_graphs.lua
143 lines (113 loc) · 3.62 KB
/
05_adder_with_graphs.lua
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
--
-- (C) Copyright 2017 Pavel Tisnovsky
--
-- All rights reserved. This program and the accompanying materials
-- are made available under the terms of the Eclipse Public License v1.0
-- which accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- Contributors:
-- Pavel Tisnovsky
--
require("nn")
require("gnuplot")
TRAINING_DATA_SIZE = 500
INPUT_NEURONS = 2
HIDDEN_NEURONS = 2
OUTPUT_NEURONS = 1
MAX_ITERATION = 200
LEARNING_RATE = 0.01
function prepare_training_data(training_data_size)
local training_data = {}
function training_data:size() return training_data_size end
for i = 1,training_data_size do
local input = torch.randn(2)
local output = torch.Tensor(1)
output[1] = input[1] + input[2]
training_data[i] = {input, output}
end
return training_data
end
function construct_neural_network(input_neurons, hidden_neurons, output_neurons)
local network = nn.Sequential()
network:add(nn.Linear(input_neurons, hidden_neurons))
--network:add(nn.ReLU())
network:add(nn.Tanh())
network:add(nn.Linear(hidden_neurons, output_neurons))
return network
end
function train_neural_network(network, training_data, learning_rate, max_iteration)
local criterion = nn.MSECriterion()
local trainer = nn.StochasticGradient(network, criterion)
trainer.learningRate = learning_rate
trainer.maxIteration = max_iteration
trainer:train(training_data)
end
function validate_neural_network(network, validation_data)
for i,d in ipairs(validation_data) do
local d1, d2 = d[1], d[2]
local input = torch.Tensor({d1, d2})
local prediction = network:forward(input)[1]
local correct = d1 + d2
local err = math.abs(100.0 * (prediction-correct)/correct)
local msg = string.format("%2d %+6.3f %+6.3f %+6.3f %+6.3f %4.0f%%", i, d1, d2, correct, prediction, err)
print(msg)
end
end
function plot_graph(filename, x, y1, y2)
gnuplot.pngfigure(filename)
gnuplot.title("Adder NN")
gnuplot.xlabel("x")
gnuplot.ylabel("x+y")
gnuplot.movelegend("left", "top")
gnuplot.plot({"correct", x, y1},
{"predict", x, y2})
gnuplot.plotflush()
gnuplot.close()
end
function prepare_graph(filename, from, to, items, d1)
local x = torch.linspace(from, to, items)
local size = x:size(1)
local y1 = torch.Tensor(size)
local y2 = torch.Tensor(size)
for i = 1, size do
local d2 = x[i]
-- presny vysledek
y1[i] = d1 + d2
-- vstup do neuronove site
local input = torch.Tensor({d1, d2})
-- vysledek odhadnuty neuronovou siti
local prediction = network:forward(input)[1]
y2[i] = prediction
end
plot_graph(filename, x, y1, y2)
end
network = construct_neural_network(INPUT_NEURONS, HIDDEN_NEURONS, OUTPUT_NEURONS)
training_data = prepare_training_data(TRAINING_DATA_SIZE)
train_neural_network(network, training_data, LEARNING_RATE, MAX_ITERATION)
print(network)
x=torch.Tensor({0.5, -0.5})
prediction = network:forward(x)
print(prediction)
validation_data = {
{ 1.0, 1,0},
{ 0.5, 0.5},
{ 0.2, 0.2},
-------------
{-1.0, 1.1},
{-0.5, 0.6},
{-0.2, 0.3},
-------------
{ 1.0, -1.1},
{ 0.5, -0.6},
{ 0.2, -0.3},
-------------
{-1.0, -1,0},
{-0.5, -0.5},
{-0.2, -0.2},
}
validate_neural_network(network, validation_data)
prepare_graph("adder_a1.png", -2, 2, 21, 0.5)
prepare_graph("adder_a2.png", -10, 10, 21, 0.5)
prepare_graph("adder_a3.png", -2, 2, 21, 2.0)
prepare_graph("adder_a4.png", -2, 2, 21, 5.0)