Garmaine Staff asked 2 years ago

I want to make a AI which calculates the posible value of a variable, like the value of a cryptocoin or another thing. So, I tried this code:


async function trainModel(inputs, outputs, trainingsize, window_size, n_epochs, learning_rate, n_layers, callback){

  const input_layer_shape  = window_size;
  const input_layer_neurons = 100;

  const rnn_input_layer_features = 10;
  const rnn_input_layer_timesteps = input_layer_neurons / rnn_input_layer_features;

  const rnn_input_shape  = [rnn_input_layer_features, rnn_input_layer_timesteps];
  const rnn_output_neurons = 20;

  const rnn_batch_size = window_size;

  const output_layer_shape = rnn_output_neurons;
  const output_layer_neurons = 1;

  const model = tf.sequential();

  let X = inputs.slice(0, Math.floor(trainingsize / 100 * inputs.length));
  let Y = outputs.slice(0, Math.floor(trainingsize / 100 * outputs.length));

  const xs = tf.tensor2d(X, [X.length, X[0].length]).div(tf.scalar(10));
  const ys = tf.tensor2d(Y, [Y.length, 1]).reshape([Y.length, 1]).div(tf.scalar(10));

  model.add(tf.layers.dense({units: input_layer_neurons, inputShape: [input_layer_shape]}));
  model.add(tf.layers.reshape({targetShape: rnn_input_shape}));

  let lstm_cells = [];
  for (let index = 0; index < n_layers; index++) {
       lstm_cells.push(tf.layers.lstmCell({units: rnn_output_neurons}));

    cell: lstm_cells,
    inputShape: rnn_input_shape,
    returnSequences: false

  model.add(tf.layers.dense({units: output_layer_neurons, inputShape: [output_layer_shape]}));

    optimizer: tf.train.adam(learning_rate),
    loss: 'meanSquaredError'

  const hist = await, ys,{ 
    batchSize: rnn_batch_size, epochs: n_epochs, callbacks: {
      onEpochEnd: async (epoch, log) => {
        callback(epoch, log);

  return { model: model, stats: hist };

    return trainModel([0], array1, 1, innerWidth, 999999, 1, layers, true)


And the result was this:

brain1.js:21 Uncaught (in promise) ReferenceError: tf is not defined
    at trainModel (brain1.js:21)
    at train (brain1.js:62)
    at brain1.js:65

…after that. How to solve this error, and finally, get this script working? My objetive is to do a "time-TV" and predict an amount of variables like some bunch of sites or images and get some curious data. Could it be possible???