Overview
see reference
Steps
- Create the Model
- Add First Dense Layer
- Add Reshape Layer
- Add LSTM Cells
- Add Output Dense Layer
- Fit to Data
Full Code
await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest');
const model = tf.sequential();
//create first layer with two inputs and 10 outputs
const input_layer_shape = 2;
const input_layer_neurons = 10;
model.add(tf.layers.dense({units: input_layer_neurons, inputShape: [input_layer_shape]}));
const input_layer_features = 5;
const input_layer_timesteps = input_layer_neurons / input_layer_features;
const input_shape = [ input_layer_features, input_layer_timesteps ];
//reshape the 10 output neurons to [5,2]
model.add(tf.layers.reshape({targetShape: input_shape}));
let output_neurons = 20;
let numberOfLayers = 2;
let lstm_cells = [];
for (let index = 0; index < numberOfLayers; index++) {
lstm_cells.push(tf.layers.lstmCell({units: output_neurons}));
}
//add the lstm cells
model.add(tf.layers.rnn({cell: lstm_cells,
inputShape: input_shape, returnSequences: false}));
const output_layer_neurons = 1;
model.add(tf.layers.dense({units: output_layer_neurons, inputShape: [output_neurons]}));
let learningRate = 0.01;
const opt_adam = tf.train.adam(learningRate);
model.compile({ optimizer: opt_adam, loss: 'meanSquaredError'});
//run on data
let inputs = [[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1]];
let outputs = [1,1,1,1,1,1,1];
const xs = tf.tensor2d(inputs, [inputs.length, inputs[0].length]);
const ys = tf.tensor2d(outputs, [outputs.length, 1]).reshape([outputs.length, 1]);
let callback = (epoch,log)=>{};
let epochs = 10;
let batch_size = 1;
const hist = await model.fit(xs, ys,
{ batchSize: batch_size, epochs: epochs, callbacks: {
onEpochEnd: async (epoch, log) => { callback(epoch, log); }}});
Try it!