Overview
Create the Model
const model = tf.sequential({
layers:[
tf.layers.simpleRNN({
units:3,
inputShape:[2,1]
})
]
});
An alternative approach is to create the model and then add the layers.
const meodel = tf.sequential();
model.add(tf.layers.simpleRNN({
units:3,
inputShape:[2,1]
}));
Prediction
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]
})
]
});
let input = tf.tensor([[[1],[2]]]);
let output = model.predict(input);
let matrix = await ut.toArray(output);
Try it!
Training
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.compile({
loss:tf.losses.meanSquaredError,
optimizer:tf.train.adam(0.1)
});
let X = [
[[1],[2]],
[[2],[2]]
];
let xs = tf.tensor(X);
let y= [
[1,1,1],
[1,3,1]
];
let ys = tf.tensor(y);
await model.fit(xs,ys,{
batchSize:2,
shuffle:true,
epochs:10
});
Try it!