Mathematica for machine learning - Learning a map

Mathematica newly introduced machine learning to its kitty. And I was excited to see its capabilities! So I decided to test its RNN begin with (Really RNN!).

So here is a simple problem for Mathematica's RNN chain to learn - A simple map 2x Mod 1.

You start with a number say 'x' the next number in the series is 2*x modulus 1. That multiplies the number by 2 and take only the decimal parts (remove the integer part).

Ex. x=1.2 , 2x mod 1 = 0.4

The question I tried to ask was that does the RNN learn the map after some iterations?

Here is the code for doing the training:

f [x_] := Mod[2*x, 1];
data = NestList[f, 0.1, 100];
n = 1;
training =
List /@ Most[#] -> List@Last[#] & /@ (Partition[data, n + 1, 1])];
net = NetChain[{GatedRecurrentLayer[40], GatedRecurrentLayer[40],
GatedRecurrentLayer[40], GatedRecurrentLayer[40],
GatedRecurrentLayer[20], GatedRecurrentLayer[20],
GatedRecurrentLayer[20], GatedRecurrentLayer[20], LinearLayer[1]},
"Input" -> {n, 1}, "Output" -> 1]
trained = NetTrain[net, training, TargetDevice -> "CPU"]
trainedmap [x_] := Flatten[Append[Rest[x], trained[x]]];

Here is the plot of the result where predicted is in blue and ground truth in yellow. I gave RNN an initial condition then its output was used over and over again.



And Now the deeper question did it actually learn the function?

rnnand original

The Red is the original function and the blue what RNN learns.

Can you make it better and learn the complete function!? That's for some other time.




subscribe via RSS