Reseau de neuronne avec maple




Reseau de neuronne



Artificial Neural Network



restart:

with(Maplets): with(Maplets[Elements]):
 rnd := proc()
 0.1*evalf(rand()/10^12)-0.05:
end proc:
makeNetwork := proc(nVars)
local w1, w2, nHidden, i, j, temp:
nHidden := 2^(nVars):
w1 := []:
w2 := Vector([]):
 for i from 1 to nHidden do
 temp := Vector([]):
 for j from 1 to (nVars + 1) do
temp := Vector([op(convert(temp,list)),rnd()]):
end do:
w1 := [op(w1),temp]:
w2 := Vector([op(convert(w2,list)),rnd()]):
end do:
[nVars,w1,w2];
end proc:
perceptron := proc(w, x)
local res:
res := LinearAlgebra:-DotProduct(w,x):
if res > 0 then
res := 1:
else
res := -1:
end if:
res:
end proc:
 getValue := proc(network::list, Data_Point::list, ROUNDOFF::truefalse, returnHidden::truefalse)
local nVars, nHidden, w1, w2, i, data_point, outputHidden, outputHiddenV, roundOff, output, ret, input:
nVars := network[1]:
data_point := Vector(Data_Point):
nHidden := 2^(nVars):
w1 := network[2]:
w2 := network[3]:
if nargs = 2 then roundOff := true: else roundOff := ROUNDOFF: end if:
outputHidden := []:
input := [op(convert(data_point,list)), 1]:
for i from 1 to nops(input) do
input[i] := evalf(( input[i] * 2 ) - 1):
end do:
input := Vector(input):
for i from 1 to nHidden do
outputHidden := [op(outputHidden),perceptron(w1[i],input)]:
end do:
outputHiddenV := Vector(outputHidden):
output := LinearAlgebra:-DotProduct(w2,outputHiddenV):
ret := []:
if nargs = 3 or nargs = 2 then
if roundOff then
ret := round(evalf((output+1)/2)):
else
ret := evalf(( output + 1 ) / 2):
end if:
elif nargs = 4 then
ret := [outputHidden, output]:
end if:
ret:
end proc:
 listAll := proc(network::list, roundOff)
local nVars, testVectors, i, ret:
nVars := network[1]:
ret := []:
testVectors := makeTestVectors(nVars):
for i from 1 to 2^nVars do
if nargs = 1 then
ret := [op(ret),getValue(N,testVectors[i])]:
else
ret := [op(ret),getValue(N,testVectors[i],roundOff)]:
end if:
end do:
ret:
end proc:
rnd2 := proc(n)
floor(n*evalf(rand()/10^12)) + 1;

end proc:
 makeTestVectors := proc(nVars)
local nHidden, tmp, tmp2, res, ret, i, j:
nHidden := 2^(nVars):
ret := []:
for i from 1 to nHidden do
res := []:
tmp := convert(convert(nHidden - i, binary),string):
while length(tmp) < nVars do

tmp := cat('0',tmp):
end do:
for j from 1 to nVars do
tmp2 := parse(StringTools:-SubString(tmp,1..1)):
res := [op(res),tmp2]:
tmp := StringTools:-SubString(tmp,2..-1):
end do:
ret := [op(ret),res]:
end do:
ret:
end proc:
 trainNetwork := proc(network::list,dataset::list, n::integer, learning_rate)
local nVars, nHidden, w1, w2, testVectors, k, r, i, j, input, exOutput, networkOutput, hiddenOutput, errorHidden, errorOutput, returnNetwork:
nVars := network[1]:
nHidden := 2^(nVars):
w1 := network[2]:
w2 := network[3]:
testVectors := makeTestVectors(nVars):
returnNetwork := [nVars,w1,w2]:
for k from 1 to n do
r := rnd2(nHidden):
input := testVectors[r]:
exOutput := evalf((dataset[r] * 2)-1):
networkOutput := getValue(returnNetwork, input, false,true):
hiddenOutput := networkOutput[1]:
networkOutput := networkOutput[2]:
errorOutput := (exOutput - networkOutput):
errorHidden := []:
for i from 1 to nHidden do
errorHidden := [op(errorHidden),w2[i]*errorOutput]:
for j from 1 to nVars do
w1[i][j] := w1[i][j] + learning_rate * errorHidden[i] * (evalf((input[j]*2)-1)):
end do:
w2[i] := w2[i] + learning_rate * errorOutput * hiddenOutput[i]:
end do:
returnNetwork := [nVars,w1,w2]:
updateActual(returnNetwork):
if updateError(returnNetwork) then
k := n:
end if:
end do:
returnNetwork:
end proc:
makeDataSet := proc(network, blnStr)
local nVars, nHidden, execStr, i, j, testVectors, ret:
nVars := network[1]:
nHidden := 2^(nVars):
testVectors := makeTestVectors(nVars):
ret := []:
for i from 1 to nHidden do
execStr := blnStr:
for j from 1 to nVars do
if testVectors[i][j] = 1 then testVectors[i][j] := 'true':
else testVectors[i][j] := 'false': end if:
execStr := StringTools:-SubstituteAll(execStr,'v'||j,testVectors[i][j]):
end do:
if parse(execStr) then
ret := [op(ret),1]:
else
ret := [op(ret),0]:
end if:
end do:
ret:
end proc:
 drawNetwork := proc(network)
local nVars, nHidden, redDots, greenDots, blueDot, i, j, redLines, blueLines, r:
nVars := network[1]:
if nVars = 1 then r := 0.1: else r := 0.35: end if:
nHidden := 2^(nVars):
redDots := []: greenDots := []: redLines := []: blueLines := []:
for i from 1 to nVars do
redDots := [op(redDots),plottools[disk]([-nHidden/4,i-(nVars+1)/2],r,color=red)]:
for j from 1 to nHidden do
redLines := [op(redLines),plottools[line]([-nHidden/4,i-(nVars+1)/2],[0,j-(nHidden+1)/2],color=red)]:
end do:
end do:
for i from 1 to nHidden do
greenDots := [op(greenDots),plottools[disk]([0,i-(nHidden+1)/2],r,color=green)]:
blueLines := [op(blueLines),plottools[line]([0,i-(nHidden+1)/2],[nHidden/4,0],color=blue)]:
end do:
blueDot := plottools[disk]([nHidden/4,0],r,color=blue):
plots[display](seq(redLines[i],i=1..nops(redLines)),seq(blueLines[i],i=1..nops(blueLines)),seq(redDots[i],i=1..nVars),seq(greenDots[i],i=1..nHidden),blueDot,scaling=constrained,axes=none);
end proc:
q := proc(n)
floor(n/2):
end proc:
r := proc(n)
n mod 2;
end proc:
drawNetwork3D := proc(network, th, ph)
local nHidden, greenDots, nVars, i, j, k, sideN,sideHole,symSiz, redLines, greenLines:
nVars := network[1]:
symSiz := 100:
nHidden := 2^(nVars):
greenDots := []:
redLines := []:
greenLines := []:
if nHidden = 2 then
greenDots := [plottools[point]([-0.5,0,0],color=green,symbol=cross,symbolsize=symSiz),
plottools[point]([0.5,0,0],color=green,symbol=cross,symbolsize=symSiz)]:
redLines := [plottools[line]([0,0,-sqrt(2)],[-0.5,0,0],color=red),plottools[line]
([0,0,-sqrt(2)],[0.5,0,0],color=red)]:
> greenLines := [plottools[line]([-0.5,0,0],[0,0,sqrt(2)],color=green),plottools[line]
([0.5,0,0],[0,0,sqrt(2)],color=green)]:
elif nVars mod 2 = 0 then
sideN := 2^(nVars/2):
symSiz := floor(350 / sideN):
for i from 1 to sideN do
for j from 1 to sideN do
greenDots := [op(greenDots),plottools[point]([-(sideN/2)+0.5 + (i-1),
-(sideN/2)+0.5 + (j - 1), 0],color=green,symbol=cross,symbolsize=symSiz)]:
greenLines := [op(greenLines),plottools[line]([-(sideN/2)+0.5 + (i-1),
-(sideN/2)+0.5 + (j - 1), 0],[0,0,sqrt(nHidden)],color=green)]:
for k from 1 to nVars do
redLines := [op(redLines),plottools[line]([-(0.5) + r(k-1), -(q(nVars -
1) * 0.5) + q(k-1), -sqrt(nHidden)],[-(sideN/2)+0.5 + (i-1), -(sideN/2)+0.5 + (j-1),
0],color=red)]:
end do:
end do:
end do:else
sideN := 2^((nVars - 3)/2)*3:
sideHole := sqrt(sideN^2 - nHidden):
symSiz := floor(350 / sideN):
for i from 1 to sideN do
for j from 1 to sideHole do
greenDots := [op(greenDots),plottools[point]([-(sideN/2)+0.5+(i-1),
-(sideN/2)+0.5+(j-1),0],color=green,symbol=cross,symbolsize=symSiz),plottools[point]
([-(sideN/2)+0.5+(i-1), sideHole/2 + 0.5 + (j-1),0],color=green,symbol=cross,symbolsize=symSiz)]:
> greenLines := [op(greenLines),plottools[line]([-(sideN/2)+0.5+(i-1), -(sideN/2)+0.5+(j-1),0],
[0,0,sqrt(nHidden)],color=green),plottools[line]([-(sideN/2)+0.5+(i-1), sideHole/2+ 0.5 + (j-1),0],[0,0,sqrt(nHidden)],color=green)]:for k from 1 to nVars do
redLines := [op(redLines),plottools[line]([-0.5 + r(k-1), -(q(nVars - 1) *0.5) + q(k-1), -sqrt(nHidden)],[-(sideN/2)+0.5+(i-1), -(sideN/2)+0.5+(j-1),0],color=red)
,plottools[line]([-0.5 + r(k-1), -(q(nVars - 1) * 0.5) + q(k-1), -sqrt(nHidden)],[-(sideN/2)+
0.5+(i-1), sideHole/2 + 0.5 + (j-1),0],color=red)]:
end do:
end do:
end do:
for i from 1 to sideHole do
for j from 1 to sideHole do
greenDots := [op(greenDots),plottools[point]([-(sideN/2)+0.5+(i-1),-(sideHole/2) + 0.5 + (j-1),0],color=green,symbol=cross,symbolsize=symSiz),
plottools[point]([sideHole/2 + 0.5 + (i-1), -(sideHole/2) + 0.5 + (j-1), 0],
color=green,symbol=cross,symbolsize=symSiz)]:
> greenLines := [op(greenLines),plottools[line]([-(sideN/2)+
0.5+(i-1), -(sideHole/2) + 0.5 + (j-1),0],[0,0,sqrt(nHidden)],color=
green),plottools[line]([sideHole/2 + 0.5 + (i-1), -(sideHole/2) + 0.5+ (j-1), 0],[0,0,sqrt(nHidden)],color=green)]:
for k from 1 to nVars do
redLines := [op(redLines),plottools[line]([-0.5 + r(k-1),
-(q(nVars - 1) * 0.5) + q(k-1), -sqrt(nHidden)],[-(sideN/2)+0.5+(i-1),
-(sideHole/2) + 0.5 + (j-1),0],color=red),plottools[line]([-0.5 + r(k-1), -
(q(nVars - 1) * 0.5) + q(k-1), -sqrt(nHidden)],[sideHole/2 + 0.5 + (i-1), -
(sideHole/2) + 0.5 + (j-1), 0],color=red)]:
end do:
end do:
end do:
end if:
plots[display](seq(greenDots[i],i=1..nops(greenDots)),seq(redLines
[i],i=1..nops(redLines)),seq(greenLines[i],i=1..nops(greenLines)),axes=
none,style=patchnogrid,scaling=constrained,orientation=[th,ph]);
end proc:
N := makeNetwork(2):
FmakeNew := proc()
global N:
local n, bln3D:
n := parse(Maplets:-Tools:-Get(TFnVars)):
N := makeNetwork(n):
bln3D := parse(Maplets:-Tools:-Get(CB3d)):
updateActual(N):
clearExpected():
clearError():
if bln3D then
drawNetwork3D(N, 50, 68):
else
drawNetwork(N):
end if:
end proc:
> FgetValue := proc()
global N:
local input, blnRound:
input := [parse(Maplets:-Tools:-Get(TFinput))]:
blnRound := parse(Maplets:-Tools:-Get(CBroundOff)):
getValue(N,input,blnRound):
end proc:
> Ftrain := proc()
global N:
local tStr, t, n, rate:
tStr := Maplets:-Tools:-Get(TFblnStr):
t := makeDataSet(N,tStr):
Maplets:-Tools:-Set(TBexOut = convert(t,string)):
rate := parse(Maplets:-Tools:-Get(TFrate)):
n := parse(Maplets:-Tools:-Get(TFn)):
N := trainNetwork(N,t,n,rate):
end proc:
calError := proc(start)
global N:
local tStr,t,output,ret,i,blnRnd:
if nargs = 1 and start then
tStr := 'v1 or v2':
blnRnd := true:
else
tStr := Maplets:-Tools:-Get(TFblnStr):
blnRnd := parse(Maplets:-Tools:-Get(CBrndOffError)):
end if:
t := makeDataSet(N,tStr):
output := listAll(N, blnRnd):
ret := []:
for i from 1 to nops(output) do
if blnRnd then
ret := [op(ret),round(output[i])-round(t[i])]:
else
ret := [op(ret),output[i]-t[i]]:
end if:
end do:
ret:
end proc:
updateError := proc(network)
local tStr,t,output,ret,i,blnRnd, finished:
tStr := Maplets:-Tools:-Get(TFblnStr):
blnRnd := parse(Maplets:-Tools:-Get(CBrndOffError)):
t := makeDataSet(network,tStr):
output := listAll(network,blnRnd):
finished := true:
ret := []:
for i from 1 to nops(output) do
if blnRnd then
ret := [op(ret),round(output[i])-round(t[i])]:
else
ret := [op(ret),output[i]-t[i]]:
end if:
if not (round(output[i]) - round(t[i]) = 0) then
finished := false:
end if:
end do:
Maplets:-Tools:-Set(TBerror = convert(ret,string)):
finished:
end proc:
updateActual := proc(network)
local blnRnd:
blnRnd := parse(Maplets:-Tools:-Get(CBrndOffError)):
Maplets:-Tools:-Set(TBacOut = convert(listAll(network,blnRnd),string)):
end proc:
clearExpected := proc()
Maplets:-Tools:-Set(TBexOut = ''):
end proc:
clearError := proc()
Maplets:-Tools:-Set(TBerror = ''):
end proc:
ann := Maplet(Window[W1](title='ANN', menubar=MB1, 'layout' = BL1),
MenuBar[MB1](Menu('File', MenuItem('Close', Shutdown()))),
BoxLayout[BL1](inset=0,
BoxColumn(inset=0,spacing=0,
BoxRow(inset=0,spacing=0,
Plotter['P1'](value = drawNetwork(N))),BoxRow(inset=0, spacing=0,BoxColumn(inset=0,spacing=0,
BoxRow(inset=0,spacing=0,
Label('Expected Output: ',font=Font('courier',size=12)),
TextBox[TBexOut](editable=false,font=Font('courier',size=12),width=35,
height=3, '[1, 1, 1, 0]')),BoxRow(inset=0,spacing=0,
Label('Actual Output: ',font=Font('courier',size=12)),
TextBox[TBacOut](editable=false,font=Font('courier',size=12),width=35,height=3,value=listAll(N))),
BoxRow(inset=0,spacing=0,
Label('Error: ',font=Font('courier',size=12)),TextBox[TBerror](editable=false,font=Font('courier',size=12),width=35
,height=3,value=calError(true))),BoxRow(inset=0,spacing=0,CheckBox[CBrndOffError](value=true,caption='Round Off:
',font=Font('courier',size=12)))))),BoxColumn(inset=0,spacing=0,
BoxRow(inset=0,spacing=0,border=true,caption='Make a new Network',
Button[BmakeNew]('Create Network',onclick=AmakeNew),' Number of Variables: ',
TextField[TFnVars](4,'2',halign=right),
CheckBox[CB3d](false,caption='3D')),BoxRow(inset=0,spacing=0,border=true,caption='Train the network',
BoxColumn(inset=0,spacing=0,
BoxRow(inset=0,spacing=0,'Enter a boolean function using v1,v2,... as the variables'),
BoxRow(inset=0,spacing=0,
TextField[TFblnStr](15,'v1 or v2',halign=right)),
BoxRow(inset=0,spacing=0,'Enter the learning rate: ',TextField[TFrate](4,'0.05',halign=right)),
BoxRow(inset=0,spacing=0,'Enter the number of times to train: ',
TextField[TFn](4,'200',halign=right)
),BoxRow(inset=0,spacing=0,
Button[Btrain]('Train',onclick=Atrain)))),
BoxRow(inset=0,spacing=0,border=true,caption='Input a vector',
BoxColumn(inset=0,spacing=0,
BoxRow(inset=0,spacing=0,
BoxColumn(inset=0,spacing=0,
'Input a list of numbers, separated by commas,',
'representing the values of the input variables.',
'Ex.: 0,1,0,0,1 for an input vector of 5 variables'
)),BoxRow(inset=0,spacing=0,
TextField[TFinput](10,'1,0',halign=right)
),BoxRow(inset=0,spacing=0,
Button[BgetValue]('Get Value',onclick=AgetValue),
' ',CheckBox[CBroundOff](caption='Round Off',value=true)),BoxRow(inset=0,spacing=0,'Result: ',
TextField[TFresult](10,editable=false,halign=right)))))),
Action[AmakeNew](Evaluate('P1' = 'FmakeNew()')),
Action[AgetValue](Evaluate(TFresult = 'FgetValue()')),
Action[Atrain](Evaluate(function = 'Ftrain()'))):

> Display(ann);
> drawNetwork3D(N,90,90);


Ajouter un commentaire
 
MOBILISONS NOUS POUR LE DEVELOPPEMENT DE SARGHINE EN AIDANT L'ASSOCIATION AMSIRAR.

Créer un site gratuit avec e-monsite - Signaler un contenu illicite sur ce site