Mathematica newly introduced machine learning to its kitty. And I was excited to see its capabilities! So I decided to test its RNN abilities..to 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 =

RandomSample[

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?

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.

### Like this:

Like Loading...

*Related*

## Published by midhun

Trying to learn the working of stuff around me mostly by reading and sometimes by experimentation /modelling.
View all posts by midhun

Hello

Hope you & your family are safe in this rain!

LikeLike

Yes, Thank you very much. How about your family?

LikeLike

How did you plot the results and generate the graphs?

LikeLike

There are functions in mathematica to do that

LikeLike