# What's going wrong with this button! [duplicate]

I'm implementing an Adaline algorithm where all inputs are 1x5526 and the output is a number from 1 to 10. I have this two functions defined and worked outside the Manipulate environment:

test[in_, weights_] := (a = Round[Total[Most[weights]*Most[in]] + Last[weights]];  Return[a])
train[in_, out_, alpha_, delta_] := ((*uweights holds also the bias with 1*)
sizePattern = Length[in[[1]]];
sizeOutput = 1;
Length[out];
uweights = ConstantArray[0, {sizePattern}];
dweights = {};
flag = True;
cont = 0;
Error = {};
While[flag == True && cont < 10000,
flag = False;
perror = 0;
cont++;
(*Checking each input-output*)
For[i = 1, i <= 1(*Dimensions[out][[1]]*), i++,
(*Checking each neuron*)
For[k = 1, k <= Length[out](*Dimensions[out][[2]]*), k++,
est = Total[Most[uweights]*Most[in[[k]]]] + Last[uweights];
If[out[[k]] + delta <= est || out[[k]] - delta >= est,
(*Print["Estimated different"] ;*)
flag = True;
dweights = alpha*1*(out[[k]] - est)*Most[in[[k]]];
dbias = alpha*1*(out[[k]] - est);
perror = perror + .5*(out[[k]] - est)^2;
uweights = uweights + Flatten[{dweights, dbias}]]]]];
Return[uweights])


In these lines, they gave the amount of good answer I get with these weights:

AbsoluteTiming[w = train[Samples, AOutput, 0.00001, 0.4];]
uweights = w;
Report = Table[test[Samples[[i]], w], {i, 30}];
Count[Table[Report[[i]] == AOutput[[i]], {i, 30}], True]


{28.516558, Null}

30

The problem emerges when I put the functions test and train inside the Manipulate environment:

Manipulate[
Text@Style["    ", "Large"]},
{ArrayPlot[ArrayReshape[Samples[[dato]], {85, 65}]]},
(*This line was add to verify if train is working!!!*)
{weights // MatrixForm}}],
{{weights, {}}, ControlType -> None},
{{dato, 1}, ControlType -> None},
{{alpha, 0.00001, "Learning rate "}, 0.0000001, .0001, 0.0000005,
Appearance -> "Labeled"},
{{delta, 0.5, "Error range "}, 0.01, 0.5, 0.01,
Appearance -> "Labeled"},
Dynamic[FontType], {1 -> "Comics", 2 -> "Times", 3 -> "Calibri"}]}],
Row[{Button["Train", weights = train[Samples, AOutput, alpha, delta],
Background -> Yellow, ImageSize -> Medium],
Style["                    ", 14],
Button["Test",
If[FontType == 1,
Switch[NumberPicked, 0, dato = 1, 1, dato = 4, 2, dato = 7, 3,
dato = 10, 4, dato = 13, 5, dato = 16, 6, dato = 19, 7,
dato = 22, 8, dato = 25, 9, dato = 28]];
If[FontType == 2,
Switch[NumberPicked, 0, dato = 2, 1, dato = 5, 2, dato = 8, 3,
dato = 11, 4, dato = 14, 5, dato = 17, 6, dato = 20, 7,
dato = 23, 8, dato = 26, 9, dato = 29]];
If[FontType == 3,
Switch[NumberPicked, 0, dato = 3, 1, dato = 6, 2, dato = 9, 3,
dato = 12, 4, dato = 15, 5, dato = 18, 6, dato = 21, 7,
dato = 24, 8, dato = 27, 9, dato = 30]];
Background -> Green, ImageSize -> Medium]}]]


And the output is this:

After push the button train, it seems that the train function was executed, but no values are returned as you can see below the zero. I appreciate any help with this! maybe it seems a silly mistake but I cannot see it.

-

## marked as duplicate by m_goldberg, ciao, Sjoerd C. de Vries, Yves Klett, KubaMay 1 '14 at 9:38

This question was marked as an exact duplicate of an existing question.

Dynamic@weights? – wxffles Apr 30 '14 at 4:02
If the train calculation takes longer than 5 seconds the button will be timing out. You might need to use the Method -> "Queued" option. – Simon Woods Apr 30 '14 at 20:32
This question appears to be off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster and answers will not benefit others. – m_goldberg May 1 '14 at 2:58
Thanks Simon! that suggestion solve the problem! – Jotasmall May 1 '14 at 11:22
m_goldberg it would be nice that you indicate a link toward the duplicated question! – Jotasmall May 1 '14 at 11:29

I suggest that you wrap the definition of the functions into the Module or with if there are many internal variables:

test[in_, weights_] := Round[Total[Most[weights]*Most[in]] + Last[weights]]

train[in_, out_, alpha_, delta_] :=
Module[{sizePattern, sizeOutput, uweights, dweights, flag,
cont, Error, perror, i, k, est, dbias},
…
Return[uweights]]


Sometimes the scoping issue will cause the problem. Is there a way to share the data you are using to assist the debugging?

-
Nope, it is a set of 200 images of 85x65! thanks, but your answer doesn't solve this problem. – Jotasmall May 1 '14 at 11:16