Simple Recurrent Neural Network

Overview


This example continues with the single layer simple recurrent network

Adding a Dense Layer


To turn the simple RNN into a multi-layer neural network can be done by simple adding a layer to the model.

model.add(tf.layers.dense({inputShape:[3],units:3,activation:'relu'}));
				

Full Script


The following script implements a simple RNN and then runs it on a sample input. No training is done, so the output will be random.


//load tensor flow library
await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest');
let ut = await import('/lib/tensor-flow/util/v1.0.0/util.mjs');

const model = tf.sequential({
    layers:[
      tf.layers.simpleRNN({
        units:3,
        inputShape:[2,1]
      })
    ]
  });
  
model.add(tf.layers.dense({units:3,activation:'relu'}));

model.compile({
  loss:tf.losses.meanSquaredError,
  optimizer:tf.train.adam(0.1)
});

let X = [
	// datapoint 1
    [[1],[2]],
	//datapoint 2
    [[2],[2]]
];
let xs = tf.tensor(X);

let y= [
	//datapoint1
    [1,1,1],
	//datapoint2
    [1,3,1]
];
let ys = tf.tensor(y);

await model.fit(xs,ys,{
  batchSize:2,
  shuffle:true,
  epochs:10
});
				
Try it!

Contents