-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathself_organizing_network.py
47 lines (40 loc) · 1.12 KB
/
self_organizing_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
from hsom import SelfOrganizingNetwork
from random import sample
input_size = 100
layer_sizes = [20, 15, 10, 5, 2, 1]
input_percents = [0.2, 0.2, 0.2, 0.2, 0.75, 1.0]
learning_rate = 0.05
boost_factor = 1
node_count = 5
winner_count = 1
initial_range = (-0.5, 0.5)
# Create hierarchical layers of size 20, 15, 10, 5, 2, and 1
self_organizing_network = SelfOrganizingNetwork(
input_size=input_size,
layer_sizes=layer_sizes,
input_percents=input_percents,
learning_rates=learning_rate,
boost_factors=boost_factor,
node_counts=node_count,
winner_counts=winner_count,
initial_ranges=initial_range)
# Create a set of sparse samples
samples = []
for i in range(node_count):
X = [0 for j in range(input_size)]
I = [j for j in range(input_size)]
for j in sample(I, int(input_size * 0.2)):
X[j] = 1
samples.append(X)
for i in range(200):
self_organizing_network.train(samples)
outputs = self_organizing_network.test(samples)
for i in range(len(outputs)):
print(outputs[i])
# TEST RESULTS
#
# [0, 0, 0, 1, 0]
# [0, 1, 0, 0, 0]
# [0, 0, 0, 0, 1]
# [0, 0, 1, 0, 0]
# [1, 0, 0, 0, 0]