0

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:

tfs=document.createElement("script")
tfs.src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/1.7.2/tf.js"
document.head.appendChild(tfs)

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}));
  }

  model.add(tf.layers.rnn({
    cell: lstm_cells,
    inputShape: rnn_input_shape,
    returnSequences: false
  }));

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

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

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

  return { model: model, stats: hist };
}

train=(array1,layers)=>{
    return trainModel([0], array1, 1, innerWidth, 999999, 1, layers, true)
}

console.log(train([1],10))

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???

4

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy