Commit 2e86a50e authored by Ngocson's avatar Ngocson

Correcting and cleaning the FPAA simulation'

parent 9147a91b
# -*- coding: utf-8 -*-
import sys
sys.path.append('../../module/')
......@@ -6,10 +8,6 @@ import matplotlib.pyplot as plt
from random import *
import Reservoir
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
N = 6000 #Total number of points for the training
n = 100 #Number of points skipped to remove the influence of the initial state
n_show = 200
......
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 15:20:31 2019
@author: ngocson
"""
import sys
sys.path.append('../../module/')
......@@ -13,10 +8,8 @@ import matplotlib.pyplot as plt
from random import *
import Reservoir
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
N = 400 #Total number of points for the signal
n = 100 #Number of points skipped to remove the influence of the initial state
k = np.linspace(1,N,N)/4
......
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 15:20:31 2019
@author: ngocson
"""
import sys
sys.path.append('../../module/')
......@@ -13,10 +8,6 @@ import matplotlib.pyplot as plt
from random import *
import Reservoir
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
N = 400 #Total number of points for the signal
n = 200 #Number of points skipped to remove the influence of the initial state
k = np.linspace(1,N,N)/4
......
# -*- coding: utf-8 -*-
import sys
sys.path.append('../../module/')
......@@ -7,11 +9,6 @@ from random import *
import Reservoir
import NARMA
'''
import scipy.sparse.linalg as splinalg
from scipy.sparse import csr_matrix
'''
n_train, n_test = 4000,1000 #Total number of points for the signal
d_train, d_test = 400,402 #Number of points skipped to remove the influence of the initial state
n_show = 200
......
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 14:46:13 2019
@author: Ngocson
"""
import numpy as np
import matplotlib.pyplot as plt
import csv
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)
def first_order_numerical_filter(Y,X,tau = 5e-9):
return (X-Y)/tau
def approx_lin(x,
vr=0.8,
a=2.0,
s=1.0):
global V
fi = min(max(100.0*(vr+1)/11,0.0),99.0)
if not fi.is_integer():
i = int(fi)
f = V[i]
else:
f = V[int(fi)]
fx = min(max(1000.0*(x+5)*s,0.0),24999.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)]
def approx(x):
P = [-0.0028984 , 0.02966824, -0.11060687, 0.27304077, -0.1222173 , 0.01029023]
def PolyEval(x,P):
y = 0
for i in range(len(P)):
y += x**(len(P)-i-1)*P[i]
return y
if x < 0.11:
return 0
else:
return min(2.5,PolyEval(x,P))
class reservoirNMOS:
def __init__(s ,
f = first_order_numerical_filter,
activation = np.vectorize(approx_lin),
te = 1e-8,
tc = 1e-9,
N = 50,
vr = 0.3,
sp = None,
p = 0.1,
s_min = 0, #S for synapse
s_max = 1,
i_min = 0, #I for input
i_max = 1,
b_min = 0, # B for biase
b_max = 1,
a_min = 1./2.5, #A for activation
a_max = 2.5,
input_scalling=1,
verbose = False):
s.activation = activation
s.f = f
s.te = te
s.tc = tc
s.N = N
s.sp = sp
s.p = p
s.input_scalling = input_scalling
s.s_min = s_min
s.s_max = s_max
s.i_max = i_max
s.i_min = i_min
s.b_min = b_min
s.b_max = b_max
s.a_max = a_max
s.a_min = a_min
if sp:
s.W = np.matrix([[np.random.rand()*(s.s_max-s.s_min) + s.s_min if np.random.rand() < p else 0 for j in range(s.N)] for i in range(s.N)])
eigen,_ = np.linalg.eig(s.W)
s.W = s.W*s.sp/max(eigen.real)
else:
flag = True
while flag:
s.C = np.matrix([[np.random.rand()*(s.s_max-s.s_min) + s.s_min if np.random.rand() < p else 0 for j in range(s.N)] for i in range(s.N)])
s.W = np.matrix(np.zeros((s.N,s.N)))
flag = False
for i in range(s.N):
Ctot = s.C[i].sum()
if Ctot == 0:
flag = True
break
s.W[i] = 1.*s.C[i]/Ctot
if verbose:
plt.figure('weights')
plt.imshow(s.W)
plt.colorbar()
plt.show()
plt.figure()
plt.hist(s.W)
plt.show()
s.A = np.array([np.random.rand()*(s.a_max-s.a_min) + s.a_min for i in range(s.N)])
s.Ci = np.matrix([np.random.rand()*(s.i_max-s.i_min) + s.i_min for i in range(s.N)])
s.Wi = np.matrix(np.zeros(s.N))
Ctot = s.Ci.sum()
for i in range(s.N):
s.Wi[0,i] = 1.*s.Ci[0,i]/Ctot
s.B = np.matrix([np.random.rand()*(s.b_max-s.b_min) + s.b_min for i in range(s.N)])
s.Wo = np.matrix(np.zeros((1,s.N)))
s.Bo = 0
s.nmos_voltages = np.matrix(np.zeros(((1,s.N))))
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
s.nmos_voltages_dt = np.matrix(np.zeros(((1,s.N))))
s.filter_outputs_dt = np.matrix(np.zeros(((1,s.N))))
s.preactivation_dt = s.B
s.u_dt = 0
s.Vr = np.ones(s.N)*vr
def reset(s,rand = False):
if rand:
s.filter_outputs = np.matrix(np.random.random(((1,s.N))))
else:
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
def read(s):
return (s.Wo*s.filter_outputs.T)[0,0] + s.Bo
def m_step(s,u):
preactivation = s.preactivation_dt+(s.filter_outputs-s.filter_outputs_dt)*s.W.T+s.Wi*(u-s.u_dt)*s.input_scalling
s.u_dt = u
s.nmos_voltages_dt = s.nmos_voltages
s.filter_outputs_dt = s.filter_outputs
s.preactivation_dt = preactivation
s.nmos_voltages = s.activation(preactivation,s.Vr,s.A)
k1 = s.f(s.filter_outputs,
s.nmos_voltages)
k2 = s.f(s.filter_outputs+0.5*k1*s.tc,
s.nmos_voltages)
k3 = s.f(s.filter_outputs+0.5*k2*s.tc,
s.nmos_voltages)
k4 = s.f(s.filter_outputs+k1*s.tc,
s.nmos_voltages)
s.filter_outputs = s.filter_outputs + s.tc/6.*(k1 + 2*k2 + 2*k3 + k4)
def step(s,u):
n_iter = s.te/s.tc
for i in range(int(n_iter)):
s.m_step(u)
def steps(s,U,verbose = False):
outps = []
rep = []
for u in U:
s.step(u)
outps.append(s.read())
rep.append(s.filter_outputs[0,0])
if verbose:
plt.figure()
plt.title("Output")
plt.plot(rep)
plt.show()
return np.array(outps)
def fit(s,Xt,U,lmbda = 0.05, verbose = False):
s.M = np.zeros((s.N+1,len(U)))
for x in range(len(U)):
s.step(U[x])
s.M[:,x] = np.concatenate((s.filter_outputs.T,[[1]])).T
s.M = np.matrix(s.M.astype(float))
tmp = np.matrix(Xt)*(np.linalg.inv(s.M.T*s.M+lmbda*np.eye(Xt.shape[0]))*s.M.T)
s.Wo = tmp[:,:-1]
s.Bo = tmp[-1,-1]
X_ = (tmp*s.M)
_X = np.matrix(Xt)
if verbose:
plt.figure()
plt.title("State of the reservoir during training")
plt.imshow(s.M)
plt.colorbar()
plt.show()
plt.figure()
plt.title("training results")
plt.plot(_X.T,'r-',label="teacher")
plt.plot(X_.T,'b-', label="reservoir output")
plt.legend()
plt.show()
return np.sqrt(np.linalg.norm(X_-_X)/X_.var())
def response_NODE(s,U,n):
M = np.zeros(len(U))
for x in range(len(U)):
s.step(U[x])
M[x] =s.filter_outputs[0,n]
plt.figure()
plt.title("State of the node n°"+str(n))
plt.plot(M,'r-')
plt.show()
return M
def responses_NODE(s,U,N):
M = np.zeros((len(U),len(N)))
for x in range(len(U)):
s.step(U[x])
for n in range(len(N)):
M[x,n] =s.filter_outputs[0,N[n]]
plt.figure()
plt.title("State of the nodes "+str(N))
for n in range(len(N)):
plt.plot(M[:,n],label=str(N[n]))
plt.legend()
plt.show()
return M
......@@ -4,6 +4,9 @@ Created on Tue Apr 23 20:21:40 2019
@author: Ngocson
"""
import sys
sys.path.append('../../module/')
import fpaa_reservoir
import csv
import numpy as np
......
......@@ -4,9 +4,10 @@ Created on Tue Apr 23 20:21:40 2019
@author: Ngocson
"""
from fpaa_reservoir import *
import sys
sys.path.append('../../module/')
from fpaa_reservoir import *
with open('santa_fe.txt') as sftr_file:
sftr_reader = csv.reader(sftr_file, delimiter=';')
......
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 14:46:13 2019
@author: Ngocson
"""
import numpy as np
import matplotlib.pyplot as plt
import csv
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)
def first_order_numerical_filter(Y,X,tau = 5e-9):
return (X-Y)/tau
def approx_lin(x,
vr=0.8,
a=2.0,
s=1.0):
global V
fi = min(max(100.0*(vr+1)/11,0.0),99.0)
if not fi.is_integer():
i = int(fi)
f = V[i]
else:
f = V[int(fi)]
fx = min(max(1000.0*(x+5)*s,0.0),24999.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)]
def approx(x):
P = [-0.0028984 , 0.02966824, -0.11060687, 0.27304077, -0.1222173 , 0.01029023]
def PolyEval(x,P):
y = 0
for i in range(len(P)):
y += x**(len(P)-i-1)*P[i]
return y
if x < 0.11:
return 0
else:
return min(2.5,PolyEval(x,P))
class reservoirNMOS:
def __init__(s ,
f = first_order_numerical_filter,
activation = np.vectorize(approx_lin),
te = 1e-8,
tc = 1e-9,
N = 50,
vr = 0.3,
sp = None,
p = 0.1,
s_min = 0, #S for synapse
s_max = 1,
i_min = 0, #I for input
i_max = 1,
b_min = 0, # B for biase
b_max = 1,
a_min = 1./2.5, #A for activation
a_max = 2.5,
input_scalling=1):
s.activation = activation
s.f = f
s.te = te
s.tc = tc
s.N = N
s.sp = sp
s.p = p
s.input_scalling = input_scalling
s.s_min = s_min
s.s_max = s_max
s.i_max = i_max
s.i_min = i_min
s.b_min = b_min
s.b_max = b_max
s.a_max = a_max
s.a_min = a_min
if sp:
s.W = np.matrix([[np.random.rand()*(s.s_max-s.s_min) + s.s_min if np.random.rand() < p else 0 for j in range(s.N)] for i in range(s.N)])
eigen,_ = np.linalg.eig(s.W)
s.W = s.W*s.sp/max(eigen.real)
else:
flag = True
while flag:
s.C = np.matrix([[np.random.rand()*(s.s_max-s.s_min) + s.s_min if np.random.rand() < p else 0 for j in range(s.N)] for i in range(s.N)])
s.W = np.matrix(np.zeros((s.N,s.N)))
flag = False
for i in range(s.N):
Ctot = s.C[i].sum()
if Ctot == 0:
flag = True
break
s.W[i] = 1.*s.C[i]/Ctot
plt.figure('weights')
plt.imshow(s.W)
plt.colorbar()
plt.show()
plt.figure()
plt.hist(s.W)
plt.show()
s.A = np.array([np.random.rand()*(s.a_max-s.a_min) + s.a_min for i in range(s.N)])
s.Ci = np.matrix([np.random.rand()*(s.i_max-s.i_min) + s.i_min for i in range(s.N)])
s.Wi = np.matrix(np.zeros(s.N))
Ctot = s.Ci.sum()
for i in range(s.N):
s.Wi[0,i] = 1.*s.Ci[0,i]/Ctot
s.B = np.matrix([np.random.rand()*(s.b_max-s.b_min) + s.b_min for i in range(s.N)])
s.Wo = np.matrix(np.zeros((1,s.N)))
s.Bo = 0
s.nmos_voltages = np.matrix(np.zeros(((1,s.N))))
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
s.nmos_voltages_dt = np.matrix(np.zeros(((1,s.N))))
s.filter_outputs_dt = np.matrix(np.zeros(((1,s.N))))
s.preactivation_dt = s.B
s.u_dt = 0
s.Vr = np.ones(s.N)*vr
def reset(s,rand = False):
if rand:
s.filter_outputs = np.matrix(np.random.random(((1,s.N))))
else:
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
def read(s):
return (s.Wo*s.filter_outputs.T)[0,0] + s.Bo
def m_step(s,u):
preactivation = s.preactivation_dt+(s.filter_outputs-s.filter_outputs_dt)*s.W.T+s.Wi*(u-s.u_dt)*s.input_scalling
s.u_dt = u
s.nmos_voltages_dt = s.nmos_voltages
s.filter_outputs_dt = s.filter_outputs
s.preactivation_dt = preactivation
s.nmos_voltages = s.activation(preactivation,s.Vr,s.A)
k1 = s.f(s.filter_outputs,
s.nmos_voltages)
k2 = s.f(s.filter_outputs+0.5*k1*s.tc,
s.nmos_voltages)
k3 = s.f(s.filter_outputs+0.5*k2*s.tc,
s.nmos_voltages)
k4 = s.f(s.filter_outputs+k1*s.tc,
s.nmos_voltages)
s.filter_outputs = s.filter_outputs + s.tc/6.*(k1 + 2*k2 + 2*k3 + k4)
def step(s,u):
n_iter = s.te/s.tc
for i in range(int(n_iter)):
s.m_step(u)
def steps(s,U,verbose = False):
outps = []
rep = []
for u in U:
s.step(u)
outps.append(s.read())
rep.append(s.filter_outputs[0,0])
if verbose:
plt.figure()
plt.title("Output")
plt.plot(rep)
plt.show()
return np.array(outps)
def fit(s,Xt,U,lmbda = 0.05):
s.M = np.zeros((s.N+1,len(U)))
for x in range(len(U)):
s.step(U[x])
s.M[:,x] = np.concatenate((s.filter_outputs.T,[[1]])).T
s.M = np.matrix(s.M.astype(float))
tmp = np.matrix(Xt)*(np.linalg.inv(s.M.T*s.M+lmbda*np.eye(Xt.shape[0]))*s.M.T)
s.Wo = tmp[:,:-1]
s.Bo = tmp[-1,-1]
X_ = (tmp*s.M)
_X = np.matrix(Xt)
plt.figure()
plt.title("State of the reservoir during training")
plt.imshow(s.M)
plt.colorbar()
plt.show()
plt.figure()
plt.title("training results")
plt.plot(_X.T,'r-',label="teacher")
plt.plot(X_.T,'b-', label="reservoir output")
plt.legend()
plt.show()
return np.sqrt(np.linalg.norm(X_-_X)/X_.var())
def response_NODE(s,U,n):
M = np.zeros(len(U))
for x in range(len(U)):
s.step(U[x])
M[x] =s.filter_outputs[0,n]
plt.figure()
plt.title("State of the node n°"+str(n))
plt.plot(M,'r-')
plt.show()
return M
def responses_NODE(s,U,N):
M = np.zeros((len(U),len(N)))
for x in range(len(U)):
s.step(U[x])
for n in range(len(N)):
M[x,n] =s.filter_outputs[0,N[n]]
plt.figure()
plt.title("State of the nodes "+str(N))
for n in range(len(N)):
plt.plot(M[:,n],label=str(N[n]))
plt.legend()
plt.show()
return M
......@@ -4,6 +4,9 @@ Created on Tue Apr 23 20:21:40 2019
@author: Ngocson
"""
import sys
sys.path.append('../module/')
from fpaa_reservoir import *
......
......@@ -135,6 +135,7 @@ class reservoirNMOS:
s.Wo = np.matrix(np.zeros((1,s.N)))
s.Bo = 0
s.Wio = np.matrix(np.zeros(1))
s.nmos_voltages = np.matrix(np.zeros(((1,s.N))))
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
......@@ -152,8 +153,8 @@ class reservoirNMOS:
else:
s.filter_outputs = np.matrix(np.zeros(((1,s.N))))
def read(s):
return (s.Wo*s.filter_outputs.T)[0,0] + s.Bo
def read(s,U):
return (s.Wo*s.filter_outputs.T + s.Wio*U)[0,0] + s.Bo
def m_step(s,u):
......@@ -187,7 +188,7 @@ class reservoirNMOS:
rep = []
for u in U:
s.step(u)
outps.append(s.read())
outps.append(s.read(u))
rep.append(s.filter_outputs[0,0])
if verbose:
plt.figure()
......@@ -197,16 +198,18 @@ class reservoirNMOS:
return np.array(outps)
def fit(s,Xt,U,lmbda = 0.05, verbose = False):
s.M = np.zeros((s.N+1,len(U)))
s.M = np.zeros((s.N+2,len(U)))
for x in range(len(U)):
s.step(U[x])
s.M[:,x] = np.concatenate((s.filter_outputs.T,[[1]])).T
s.M[:s.N+1,x] = np.concatenate((s.filter_outputs.T,[[1]])).T
s.M[s.N+1:,x] = U[x]
s.M = np.matrix(s.M.astype(float))
tmp = np.matrix(Xt)*(np.linalg.inv(s.M.T*s.M+lmbda*np.eye(Xt.shape[0]))*s.M.T)
s.Wo = tmp[:,:-1]
s.Bo = tmp[-1,-1]
s.Wo = s.M[0,:s.N]
s.Bi = s.M[0,s.N]
s.Wio = s.M[0,s.N+1:]
X_ = (tmp*s.M)
_X = np.matrix(Xt)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment