This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
/
PyESN Trial.py
95 lines (73 loc) · 2.61 KB
/
PyESN Trial.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
import numpy as np
from pyESN import ESN
#Set Seed
rng = np.random.RandomState(42)
def sigmoid(a):
"""Sigmoid (logistic) firing function."""
return 1/(1 + np.exp(-a))
def dsigmoid(b):
"""Derivative of sigmoid firing function b=fire(a), as function of b."""
# Used in the backpropagation algorithm to compute error signals at hidden layers.
return b*(1-b)
def invsigmoid(a):
return np.log(a/(1-a))
def generateParity(length, parity):
u = np.random.randint(2, size=length).reshape(length,1)
y = []
currentParity = 0
for i in range(length):
if parity == 2:
parityState = np.array([0])
else:
parityState = np.zeros(parity-1)
currentU = u[i]
currentParity = (currentParity + currentU) % parity
parityState = np.zeros((1, parity-1))
if currentParity != 0:
parityState[:,currentParity-1] = 1
parityState = parityState
y.append(parityState)
return(u, np.array(y).reshape(length, parity-1))
def bounder(x):
return(1 if x >= 0.5 else 0)
#Create Data
lengthTrain = 1000
lengthTest = 1000
parity = 3
input_shift = [0]
(u,y) = generateParity(lengthTrain, parity)
#Create network
esn = ESN(n_inputs = 1,
n_outputs = parity - 1,
n_reservoir = 300,
# spectral_radius = 0.25,
sparsity = 0.9,
noise = 0.01,
# input_shift = input_shift,
# input_scaling = [0.01],
# teacher_scaling = 1.12,
teacher_shift = -0.7,
out_activation = np.tanh,
inverse_out_activation = np.arctanh,
random_state = rng,
silent = False)
print('fitting')
pred_train = esn.fit(u, y)
#Assess Training Error
vec_bounder = np.vectorize(bounder)
bounded = vec_bounder(pred_train)
#print(bounded)
#print('\n', y.reshape(lengthTrain, parity-1))
boundedErrors = np.abs(bounded - y.reshape(lengthTrain, parity-1))
numWrong = sum([1 if xi != 0 else 0 for xi in np.sum(boundedErrors, axis = 1)])
print('Number Wrong (Training):', numWrong, 'Out of', lengthTrain, '\n')
#Testing
(uTest,yTest) = generateParity(lengthTest, parity)
pred_test = esn.predict(uTest)
print('Testing MSE:',np.sqrt(np.mean((pred_test - yTest)**2)))
boundedTest = vec_bounder(pred_test)
boundedTestErrors = np.abs(boundedTest - yTest.reshape(lengthTest, parity-1))
numWrongTest = sum([1 if xi != 0 else 0 for xi in np.sum(boundedTestErrors, axis = 1)])
#print(boundedTest)
#print('\n', yTest.reshape(lengthTest, parity-1))
print('Number Wrong (Testing):', numWrongTest, 'Out of', lengthTest, '\n')