-
Notifications
You must be signed in to change notification settings - Fork 0
/
sin.py
92 lines (78 loc) · 3.39 KB
/
sin.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
80
81
82
83
84
85
86
87
88
89
90
91
92
# coding=utf-8
import math
from numpy import arange
from pybrain import SigmoidLayer, LinearLayer, FullConnection, FeedForwardNetwork, BiasUnit
from pybrain.datasets import SupervisedDataSet
from network import SupervisedNeuralNetwork
__author__ = 'Michał Ciołczyk'
class SinNeuralNetwork(SupervisedNeuralNetwork):
def __init__(self, hidden_layer_size):
"""Sin Neural Network class.
:param hidden_layer_size: Number of neurons in hidden layer.
"""
learning_data = SinNeuralNetwork.create_sin_learning_sample(-math.pi / 2, math.pi / 2, math.pi / 10)
test_data = SinNeuralNetwork.create_sin_sample(-math.pi, math.pi, math.pi / 20)
verify_data = SinNeuralNetwork.create_sin_sample(-math.pi / 2, math.pi / 2, math.pi / 20)
SupervisedNeuralNetwork.__init__(self, learning_data, verify_data, test_data)
self.network = SinNeuralNetwork.create_network(hidden_layer_size)
@staticmethod
def create_sin_learning_sample(start_range, end_range, step):
"""Samples sin function as SupervisedDataSet.
:rtype : SupervisedDataSet
:param start_range: start of sampling range (inclusive)
:param end_range: end of sampling range (exclusive)
:param step: sampling step
:return: sampled data
"""
data = SupervisedDataSet(1, 1)
for (x, y) in SinNeuralNetwork.create_sin_sample(start_range, end_range, step):
data.addSample([x], [y])
return data
@staticmethod
def create_sin_sample(start_range, end_range, step):
"""Samples sin function as list.
:rtype : list
:param start_range: start of sampling range (inclusive)
:param end_range: end of sampling range (exclusive)
:param step: sampling step
:return: sampled data
"""
data = []
for x in arange(start_range, end_range, step):
data.append((x, math.sin(x)))
return data
@staticmethod
def create_network(hidden_layer_size):
"""Creates sin NN.
:param hidden_layer_size: size of hidden layer
:return: network
"""
# Create network
network = FeedForwardNetwork()
# Create network layers and bias
input_layer = LinearLayer(1)
hidden_layer = SigmoidLayer(hidden_layer_size)
output_layer = SigmoidLayer(1)
bias = BiasUnit()
# Create connections
input_hidden_connection = FullConnection(input_layer, hidden_layer)
hidden_output_connection = FullConnection(hidden_layer, output_layer)
bias_hidden_connection = FullConnection(bias, hidden_layer)
bias_output_connection = FullConnection(bias, output_layer)
# Add network layers and bias to network
network.addInputModule(input_layer)
network.addModule(hidden_layer)
network.addOutputModule(output_layer)
network.addModule(bias)
# Add connections between layers and bias
network.addConnection(input_hidden_connection)
network.addConnection(hidden_output_connection)
network.addConnection(bias_hidden_connection)
network.addConnection(bias_output_connection)
# Sort modules and return network
network.sortModules()
return network
if __name__ == "__main__":
sin_NN = SinNeuralNetwork(10)
# sin_NN.run(0.01, 0.99, 1000)
sin_NN.run_until_convergence(0.01, 0.99, 10000, 50, True)