Commit fbffd128 authored by Ngocson's avatar Ngocson

cleaning

parent fe306096
This diff is collapsed.
import numpy as np
class LeakyEsn:
def __init__(s,
N,
Sp,
p,
a,
c,
dt,
Activation,
v,
inputScalling,
outputScalling,
Wmin,
Wmax):
s.N = N
s.Activation = Activation
s.v = v
s.p = p
s.sp = sp
s.a = a
s.c = c
s.dt = dt
s.outputScalling= outputScalling
s.inputScalling = inputScalling
s.Wmin = Wmin
s.Wmax = Wmax
s.states = np.zeros(s.N)
s.W = Ïnp.matrix( [ [(s.Wmax-s.Wmin)*np.random.rand() + s.Wmin for i in range(s.N)] for j in range(s.N)] )
while not s.damped(1e-4):
s.W = np.matrix( [ [(s.Wmax-s.Wmin)*np.random.rand() + s.Wmin for i in range(s.N)] for j in range(s.N)] )
pass
def damped(s,e)
s.states = np.Random.rand(s.N)
Y = steps(np.zeros((s.N,10*s.N)))
if np.sum((Y[-1]>e).astype(np.int))>0:
return False
return True
def steps(s,U,X):
pass
def step(s,U):
\ No newline at end of file
......@@ -29,7 +29,8 @@ class Reservoir:
Activation = np.tanh,
Feedback = True,
inputScaling = 1,
Verbose = False):
Verbose = False,
damping = True):
s.N = N
s.v = v
s.p = p
......@@ -39,8 +40,9 @@ class Reservoir:
s.Feedback = Feedback
s.Activation = Activation
s.initWeight()
while not s.damped():
s.initWeight()
if damping:
while not s.damped():
s.initWeight()
if Verbose:
print("W.std = ",s.W.std())
......@@ -110,7 +112,7 @@ class Reservoir:
s.step(u,x)
states.append(s.read(u))
return states
return np.array(states)
def fit(s,Y,U,Forced = False, Show=False):
......
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 14:23:42 2019
@author: ngocson
"""
from NARMA import *
from Reservoir import *
import numpy as np
import matplotlib.pyplot as plt
from random import *
global V
V = []
with open('ActivationFunction.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for row in csv_reader:
L = []
for value in row:
L.append(float(value))
V.append(L)
with open('santa_fe.txt') as sftr_file:
sftr_reader = csv.reader(sftr_file, delimiter=';')
santa_fe = np.array(next(sftr_reader)).astype(np.float)
santa_fe = santa_fe/santa_fe.max()
def approx_lin(x,vr=0.8,a=0.5,s=4.0,b=1.0):
global V
fi = min(max(20.0*vr,0.0),99.0)
if not fi.is_integer():
i = int(fi)
f = V[i]
else:
f = V[int(fi)]
fx = min(max(400.0*(b+x)*s,0.0),1999.0)
if not fx.is_integer():
x = int(fx)
return a*((x+1-fx)*(f[x]-f[x+1])+f[x+1])
else:
return a*f[int(fx)]
n_train, n_test, d_train, d_test = 2000,1000,200,202
Nessais = 10
Ntrain = 50
sp = 0.8
Narma10Generator = NARMA()
TrainError = []
TestError = []
for i in range(Ntrain):
R = Reservoir(N = 100,
p = 0.05,
sp = sp,
outputScaling = sp*0.5,
v = 0.0001,
Activation = np.vectorize(approx_lin),
Feedback = False,
inputScaling = 0.1,
damping = False
)
for j in range(Nessais):
Narma10Generator.reset(np.zeros(10),np.zeros(10))
U_train = (np.random.rand(d_train+n_train)*0.5)
U_test = (np.random.rand(d_test+n_test)*0.5)
Y_train = Narma10Generator.getTimeSerie(U_train)
epsi = R.fit(Y = Y_train[d_train:],
U = U_train[d_train:],
Forced = True,
Show = (i*Ntrain+j)%40==0
)
Narma10Generator.reset(np.zeros(10),np.zeros(10))
R.reset(Zeros=True)
Y_ = R.steps(U_test)[d_test:]
Y_test = Narma10Generator.getTimeSerie(U_test)[d_test:]
NRMSE = np.sqrt(((Y_-Y_test)**2).mean()/np.matrix(Y_test).std())
if (i*Ntrain+j)%40 ==0:
print(epsi,NRMSE)
TrainError.append(epsi)
TestError.append(NRMSE)
plt.plot(Y_[:100],'r')
plt.plot(Y_test[:100],'g')
plt.show()
plt.plot(Y_[-150:],'r')
plt.plot(Y_test[-150:],'g')
plt.show()
TrE = np.array(TrainError)
TsE = np.array(TestError)
print(TrE[~np.isnan(TrE)].mean(),TsE[~np.isnan(TsE)].mean())
print(TrE[~np.isnan(TrE)].max(),TsE[~np.isnan(TsE)].max())
print(TrE[~np.isnan(TrE)].min(),TsE[~np.isnan(TsE)].min())
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 23 20:21:40 2019
@author: Ngocson
"""
from fpaa_reservoir import *
with open('santa_fe.txt') as sftr_file:
sftr_reader = csv.reader(sftr_file, delimiter=';')
santa_fe = np.array(next(sftr_reader)).astype(np.float)
santa_fe = santa_fe/santa_fe.max()
N = 40
n_init = 2*N
n_tot = 3000
X = santa_fe[:-1]
Y = santa_fe[1:]
s = reservoirNMOS(N=N,
p = 5/N,
vr = 0.8,
a_max = 2.5,
a_min = 1/2.5,
b_max = 0.7,
input_scalling=10.0,
s_min = 0.0)
###################### LET THE FUN BEGIN ##################
s.steps(X[:n_init])
e = s.fit(Y[n_init:n_tot],X[n_init:n_tot],0.001)
print(e)
s.reset(True)
Y_ = s.steps(X)
plt.figure()
plt.title('After training')
plt.plot(Y[n_init:],'r-',label='teacher')
plt.plot(Y_[n_init:],'b-',label='outp')
plt.legend()
plt.show()
plt.figure()
print(np.sqrt(np.linalg.norm(Y_[n_init:]-Y[n_init:])/Y_[n_init:].std()))
s.reset(True)
plt.figure()
plt.title('Testing')
plt.plot(Y[-1000:],'r.-',label='teacher')
plt.plot(Y_[-1000:],'b.-',label='outp')
plt.legend()
plt.show()
s.responses_NODE(X[:500],range(N))
\ No newline at end of file
This diff is collapsed.
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 14:00:12 2019
@author: ngocson
"""
import numpy as np
class NARMA:
def __init__(s,n=10,alpha=0.3,beta=0.05,gamma=1.5,delta=0.1):
s.n = n
s.alpha = alpha
s.beta = beta
s.gamma = gamma
s.delta = delta
s.reset(np.zeros(n),(np.random.rand(n)-0.5)*0.2)
def reset(s,state,inputs):
s.state = state
s.inputs= inputs
def step(s,u):
yt = s.alpha*s.state[-1]\
+s.beta*s.state[-1]*s.state.sum()\
+s.gamma*s.inputs[0]*s.inputs[-1]\
+s.delta
s.inputs= np.append(s.inputs[1:],u)
s.state = np.append(s.state[1:],yt)
def steps(s,U):
for u in U:
s.step(u)
def read(s):
return s.state[-1]
def getTimeSerie(s,U):
Y = []
for u in U:
s.step(u)
Y.append(s.read())
return np.array(Y)
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 20:49:48 2019
@author: ngocson
"""
import numpy as np
from scipy import linalg
from random import random
import matplotlib.pyplot as plt
import csv
def write_matrix(matrix, filename = "matrice.csv"):
with open(filename, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
for row in matrix:
writer.writerow(row)
class Reservoir:
def __init__(s,
N=100,
p=0.2,
sp=0.8,
outputScaling = 1,
v=0,
Activation = np.tanh,
Feedback = True,
inputScaling = 1,
Verbose = False,
damping = True):
s.N = N
s.v = v
s.p = p
s.sp = sp
s.outputScaling = outputScaling
s.inputScaling = inputScaling
s.Feedback = Feedback
s.Activation = Activation
s.initWeight()
if damping:
while not s.damped():
s.initWeight()
if Verbose:
print("W.std = ",s.W.std())
print("Wi.std = ",s.Wi.std())
ei,_ = np.linalg.eig(s.W)
print("W.eig.mean = ",np.abs(ei).mean())
print("W.eig.std = ",np.abs(ei).std())
ei,_ = np.linalg.eig(s.Wi.T*s.Wi)
print("Wi.eig.mean = ",np.abs(ei).mean())
print("Wi.eig.std = ",np.abs(ei).std())
if Feedback:
ei,_ = np.linalg.eig(s.Wfb.T*s.Wfb)
print("Wfb.eig.mean = ",np.abs(ei).mean())
print("Wfb.eig.std = ",np.abs(ei).std())
def initWeight(s):
#s.W = np.matrix([np.array( [0 if random()> p else -0.4 if random() < 0.5 else 0.4 for i in range(N)]) for j in range(s.N)])
s.W = np.matrix([np.array([random()-0.5 if random()<s.p else 0 for i in range(s.N)]) for j in range(s.N)])
#s.W = s.W - np.mean(s.W)
eigen,_ = linalg.eig(s.W)
s.W = s.W*s.sp/max(eigen.real)
s.reset()
s.Wi = np.matrix( np.random.rand(s.N) * 2 - 1)*s.inputScaling
#s.Wi = np.matrix([1 if random() < 0.5 else -1 for i in range(N)])
if s.Feedback:
s.Wfb = s.outputScaling*np.matrix(np.random.rand(s.N) * 2 - 1)
s.Wo = np.matrix([1.0 for i in range(s.N)])
s.Wio = np.matrix([1.0])
s.Bi = 1.0
def read(s,U):
return (s.Wo*s.state.T+s.Wio*U)[0,0] + s.Bi
def reset(s,Zeros = False):
#s.state = np.matrix(np.zeros((1,s.N)))
if Zeros:
s.state = np.matrix([0 for i in range(s.N)])
else:
s.state = np.matrix([2*random()-1 for i in range(s.N)])
def step(s,U=0,X = None): #U is a scalar
preactivation = s.state*s.W+ s.Wi*U
if s.Feedback:
if np.array_equal(X,np.array(None)):
preactivation += s.Wfb*s.read(U)
else:
preactivation += s.Wfb*X
s.state = s.Activation(preactivation) + s.v*(np.random.rand(s.N)-0.5)
return s.state
def steps(s,U, X = np.array(None)):
states = []
if np.array_equal(X,np.array(None)):
for u in U:
s.step(u)
states.append(s.read(u))
else:
for u,x in zip(U,X):
s.step(u,x)
states.append(s.read(u))
return np.array(states)
def fit(s,Y,U,Forced = False, Show=False):
s.states_over_time = np.zeros((s.N+2,len(U)))
if s.Feedback:
s.step(U[0],Y[0])
s.states_over_time[:s.N+1,0] = np.concatenate((s.state.T,[[1.0]])).T
s.states_over_time[s.N+1:,0] = U[0]
for x in range(1,len(U)):
if Forced:
s.step(U[x],Y[x])
else:
s.step(U[x])
s.states_over_time[:s.N+1,x] = np.concatenate((s.state.T,[[1.0]])).T
s.states_over_time[s.N+1:,x] = U[x]
s.states_over_time = np.matrix(s.states_over_time.astype(float))
s.M = np.matrix(Y[1:])*np.linalg.pinv(s.states_over_time)
_X = np.matrix(Y[1:])
else:
for u in range(len(U)):
s.step(U[u])
s.states_over_time[:s.N+1,u] = np.concatenate((s.state.T,[[1.0]])).T
s.states_over_time[s.N+1:,u] = U[u]
s.states_over_time=np.matrix(s.states_over_time.astype(float))
s.M = np.matrix(Y)*np.linalg.pinv(s.states_over_time)
_X = np.matrix(Y)
s.Wo = s.M[0,:s.N]
s.Bi = s.M[0,s.N]
s.Wio = s.M[0,s.N+1:]
X_ = (s.M*s.states_over_time)
if Show:
plt.plot(_X.T)
plt.plot(X_.T)
plt.show()
return np.sqrt((np.array(X_-_X)**2).mean()/_X.std())
def damped(s):
s.reset()
M = np.zeros((s.N,s.N*2))
for i in range(s.N*2):
M[:,i] = s.state
s.step(0,0)
if ((M[:,-1]<0.01)*1).sum() < s.N:
return False
else:
if s.N <=50:
for k in range(1,1+s.N):
plt.subplot(int(np.sqrt(s.N)+1),int(np.sqrt(s.N)+1),k)
plt.plot(M[k-1,:])
plt.show()
return True
def start(s,U):
s.reset()
plt.plot(s.steps(U))
def evolution(s,U,X=np.array(None)):
s.reset()
M = np.zeros((s.N,len(U)))
for i in range(len(U)):
M[:,i] = s.state
if X != np.array(None):
s.step(U[i])
else:
s.step(U[i],X[i])
for k in range(1,1+s.N):
plt.subplot(int(np.sqrt(s.N)+1),int(np.sqrt(s.N)+1),k)
plt.plot(M[k-1,:])
plt.show()
def writeRes(s,fileprefix = "Reservoir_"):
write_matrix(s.W,fileprefix+"poidsInternes.csv")
write_matrix(s.Wo,fileprefix+"poidsSortie.csv")
write_matrix(s.Wi,fileprefix+"poidsEntree.csv")
write_matrix(s.Wfb,fileprefix+"poidsFeedback.csv")
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 15:20:31 2019
@author: ngocson
"""
import numpy as np
import matplotlib.pyplot as plt
from random import *
from Reservoir import *
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
N = 400
n = 200
relu = np.vectorize(lambda x: max(0,x))
Sig = np.vectorize(lambda x: np.sign(x)*(1-np.exp(-abs(x/5))))
Ln = np.vectorize(lambda x: np.sign(x)*np.log(1+np.abs(x)))
sp = 0.8
R = Reservoir(40,
0.05,
sp,
0.0,
0.001,
Activation = Ln,
Feedback = False,
inputScaling=0.5
)
k = np.linspace(1,N,N)/4
S = np.sin(k)
X = S**7
R.steps(S[:n],X[:n])
Train = S
epsi = R.fit( X[:n],
S[:n],
Forced=False
)
#U = np.sin(k)
R.reset()
R.steps(S[:n])
Y = R.steps(S[n:])
mafigure = plt.figure()
plt.title("Results")
#plt.plot(k[n:],X[n:])
plt.figure("NRMSE")
plt.plot(k[n:],Y)
Epsi = np.sqrt(((Y-Train[n:])**2).mean()/X[n:].std())
plt.show()
'''
plt.plot(X)
plt.plot(Xsquare)
plt.show()
'''
plt.figure("Results")
R.reset()
R.steps(S[:n])
Y_ = R.steps(S[n:])
plt.plot(Y_,'r.-')
plt.plot(X[n:],'g-')
plt.show()
print(epsi,Epsi)
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 15:20:31 2019
@author: ngocson
"""
import numpy as np
import matplotlib.pyplot as plt
from random import *
from Reservoir import *
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
N = 300
n = 100
relu = np.vectorize(lambda x: max(0,x))
Sig = np.vectorize(lambda x: np.sign(x)*(1-np.exp(-abs(x/5))))
Ln = np.vectorize(lambda x: np.sign(x)*np.log(1+np.abs(x)))
sp = 0.8
R = Reservoir(20,
0.1,
sp,
0.5,
0.001,
Activation = np.tanh,
Feedback = True,
inputScaling=0
)
k = np.linspace(1,N,N)/4
U = np.ones(N)
S = np.sin(k)
X = S**7
R.steps(U[:n],X[:n])
Train = S
epsi = R.fit( Train[:n],
U[1:n],
Forced=True
)
#U = np.sin(k)
R.reset()
R.steps(U[:n])
Y = R.steps(U[n:])
mafigure = plt.figure()
plt.title("Results")
#plt.plot(k[n:],X[n:])
#plt.plot(k[n:],X[n:])
plt.figure("NRMSE")
plt.plot(k[n:],Y)
Epsi = np.sqrt(((Y-Train[n:])**2).mean()/X[n:].std())
plt.show()
'''
plt.plot(X)
plt.plot(Xsquare)
plt.show()
'''
plt.figure("Results")
R.reset()
R.reset()
R.steps(U[:100])
Y_ = R.steps(U[100:200])
plt.plot(Y_)
plt.plot(Train[100:200])
plt.show()
print(epsi,Epsi)
\ No newline at end of file
This diff is collapsed.
# -*- coding: utf-8 -*-
"""