Chaps17.mw

 Maple Text Commands for Chapter 17 - NEURAL NETWORKS 

 

Figure 17.7: The generalized delta learning rule applied to Boston housing data for three attributes. The target data is in column 14 and is the median value of the homes. The housing.txt file is on the C: drive in the Temp folder on my computer.

>

restart:with(LinearAlgebra):with(Statistics):with(RandomTools):
Dim:=506:
ourdata:=readdata("C:\\Temp\\housing.txt",Dim):
ourMatrix:=Matrix(ourdata):
# Scale all data to zero mean and unit variance.
M:=Transpose(ourMatrix):
T:=M[14]:
meant:=(max(T)+min(T))/2:tstd:=(max(T)-min(T))/2:
ones:=[seq(1,i=1..Dim)]:TT:=(convert(M[14],listlist)-ones.meant)/tstd:
M6:=(convert(M[6],listlist)-ones.Mean(M[6]))/StandardDeviation(M[6]):
M9:=(convert(M[9],listlist)-ones.Mean(M[9]))/StandardDeviation(M[9]):
M13:=(convert(M[13],listlist)-ones.Mean(M[13]))/StandardDeviation(M[13]):
X:=Transpose(Matrix([[seq(1,i=1..Dim)],M6,M9,M13])):
ww[1]:=0.1*Matrix(4,1,Generate(rational(denominator=10),makeproc=true)):
epochs:=10:eta:=0.001:k:=1:
for n from 1 to epochs do
for j from 1 to Dim do
yk:=evalf(map(x->tanh(x),X[j].ww[k]));
err:=evalm(yk-[TT[j]])[1];
g:=Transpose(X[j]).(1-yk.yk)*err;
ww[k+1]:=ww[k]-eta*Matrix(4,1,g);
k:=k+1;
end do;end do;

 

>

with(plots):NumPoints:=epochs*Dim:
points1:=[[a,ww[a][1][1]]$a=1..NumPoints]:
points2:=[[a,ww[a][2][1]]$a=1..NumPoints]:
points3:=[[a,ww[a][3][1]]$a=1..NumPoints]:
points4:=[[a,ww[a][4][1]]$a=1..NumPoints]:
plot({points1,points2,points3,points4},x=0..NumPoints,style=point,symbol=point,labels=["Number of Iterations","Weights"]);

 

Plot_2d

 

Example 5: A discrete Hopfield network, asynchronous updating. You can show that the three fundamental memories are stable. 

>

restart:with(LinearAlgebra):
X:=Matrix([[1,1,1,1,1],[1,-1,-1,1,-1],[-1,1,-1,1,1]]);
W:=Transpose(X).X/5-3*IdentityMatrix(5)/5;
with(combinat,randperm):
n:=randperm(5);
# The input vector.
xinput:=Matrix([-1,-1,-1,1,-1]);xtest:=xinput:
hsgn:=(v,x)->piecewise(v>0,1,v=0,x,v<0,-1):
for j from 1 to 5 do
m:=n[j]:
v:=W[m].Transpose(xtest):
xtest([m]):=hsgn(v(1),xtest(m)):end do:
xoutput=xtest;
if convert(xtest,list)=convert(X[1],list)
then print("Net has converged to X1"):
elif convert(xtest,list)=convert(X[2],list)
then print("Net has converged to X2"):
elif convert(xtest,list)=convert(X[3],list)
then print("Net has converged to X3"):
else print("Iterate again: May have converged to a spurious steady-state"):
end if:

 

Matrix(%id = 84667884) 

 

Matrix(%id = 85182240) 

 

[1, 4, 5, 2, 3] 

 

Matrix(%id = 85219444) 

 

xoutput = Matrix(%id = 85219444) 

 

Net has converged to X2

(1)

 

Figure 17.13: Chaotic attractor for a minimal chaotic neuromodule. 

>

restart:x:=array(0..100000):y:=array(0..100000):
b1:=-2:b2:=3:w11:=-20:w21:=-6:w12:=6:imax:=10000:
x[0]:=1:y[0]:=0.2:
for i from 0 to imax do
x[i+1]:=evalf(b1+w11/(1+exp(-x[i]))+w12/(1+exp(-y[i]))):
y[i+1]:=evalf(b2+w21/(1+exp(-x[i]))):end do:
with(plots):
points:=[[x[n],y[n]]$n=50..imax]:
pointplot(points,style=point,symbol=point,color=blue,axes=BOXED,font=[TIMES,ROMAN,15]);

 

Plot_2d

 

Figure 17.16: Bifurcation diagram for a bistable neuromodule. Parameter w[11]is ramped down from 7 to zero and then ramped up from zero back to 7. The system displays quasiperiodic and periodic behaviour for the approximate range, `and`(`<`(0, w[11]), `<`(w[11], 3.7)), and bistable behaviour for  the approximate range, `and`(`<`(5.5, w[11]), `<`(w[11], 7.)) 

>

restart:start:=7:Max:=7:b2:=3:b1:=2:w12:=-4:w21:=5:
halfN:=9999:N1:=1+halfN:itermax:=2*halfN+1:
x[0]:=-3:y[0]:=-2:
# Ramp w11 up.
for n from 0 to halfN do
w11:=start-n*Max/halfN:
x[n+1]:=evalf(b1+w11*tanh(x[n])+w12*tanh(0.3*y[n])):
y[n+1]:=evalf(b2+w21*tanh(x[n])):
end do:
with(plots):
points:=[[start-j*Max/N1,x[j]]$j=0..halfN]:
P1:=pointplot(points,style=point,symbol=point,color=blue,axes=BOXED,font=[TIMES,ROMAN,15]):
# Ramp w11 udown.
for n from N1 to itermax do
w11:=(n-N1)*Max/halfN:
x[n+1]:=evalf(b1+w11*tanh(x[n])+w12*tanh(0.3*y[n])):
y[n+1]:=evalf(b2+w21*tanh(x[n])):
end do:
with(plots):
points:=[[start+(j-N1)*Max/N1,x[N1+j]]$j=0..halfN]:
P2:=pointplot(points,style=point,symbol=point,color=blue,axes=BOXED,font=[TIMES,ROMAN,15]):
display({P1,P2},labels=['w11','x[n]']);

 

Plot_2d

 

End of Chapter 17 Commands