-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbol_memory.py
executable file
·160 lines (130 loc) · 5.71 KB
/
symbol_memory.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
##########################################################################
#
# Auteur : Nils Schaetti <nils.schaetti@unine.ch>
# Date : 19.04.2015 17:59:05
# Lieu : Nyon, Suisse
#
# Fichier sous licence GNU GPL
#
###########################################################################
import Oger
from tools.symbol_memory_generator import SymbolMemoryGenerator
from tools.metrics import remembering_rate, equal_output, lucidity, MSSR
from tools.discrete_symbol_node import DiscreteSymbolNode
import mdp
import os
import cPickle
import sys
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as cm
import numpy as np
#########################################################################
#
# Propriétés de l'expérience
#
#########################################################################
# Propriétés du réservoir
rc_SpectralRadius = 0.99 # Spectral radius
rc_Size = 500 # Taille du réservoir
rc_InputScaling = 0.5 # Dimensionnement des entrées
rc_LeakRate = 1.0 # Leak rate
# Propriétés du jeu de données
rc_DatasetSize = 40 # Longueur du jeu de données
rc_TrainingLength = 30 # Longueur d'entrainement
rc_TestLength = rc_DatasetSize - rc_TrainingLength # Longeur de test
rc_MemoryShiffting = 4
rc_SampleLength = 1000
rc_NbSymbols = np.arange(5,201,10)
rc_NbSamples = 10
####################################################
# Fonction principale
####################################################
if __name__ == "__main__":
# File destination
if len(sys.argv) >= 2:
file_output = sys.argv[1]
else:
file_output = ""
# Generate the dataset
generator = SymbolMemoryGenerator()
# Table of results for each values
mssres = []
# Reservoir
#reservoir = Oger.nodes.LeakyReservoirNode(input_dim = rc_NbSymbols, output_dim = rc_Size, input_scaling = rc_InputScaling, leak_rate = rc_LeakRate)
#symbol_out = DiscreteSymbolNode(input_dim = rc_NbSymbols, output_dim = rc_NbSymbols)
# Create the flow
#flow = mdp.Flow([reservoir, readout, symbol_out], verbose=1)
# Varying a property
for nb_symbols in rc_NbSymbols:
# Multiple sample
sample = []
for n in np.arange(0,rc_NbSamples):
# Reservoir
readout = Oger.nodes.RidgeRegressionNode()
reservoir = Oger.nodes.LeakyReservoirNode(input_dim = nb_symbols, output_dim = rc_Size, input_scaling = rc_InputScaling, leak_rate = rc_LeakRate)
#readout = Oger.nodes.RidgeRegressionNode()
symbol_out = DiscreteSymbolNode(input_dim = nb_symbols, output_dim = nb_symbols)
# Create the flow
flow = mdp.Flow([reservoir, readout, symbol_out], verbose=1)
# Generating data to learn
inputs, outputs = generator.generateDataset(n_symbols = nb_symbols, memory_shiffting = rc_MemoryShiffting, sample_length = rc_SampleLength, n_samples = rc_DatasetSize)
# Training and test
training_in, training_out = inputs[0:rc_TrainingLength], outputs[0:rc_TrainingLength]
test_in, test_out = inputs[rc_TrainingLength:], outputs[rc_TrainingLength:]
# Reservoir input data
data = [training_in, zip(training_in, training_out), None]
# Train
flow.train(data)
# For each test set
t = 0
mssr = []
for test_inputs in test_in:
# Give to reservoir
test_outputs = flow(test_inputs)
# Remove first outputs that are not linked
# to a previous symbol entered
test_outputs = test_outputs[rc_MemoryShiffting:]
targets = test_out[t][rc_MemoryShiffting:]
# Print MSSR
mssr += [MSSR(test_outputs, targets)]
#print("MSSR : " + str(MSSR(test_outputs, targets) * 100.0))
# Compare y and y_
"""for i in range(rc_SampleLength - rc_MemoryShiffting):
print(str(targets[i]) + " ---- " + str(test_outputs[i]))
"""
t += 1
# Add result
sample += [np.average(mssr) * 100.0]
#print("MSSR for size " + str(size) + " : " + str(np.average(mssr) * 100.0))
# Add to all samples
mssres += [sample]
# Put into average and std
mssr_averages = []
mssr_errors = []
for mssr_sample in mssres:
mssr_averages += [np.average(mssr_sample)]
mssr_errors += [np.std(mssr_sample)]
# Display the graph
#plt.plot(mssres)
#plt.set_title("MSSR vs nb. of symbols")
plt.errorbar(rc_NbSymbols, mssr_averages, yerr = mssr_errors, fmt='-o')
#plt.plot(rc_NbSymbols, np.full((len(mssres)), 1.0 / float(rc_NbSymbols) * 100.0))
bottom_values = []
for nb_symbols in rc_NbSymbols:
bottom_values += [1.0 / float(nb_symbols) * 100.0]
plt.plot(rc_NbSymbols, bottom_values)
plt.show()
# Save to file
if file_output != "":
f = open(file_output, 'w')
f.write("Averages : ")
for a in mssr_averages:
f.write(str(a) + ",")
f.write("\n")
f.write("Errors : ")
for e in mssr_errors:
f.write(str(e) + ",")
f.close()