(* This is the Mathematica code (the mathematics) for the single layer neural network demonstration we did in class. It does the training part of the NN. The running of the algorithm is just using the w matrix and input to get the result and you can see how to do that in the training part. *) (* set up single layer network *) x = Transpose[{{120, 195, 173, 150, 163, 178, 180, 159, 143, 190, 186}}]; t = Transpose[{{-1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1}}]; xb = Map[Append[#, 1] & , norm[x]]; w = RandomReal[{-0.1, .1}, {2, 1}]; (* show a bunch of stuff *) MatrixForm[t] data = Join[x, t, 2]; MatrixForm[Sort[data]] MatrixForm[xb] MatrixForm[w] (* run the training for single layer*) ans = Reap[Do[ (* Reap is for plotting *) y = xb . w; yt = Map[tf[#] &, y, {2}]; Sow[EuclideanDistance[yt, t]]; (* Sow is for plotting *) eta = .1; delta = eta ( Transpose[xb].(t - yt)); w += delta; , 2000]]; ListPlot[first[second[ans]]] (* plot the result *) MatrixForm[yt] MatrixForm[t]