-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualize_rbn.py
227 lines (167 loc) · 6.45 KB
/
visualize_rbn.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
import matplotlib.pyplot as plt
import networkx as nx
from utils import get_working_dir, glob_load, user_denies, user_confirms
import numpy as np
import json
from itertools import repeat
import mdp
import log
import logging
import re
def visualize_rbn(rbn):
internal_edges = []
for i, neighbors in enumerate(rbn.connections):
internal_edges += zip(repeat(i), neighbors)
input_edges = zip(repeat('input_node'), rbn.input_connections)
print input_edges
print internal_edges
G = nx.MultiGraph()
pos = nx.spring_layout(G)
#G.add_edges_from(internal_edges, node_color='r', node_size=10)
#G.add_edges_from(input_edges, node_color='b', node_size=3)
nx.draw_networkx_nodes(G, pos,
nodelist=range(rbn.n_nodes),
node_color='b',
node_size=500,
alpha=0.8)
nx.draw_networkx_edges(G, pos,
edgelist=internal_edges)
#width=3,
#edge_color='r')
#nx.draw_networkx_edges(G,pos,
#edgelist=[(0,1),(1,2),(2,3),(3,0)],
#width=8,alpha=0.5,edge_color='r')
#nx.draw(G, pos)
plt.show()
def visualize_dataset(n=30, working_dir=None):
if not working_dir:
working_dir = get_working_dir()
test_dataset, filename = glob_load(working_dir + '*-dataset')[0]
dataset_meta = re.search(r"\[(.*)\]", filename).groups()[0]
reservoir_input = np.transpose(test_dataset[0][:n])
expected_output = np.transpose(test_dataset[1][:n])
plt.matshow(reservoir_input, cmap=plt.cm.gray)
plt.axis('off')
plt.savefig('plots/' + dataset_meta + '-input.pdf', bbox_inches='tight')
plt.matshow(expected_output, cmap=plt.cm.gray)
plt.axis('off')
plt.savefig('plots/' + dataset_meta + '-output.pdf', bbox_inches='tight')
plt.show()
def visualize_correctness(n=25, working_dir=None):
if not working_dir:
working_dir = get_working_dir()
(reservoir_input, expected_output), _ =\
glob_load(working_dir + '*-dataset')[0]
rbn_reservoir, _ = glob_load(working_dir + '*-reservoir')[0]
readout, _ = glob_load(working_dir + '*-readout')[0]
rbn_reservoir.reset_state()
flow = mdp.Flow([rbn_reservoir, readout], verbose=1)
actual_output = flow.execute(reservoir_input)
for output in actual_output:
output[0] = 1 if output[0] > 0.5 else 0
errors = sum(actual_output != expected_output)
accuracy = 1 - float(errors) / len(actual_output)
plt.title('Reservoir performance')
plt.plot(actual_output[:n], 'y', linewidth=1.5)
plt.plot(expected_output[:n], 'b', linewidth=1.5)
plt.legend(['Actual output', 'Expected output'])
plt.savefig('temp-2.pdf', bbox_inches='tight')
#def plot_fitness():
# with open('state.dat', 'r') as states:
# plots = []
# for line in states.readlines():
# state = json.loads(line)
# print state['generation']
# #print line
# # 'children': map(lambda x: x.serialize(), children),
# # 'adults': map(lambda x: x.serialize(), adults),
# # 'generation': generation,
# # 'time': arrow.utcnow().isoformat(),
#visualize_dataset()
def visualize_rbn_state(n=100, working_dir=None):
if not working_dir:
working_dir = get_working_dir()
rbn, rbn_name = glob_load(working_dir + '*-reservoir')[0]
rbn.reset_state()
if not user_denies('Perturb?'):
test_data, _ = glob_load(working_dir + '*-dataset')[0]
test_input, _ = test_data
test_input = test_input[:n]
else:
test_input = np.zeros((n, 1))
rbn.should_perturb = False
rbn_states = rbn._execute(test_input)
plt.matshow(rbn_states, cmap=plt.cm.gray)
plt.axis('off')
#plt.xlabel('State of node n in RBN')
#plt.gca().xaxis.set_label_position('top')
#plt.ylabel('Time')
plt.savefig(raw_input('Name: '), bbox_inches='tight')
#plt.show()
#plt.matshow(test_input, cmap=plt.cm.gray)
#plt.title('Reservoir input')
#input_connections = np.zeros((1, rbn.n_nodes))
#input_connections[0, rbn.input_connections] = 1
#plt.matshow(input_connections, cmap=plt.cm.gray)
#plt.title('Input connections')
#plt.show()
if __name__ == '__main__':
log.setup(logging.DEBUG)
visualize_rbn_state()
import sys
sys.exit()
from rbn import rbn_node
rbn_reservoir_ordered = rbn_node.RBNNode(
connectivity=2,
should_perturb=False,
output_dim=30,
input_connectivity=15)
#rbn_reservoir_critical = rbn_node.RBNNode(
# connectivity=2,
# should_perturb=False,
# output_dim=30,
# input_connectivity=15)
#rbn_reservoir_chaotic = rbn_node.RBNNode(
# connectivity=4,
# should_perturb=False,
# output_dim=30,
# input_connectivity=15)
test_input = np.zeros((60, 1))
ordered = rbn_reservoir_ordered._execute(test_input)
#critical= rbn_reservoir_critical._execute(test_input)
#chaotic = rbn_reservoir_chaotic._execute(test_input)
plt.matshow(ordered, cmap=plt.cm.gray)
plt.axis('off')
plt.savefig('plots/critical-phase-new.pdf', bbox_inches='tight')
#plt.matshow(critical, cmap=plt.cm.gray)
#plt.axis('off')
#plt.savefig('plots/critical-phase.pdf', bbox_inches='tight')
#plt.matshow(chaotic, cmap=plt.cm.gray)
#plt.axis('off')
#plt.savefig('plots/chaotic-phase.pdf', bbox_inches='tight')
#if user_confirms('Visualize rbn state?'):
#visualize_rbn_state()
#if user_confirms('Visualize dataset?'):
# visualize_dataset()
#visualize_correctness()
# plot_fitness()
# import sys
# sys.exit()
#
# # Create datasets
# dataset_type = default_input('Dataset [temporal_parity, temporal_density]',
# 'temporal_parity')
# n_datasets = default_input('Datasets', 10)
# task_size = default_input('Dataset length', 200)
# window_size = default_input('Window size', 3)
#
# datasets = temporal.create_datasets(
# n_datasets,
# task_size=task_size,
# window_size=window_size,
# dataset_type=dataset_type)
# training_dataset, test_dataset = datasets[:-1], datasets[-1]
#
# dataset_description = '[{}-{}-{}-{}]'.format(
# dataset_type, n_datasets, task_size, window_size)
# #logging.info(dataset_description)