-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural-network.py
787 lines (672 loc) · 31.6 KB
/
neural-network.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import math
from abc import ABC, abstractmethod
from collections import deque
from enum import Enum
import numpy as np
import random
class DataMismatchError(Exception):
""" Label and example lists have different lengths """
class NNData:
""" Maintain and dispense examples for use by a Neural
Network Application """
class Order(Enum):
""" Indicate whether data will be shuffled for each new epoch """
RANDOM = 0
SEQUENTIAL = 1
class Set(Enum):
""" Indicate which set should be accessed or manipulated """
TRAIN = 0
TEST = 1
@staticmethod
def percentage_limiter(percentage: float):
""" Ensure that percentage is bounded between 0 and 1 """
return min(1, max(percentage, 0))
def __init__(self, features=None, labels=None, train_factor=.9):
self._train_factor = NNData.percentage_limiter(train_factor)
if features is None:
features = []
if labels is None:
labels = []
self._features = None
self._labels = None
self._train_indices = []
self._test_indices = []
self._train_pool = deque()
self._test_pool = deque()
self._reporting_nodes = dict()
try:
self.load_data(features, labels)
except (ValueError, DataMismatchError):
pass
def _clear_data(self):
""" Reset features and labels, and make sure all
indices are reset as well
"""
self._features = None
self._labels = None
self.split_set()
def load_data(self, features: list = None, labels: list = None):
""" Load feature and label data, with some checks to ensure
that data is valid
"""
if features is None or labels is None:
self._clear_data()
return
if len(features) != len(labels):
self._clear_data()
raise DataMismatchError("Label and example lists have "
"different lengths")
if len(features) > 0:
if not (isinstance(features[0], list)
and isinstance(labels[0], list)):
self._clear_data()
raise ValueError("Label and example lists must be "
"homogeneous numeric lists of lists")
try:
self._features = np.array(features, dtype=float)
self._labels = np.array(labels, dtype=float)
except ValueError:
self._clear_data()
raise ValueError("Label and example lists must be homogeneous "
"and numeric lists of lists")
self.split_set()
def split_set(self, new_train_factor=None):
""" Split indices between training set and testing set based on
new train factor calculation
"""
if new_train_factor is not None:
self._train_factor = NNData.percentage_limiter(new_train_factor)
if self._features is None or len(self._features) == 0:
self._train_indices = []
self._test_indices = []
return
num_samples = list(range(len(self._features)))
random.shuffle(num_samples)
num_train = round(len(num_samples) * self._train_factor)
self._train_indices = num_samples[:num_train]
self._test_indices = num_samples[num_train:]
random.shuffle(self._train_indices)
random.shuffle(self._test_indices)
self.prime_data()
def get_one_item(self, target_set=None):
""" Return exactly one feature/label pair as a tuple """
try:
if target_set == NNData.Set.TEST:
index = self._test_pool.popleft()
else:
index = self._train_pool.popleft()
return self._features[index], self._labels[index]
except IndexError:
return None
def number_of_samples(self, target_set=None):
""" Return total number of samples"""
if target_set is NNData.Set.TRAIN:
return len(self._train_pool)
elif target_set is NNData.Set.TEST:
return len(self._test_pool)
else:
return len(self._features)
def pool_is_empty(self, target_set=None):
""" This method returns True if the target_set deque (self._train_pool
or self._test_pool) is empty, or False otherwise. If target_set is
None, use the train pool.
"""
if target_set is NNData.Set.TEST:
return len(self._test_pool) == 0
else:
return len(self._train_pool) == 0
def prime_data(self, target_set=None, order=None):
"""Load one or both deques to be used as indirect indices """
if order is None:
order = NNData.Order.SEQUENTIAL
if target_set is not NNData.Set.TRAIN:
# this means we need to prime test
test_indices_temp = list(self._test_indices)
if order == NNData.Order.RANDOM:
random.shuffle(test_indices_temp)
self._test_pool = deque(test_indices_temp)
if target_set is not NNData.Set.TEST:
train_indices_temp = list(self._train_indices)
if order == NNData.Order.RANDOM:
random.shuffle(train_indices_temp)
self._train_pool = deque(train_indices_temp)
def load_xor():
""" Load the complete population of XOR examples. Note that the
nature of this set requires 100% to be placed in training.
"""
xor_x = [[0, 0], [1, 0], [0, 1], [1, 1]]
xor_y = [[0], [1], [1], [0]]
xor_array = NNData(xor_x, xor_y, 1)
return xor_array
class LayerType(Enum):
INPUT = 0
HIDDEN = 1
OUTPUT = 2
class MultiLinkNode(ABC):
class Side(Enum):
UPSTREAM = 0
DOWNSTREAM = 1
def __init__(self):
self._reporting_nodes = {MultiLinkNode.Side.UPSTREAM: 0,
MultiLinkNode.Side.DOWNSTREAM: 0}
self._reference_value = {MultiLinkNode.Side.UPSTREAM: 0,
MultiLinkNode.Side.DOWNSTREAM: 0}
self._neighbors = {MultiLinkNode.Side.UPSTREAM: [],
MultiLinkNode.Side.DOWNSTREAM: []}
def __str__(self):
ret_str = "-->Node " + str(id(self)) + "\n"
ret_str = ret_str + " Input Nodes:\n"
for key in self._neighbors[MultiLinkNode.Side.UPSTREAM]:
ret_str = ret_str + " " + str(id(key)) + "\n"
ret_str = ret_str + " Output Nodes\n"
for key in self._neighbors[MultiLinkNode.Side.DOWNSTREAM]:
ret_str = ret_str + " " + str(id(key)) + "\n"
return ret_str
@abstractmethod
def _process_new_neighbor(self, nodes: list, side: Side):
raise NotImplementedError("This method must be implemented "
"by a subclass")
def reset_neighbors(self, nodes: list, side: Side):
self._neighbors[side] = nodes.copy()
for node in nodes:
self._process_new_neighbor(node, side)
self._reference_value[side] = (1 << len(nodes)) - 1
self._reporting_nodes[side] = 0
class Neurode(MultiLinkNode):
def __init__(self, node_type, learning_rate=.05):
super().__init__()
self._value = 0
self._node_type = node_type
self._learning_rate = learning_rate
self._weights = dict()
@property
def value(self):
return self._value
@property
def node_type(self):
return self._node_type
@property
def learning_rate(self):
return self._learning_rate
@learning_rate.setter
def learning_rate(self, value):
self._learning_rate = value
def _process_new_neighbor(self, node, side):
if side == MultiLinkNode.Side.UPSTREAM:
self._weights[node] = random.uniform(0, 1)
def _check_in(self, node, side):
node_index = self._neighbors[side].index(node)
self._reporting_nodes[side] |= (1 << node_index)
if self._reporting_nodes[side] == self._reference_value[side]:
self._reporting_nodes[side] = 0
return True
else:
return False
def get_weight(self, node):
return self._weights[node]
class FFNeurode(Neurode):
def __init__(self, my_type):
self.my_type = my_type
super().__init__(my_type)
@staticmethod
def _sigmoid(value):
return 1 / (1 + np.exp(-value))
def _calculate_value(self):
weighted_sum = 0
for neighbor in self._neighbors[MultiLinkNode.Side.UPSTREAM]:
weighted_sum += neighbor.value * self._weights[neighbor]
self._value = self._sigmoid(weighted_sum)
def _fire_downstream(self):
for downstream_neighbor in \
self._neighbors[MultiLinkNode.Side.DOWNSTREAM]:
downstream_neighbor.data_ready_upstream(self)
def data_ready_upstream(self, node):
if self._check_in(node, MultiLinkNode.Side.UPSTREAM):
self._calculate_value()
self._fire_downstream()
def set_input(self, input_value):
self._value = input_value
self._fire_downstream()
class BPNeurode(Neurode):
def __init__(self, my_type):
super().__init__(my_type)
self._delta = 0
@property
def delta(self):
return self._delta
@delta.setter
def delta(self, value):
self._delta = value
@staticmethod
def _sigmoid_derivative(value):
return value * (1 - value)
def _calculate_delta(self, expected_value=None):
if self._node_type == LayerType.OUTPUT:
self._delta = (expected_value - self.value) * \
(self._sigmoid_derivative(self.value))
else:
if self._node_type == LayerType.HIDDEN:
delta_sum = 0
for neighbor in self._neighbors[MultiLinkNode.Side.DOWNSTREAM]:
delta_sum += neighbor.delta * neighbor.get_weight(self)
self._delta = delta_sum * self._sigmoid_derivative(self.value)
def data_ready_downstream(self, node):
if self._check_in(node, MultiLinkNode.Side.DOWNSTREAM):
self._calculate_delta()
self._fire_upstream()
self._update_weights()
def set_expected(self, expected_value):
self._calculate_delta(expected_value)
for neighbor in self._neighbors[MultiLinkNode.Side.UPSTREAM]:
neighbor.data_ready_downstream(self)
def adjust_weights(self, node, adjustment):
self._weights[node] += adjustment
def _update_weights(self):
for neighbor in self._neighbors[MultiLinkNode.Side.DOWNSTREAM]:
adjustment = self._learning_rate * neighbor.delta * self.value
neighbor.adjust_weights(self, adjustment)
def _fire_upstream(self):
for upstream_neighbor in \
self._neighbors[MultiLinkNode.Side.UPSTREAM]:
upstream_neighbor.data_ready_downstream(self)
class FFBPNeurode(FFNeurode, BPNeurode):
pass
class DLLNode:
""" Node class for a DoublyLinkedList - not designed for
general clients, so no accessors or exception raising """
def __init__(self, data=None):
self.prev = None
self.next = None
self.data = data
class DoublyLinkedList:
# Behavior of Current:
# Make current = head when first item added
# Make current = next item if current deleted. If next item doesn't
# exist, make current = previous item.
class EmptyListError(Exception):
pass
def __init__(self):
self._head = None
self._tail = None
self._current = None
def __iter__(self):
self._curr_iter = self._head
return self
def __next__(self):
if self._curr_iter is None:
raise StopIteration
ret_val = self._curr_iter.data
self._curr_iter = self._curr_iter.next
return ret_val
def move_forward(self):
if not self._current:
raise DoublyLinkedList.EmptyListError
if self._current.next:
self._current = self._current.next
else:
raise IndexError
def move_back(self):
if not self._current:
raise DoublyLinkedList.EmptyListError
if self._current.prev:
self._current = self._current.prev
else:
raise IndexError
def add_to_head(self, data):
new_node = DLLNode(data)
new_node.next = self._head
if self._head:
self._head.prev = new_node
self._head = new_node
if self._tail is None:
self._tail = new_node
self.reset_to_head()
def remove_from_head(self):
if not self._head:
raise DoublyLinkedList.EmptyListError
ret_val = self._head.data
self._head = self._head.next
if self._head:
self._head.prev = None
else:
self._tail = None
self.reset_to_head()
return ret_val
def add_after_cur(self, data):
if not self._current:
raise DoublyLinkedList.EmptyListError
new_node = DLLNode(data)
new_node.prev = self._current
new_node.next = self._current.next
if self._current.next:
self._current.next.prev = new_node
self._current.next = new_node
if self._tail == self._current:
self._tail = new_node
def remove_after_cur(self):
if not self._current:
raise DoublyLinkedList.EmptyListError
if self._current == self._tail:
raise IndexError
ret_val = self._current.next.data
if self._current.next == self._tail:
self._tail = self._current
self._current.next = None
else:
self._current.next = self._current.next.next
self._current.next.prev = self._current
return ret_val
def reset_to_head(self):
if not self._head:
raise DoublyLinkedList.EmptyListError
self._current = self._head
def reset_to_tail(self):
if not self._tail:
raise DoublyLinkedList.EmptyListError
self._current = self._tail
def get_current_data(self):
if not self._current:
raise DoublyLinkedList.EmptyListError
return self._current.data
class LayerList(DoublyLinkedList):
def _link_with_next(self):
""" Link the neurodes in the current node with those in the
next node bidirectionally using the reset_neighbors method """
for node in self._current.data:
node.reset_neighbors(self._current.next.data, Neurode.Side.
DOWNSTREAM)
for node in self._current.next.data:
node.reset_neighbors(self._current.data, Neurode.Side.UPSTREAM)
def __init__(self, inputs: int, outputs: int, neurode_type: type(Neurode)):
super().__init__()
self._neurode_type = neurode_type
if inputs < 1 or outputs < 1:
raise ValueError
input_layer = [self._neurode_type(LayerType.INPUT) for _ in
range(inputs)]
output_layer = [self._neurode_type(LayerType.OUTPUT) for _ in
range(outputs)]
self.add_to_head(input_layer)
self.add_after_cur(output_layer)
self._link_with_next()
def add_layer(self, num_nodes):
""" Add a hidden layer node after self._current with num_nodes
neurodes. Do not allow an insertion after self._tail.
Link the new neurodes with their neighbors.
"""
if self._current == self._tail:
raise IndexError
hidden_layer = [self._neurode_type(LayerType.HIDDEN) for _ in
range(num_nodes)]
self.add_after_cur(hidden_layer)
self._link_with_next()
self.move_forward()
self._link_with_next()
self.move_back()
def remove_layer(self):
""" Remove the hidden layer after self._current and relink the
neurodes in the surrounding nodes. Do not allow self._tail to
be removed.
"""
if self._current == self._tail or self._current.next == self._tail:
raise IndexError
self.remove_after_cur()
self._link_with_next()
@property
def input_nodes(self):
return self._head.data
@property
def output_nodes(self):
return self._tail.data
class EmptySetException(Exception):
pass
class FFBPNetwork:
def __init__(self, num_inputs: int, num_outputs: int):
self._num_inputs = num_inputs
self._num_outputs = num_outputs
self._layer_list = LayerList(num_inputs, num_outputs, FFBPNeurode)
self._input_nodes = self._layer_list.input_nodes
self._output_nodes = self._layer_list.output_nodes
def add_hidden_layer(self, num_nodes: int, position=0):
if position < 0:
raise ValueError
self._layer_list.reset_to_head()
for i in range(position):
self._layer_list.move_forward()
if self._layer_list._current == self._layer_list._tail:
raise IndexError
self._layer_list.add_layer(num_nodes)
self._layer_list.reset_to_head()
def train(self, data_set: NNData, epochs=1000, verbosity=2,
order=NNData.Order.RANDOM):
if data_set.pool_is_empty(NNData.Set.TRAIN):
raise EmptySetException
for epoch in range(epochs):
data_set.prime_data(order)
total_error = 0.0
num_samples = 0
while not data_set.pool_is_empty(NNData.Set.TRAIN):
feature, label = data_set.get_one_item(NNData.Set.TRAIN)
for i, item in enumerate(feature):
self._input_nodes[i].set_input(item)
predicted_output = [output_node.value for output_node in
self._output_nodes]
error = [predicted - expected for predicted, expected in
zip(predicted_output, label)]
total_error += sum([e ** 2 for e in error])
num_samples += 1
for i, item in enumerate(label):
self._output_nodes[i].set_expected(item)
if epoch % 1000 == 0 and verbosity > 1:
print(f"Sample {feature} expected {label} produced "
f"{predicted_output}")
rmse = math.sqrt(total_error / num_samples)
if epoch % 100 == 0 and verbosity > 0:
print(f"Epoch {epoch} RMSE: {rmse:.3f}")
def test(self, data_set: NNData, order=NNData.Order.SEQUENTIAL):
if data_set.pool_is_empty(NNData.Set.TEST):
raise EmptySetException
data_set.prime_data(order)
total_error = 0.0
num_samples = 0
while not data_set.pool_is_empty(NNData.Set.TEST):
feature, label = data_set.get_one_item(NNData.Set.TEST)
for i, item in enumerate(feature):
self._input_nodes[i].set_input(item)
predicted_output = [output_node.value for output_node in
self._output_nodes]
error = [predicted - expected for predicted, expected in
zip(predicted_output, label)]
total_error += sum([e ** 2 for e in error])
num_samples += 1
if num_samples % 1000 == 0:
print(f"Sample {feature} expected {label} produced "
f"{predicted_output}")
rmse = math.sqrt(total_error / num_samples)
print(f"Test RMSE: {rmse}")
def run_iris():
network = FFBPNetwork(4, 3)
network.add_hidden_layer(3)
Iris_X = [[5.1, 3.5, 1.4, 0.2], [4.9, 3, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5, 3.6, 1.4, 0.2], [5.4, 3.9, 1.7, 0.4], [4.6, 3.4, 1.4, 0.3],
[5, 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2], [4.9, 3.1, 1.5, 0.1], [5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3, 1.4, 0.1], [4.3, 3, 1.1, 0.1], [5.8, 4, 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4], [5.1, 3.5, 1.4, 0.3], [5.7, 3.8, 1.7, 0.3],
[5.1, 3.8, 1.5, 0.3],
[5.4, 3.4, 1.7, 0.2], [5.1, 3.7, 1.5, 0.4], [4.6, 3.6, 1, 0.2],
[5.1, 3.3, 1.7, 0.5],
[4.8, 3.4, 1.9, 0.2], [5, 3, 1.6, 0.2], [5, 3.4, 1.6, 0.4],
[5.2, 3.5, 1.5, 0.2],
[5.2, 3.4, 1.4, 0.2], [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2],
[5.4, 3.4, 1.5, 0.4],
[5.2, 4.1, 1.5, 0.1], [5.5, 4.2, 1.4, 0.2], [4.9, 3.1, 1.5, 0.1],
[5, 3.2, 1.2, 0.2],
[5.5, 3.5, 1.3, 0.2], [4.9, 3.1, 1.5, 0.1], [4.4, 3, 1.3, 0.2],
[5.1, 3.4, 1.5, 0.2],
[5, 3.5, 1.3, 0.3], [4.5, 2.3, 1.3, 0.3], [4.4, 3.2, 1.3, 0.2],
[5, 3.5, 1.6, 0.6],
[5.1, 3.8, 1.9, 0.4], [4.8, 3, 1.4, 0.3], [5.1, 3.8, 1.6, 0.2],
[4.6, 3.2, 1.4, 0.2],
[5.3, 3.7, 1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, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [1, 0, 0, ],
[1, 0, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ],
[0, 1, 0, ], [0, 1, 0, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ],
[0, 0, 1, ], [0, 0, 1, ], [0, 0, 1, ]]
data = NNData(Iris_X, Iris_Y, .7)
network.train(data, 10001, order=NNData.Order.RANDOM)
network.test(data)
def run_sin():
network = FFBPNetwork(1, 1)
network.add_hidden_layer(3)
sin_X = [[0], [0.01], [0.02], [0.03], [0.04], [0.05], [0.06], [0.07], [0.08], [0.09], [0.1], [0.11], [0.12],
[0.13], [0.14], [0.15], [0.16], [0.17], [0.18], [0.19], [0.2], [0.21], [0.22], [0.23], [0.24], [0.25],
[0.26], [0.27], [0.28], [0.29], [0.3], [0.31], [0.32], [0.33], [0.34], [0.35], [0.36], [0.37], [0.38],
[0.39], [0.4], [0.41], [0.42], [0.43], [0.44], [0.45], [0.46], [0.47], [0.48], [0.49], [0.5], [0.51],
[0.52], [0.53], [0.54], [0.55], [0.56], [0.57], [0.58], [0.59], [0.6], [0.61], [0.62], [0.63], [0.64],
[0.65], [0.66], [0.67], [0.68], [0.69], [0.7], [0.71], [0.72], [0.73], [0.74], [0.75], [0.76], [0.77],
[0.78], [0.79], [0.8], [0.81], [0.82], [0.83], [0.84], [0.85], [0.86], [0.87], [0.88], [0.89], [0.9],
[0.91], [0.92], [0.93], [0.94], [0.95], [0.96], [0.97], [0.98], [0.99], [1], [1.01], [1.02], [1.03],
[1.04], [1.05], [1.06], [1.07], [1.08], [1.09], [1.1], [1.11], [1.12], [1.13], [1.14], [1.15], [1.16],
[1.17], [1.18], [1.19], [1.2], [1.21], [1.22], [1.23], [1.24], [1.25], [1.26], [1.27], [1.28], [1.29],
[1.3], [1.31], [1.32], [1.33], [1.34], [1.35], [1.36], [1.37], [1.38], [1.39], [1.4], [1.41], [1.42],
[1.43], [1.44], [1.45], [1.46], [1.47], [1.48], [1.49], [1.5], [1.51], [1.52], [1.53], [1.54], [1.55],
[1.56], [1.57]]
sin_Y = [[0], [0.00999983333416666], [0.0199986666933331], [0.0299955002024957], [0.0399893341866342],
[0.0499791692706783], [0.0599640064794446], [0.0699428473375328], [0.0799146939691727],
[0.089878549198011], [0.0998334166468282], [0.109778300837175], [0.119712207288919],
[0.129634142619695], [0.139543114644236], [0.149438132473599], [0.159318206614246],
[0.169182349066996], [0.179029573425824], [0.188858894976501], [0.198669330795061], [0.2084598998461],
[0.218229623080869], [0.227977523535188], [0.237702626427135], [0.247403959254523],
[0.257080551892155], [0.266731436688831], [0.276355648564114], [0.285952225104836], [0.29552020666134],
[0.305058636443443], [0.314566560616118], [0.324043028394868], [0.333487092140814],
[0.342897807455451], [0.35227423327509], [0.361615431964962], [0.370920469412983], [0.380188415123161],
[0.389418342308651], [0.398609327984423], [0.40776045305957], [0.416870802429211], [0.425939465066],
[0.43496553411123], [0.44394810696552], [0.452886285379068], [0.461779175541483], [0.470625888171158],
[0.479425538604203], [0.488177246882907], [0.496880137843737], [0.505533341204847],
[0.514135991653113], [0.522687228930659], [0.531186197920883], [0.539632048733969],
[0.548023936791874], [0.556361022912784], [0.564642473395035], [0.572867460100481],
[0.581035160537305], [0.58914475794227], [0.597195441362392], [0.60518640573604], [0.613116851973434],
[0.62098598703656], [0.628793024018469], [0.636537182221968], [0.644217687237691], [0.651833771021537],
[0.659384671971473], [0.666869635003698], [0.674287911628145], [0.681638760023334],
[0.688921445110551], [0.696135238627357], [0.70327941920041], [0.710353272417608], [0.717356090899523],
[0.724287174370143], [0.731145829726896], [0.737931371109963], [0.744643119970859],
[0.751280405140293], [0.757842562895277], [0.764328937025505], [0.770738878898969],
[0.777071747526824], [0.783326909627483], [0.78950373968995], [0.795601620036366], [0.801619940883777],
[0.807558100405114], [0.813415504789374], [0.819191568300998], [0.82488571333845], [0.83049737049197],
[0.836025978600521], [0.841470984807897], [0.846831844618015], [0.852108021949363],
[0.857298989188603], [0.862404227243338], [0.867423225594017], [0.872355482344986],
[0.877200504274682], [0.881957806884948], [0.886626914449487], [0.891207360061435],
[0.895698685680048], [0.900100442176505], [0.904412189378826], [0.908633496115883],
[0.912763940260521], [0.916803108771767], [0.920750597736136], [0.92460601240802], [0.928368967249167],
[0.932039085967226], [0.935616001553386], [0.939099356319068], [0.942488801931697],
[0.945783999449539], [0.948984619355586], [0.952090341590516], [0.955100855584692],
[0.958015860289225], [0.960835064206073], [0.963558185417193], [0.966184951612734],
[0.968715100118265], [0.971148377921045], [0.973484541695319], [0.975723357826659],
[0.977864602435316], [0.979908061398614], [0.98185353037236], [0.983700814811277], [0.98544972998846],
[0.98710010101385], [0.98865176285172], [0.990104560337178], [0.991458348191686], [0.992712991037588],
[0.993868363411645], [0.994924349777581], [0.99588084453764], [0.996737752043143], [0.997494986604054],
[0.998152472497548], [0.998710143975583], [0.999167945271476], [0.999525830605479],
[0.999783764189357], [0.999941720229966], [0.999999682931835]]
data = NNData(sin_X, sin_Y, .1)
network.train(data, 10001, order=NNData.Order.RANDOM)
network.test(data)
def run_XOR():
network = FFBPNetwork(2, 1)
xor_data = load_xor()
xor_data.split_set(new_train_factor=0.8)
network.train(xor_data, epochs=10000, verbosity=0)
network.test(xor_data)
if __name__ == "__main__":
run_iris()
run_sin()
run_XOR()