How to use @tensorflow/tfjs - 10 common examples

To help you get started, we’ve selected a few @tensorflow/tfjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Andrewnetwork / NeuralTitanic / src / modeling.js View on Github external
const xs = tf.tensor(X.slice(i*batchSize,(i+1)*batchSize))
                    const ys = tf.tensor(y.slice(i*batchSize,(i+1)*batchSize))
                    return [xs,ys];
                });

                const history = await model.fit(xs, ys, {batchSize: batchSize, epochs: 1});
                lastBatchLoss = history.history.loss[0];

                tf.dispose([xs, ys]);
                await tf.nextFrame();
                i++;
            }
        }catch(err){
            // End of epoch. 
            //console.log("Epoch "+epoch+"/"+epochs+" ended.");
            const xs = tf.tensor(X);
            const pred = model.predict(xs).dataSync();
            updatePredictions(pred);

            const accuracy = _.sum(_.map(_.zip(pred,y),(x)=> (Math.round(x[0]) == x[1]) ? 1 : 0))/pred.length;

            lossValues.push(lastBatchLoss);
            plotLoss(lossValues,accuracy);

        }
    }
    trainState.s = true;
    createTrainBttn("train",data);
    console.log("End Training");

    // Enable Form Controls
    d3.select("#modelParameters").selectAll(".form-control").attr('disabled', null);
github tensorflow / tfjs-examples / mnist-transfer-cnn / index.js View on Github external
if (trainingMode === 'freeze-feature-layers') {
      console.log('Freezing feature layers of the model.');
      for (let i = 0; i < 7; ++i) {
        this.model.layers[i].trainable = false;
      }
    } else if (trainingMode === 'reinitialize-weights') {
      // Make a model with the same topology as before, but with re-initialized
      // weight values.
      const returnString = false;
      this.model = await tf.models.modelFromJSON({
        modelTopology: this.model.toJSON(null, returnString)
      });
    }
    this.model.compile({
      loss: 'categoricalCrossentropy',
      optimizer: tf.train.adam(0.01),
      metrics: ['acc'],
    });

    // Print model summary again after compile(). You should see a number
    // of the model's weights have become non-trainable.
    this.model.summary();

    const batchSize = 128;
    const epochs = ui.getEpochs();

    const surfaceInfo = {name: trainingMode, tab: 'Transfer Learning'};
    console.log('Calling model.fit()');
    await this.model.fit(this.gte5TrainData.x, this.gte5TrainData.y, {
      batchSize: batchSize,
      epochs: epochs,
      validationData: [this.gte5TestData.x, this.gte5TestData.y],
github tensorflow / tfjs-examples / simple-object-detection / train.js View on Github external
console.log('Training using CPU.');
    require('@tensorflow/tfjs-node');
  }

  const modelSaveURL = 'file://./dist/object_detection_model';

  const tBegin = tf.util.now();
  console.log(`Generating ${args.numExamples} training examples...`);
  const synthDataCanvas = canvas.createCanvas(CANVAS_SIZE, CANVAS_SIZE);
  const synth =
      new synthesizer.ObjectDetectionImageSynthesizer(synthDataCanvas, tf);
  const {images, targets} =
      await synth.generateExampleBatch(args.numExamples, numCircles, numLines);

  const {model, fineTuningLayers} = await buildObjectDetectionModel();
  model.compile({loss: customLossFunction, optimizer: tf.train.rmsprop(5e-3)});
  model.summary();

  // Initial phase of transfer learning.
  console.log('Phase 1 of 2: initial transfer learning');
  await model.fit(images, targets, {
    epochs: args.initialTransferEpochs,
    batchSize: args.batchSize,
    validationSplit: args.validationSplit
  });

  // Fine-tuning phase of transfer learning.
  // Unfreeze layers for fine-tuning.
  for (const layer of fineTuningLayers) {
    layer.trainable = true;
  }
  model.compile({loss: customLossFunction, optimizer: tf.train.rmsprop(2e-3)});
github aayusharora / GeneticAlgorithms / part1 / src / nn.js View on Github external
}))

    /* this is the second output layer with 6 inputs coming from the previous hidden layer
    activation is again sigmoid and output is given as 2 units 10 for not jump and 01 for jump
    */
    dino.model.add(tf.layers.dense({
      inputShape:[6],
      activation:'sigmoid',
      units:2
    }))

    /* compiling the model using meanSquaredError loss function and adam 
    optimizer with a learning rate of 0.1 */
    dino.model.compile({
      loss:'meanSquaredError',
      optimizer : tf.train.adam(0.1)
    })

    // object which will containn training data and appropriate labels
    dino.training = {
      inputs: [],
      labels: []
    };
    
  } else {
    // Train the model before restarting.
    // log into console that model will now be trained
    console.info('Training');
    // convert the inputs and labels to tensor2d format and  then training the model
    console.info(tf.tensor2d(dino.training.inputs))
    dino.model.fit(tf.tensor2d(dino.training.inputs), tf.tensor2d(dino.training.labels));
  }
github tensorflow / tfjs-examples / translation / translation.ts View on Github external
const metadata = {
    'input_token_index': inputTokenIndex,
    'target_token_index': targetTokenIndex,
    'max_encoder_seq_length': maxEncoderSeqLength,
    'max_decoder_seq_length': maxDecoderSeqLength,
  };

  fs.writeFileSync(metadataJsonPath, JSON.stringify(metadata));
  console.log('Saved metadata at: ', metadataJsonPath);

  const encoderInputDataBuf = tf.buffer([
    inputTexts.length,
    maxEncoderSeqLength,
    numEncoderTokens,
  ]);
  const decoderInputDataBuf = tf.buffer([
    inputTexts.length,
    maxDecoderSeqLength,
    numDecoderTokens,
  ]);
  const decoderTargetDataBuf = tf.buffer([
    inputTexts.length,
    maxDecoderSeqLength,
    numDecoderTokens,
  ]);

  for (
    const [i, [inputText, targetText]]
    of (zip(inputTexts, targetTexts).entries() as IterableIterator<[number, [string, string]]>)
  ) {
    for (const [t, char] of inputText.split('').entries()) {
      // encoder_input_data[i, t, input_token_index[char]] = 1.
github victordibia / anomagram / app / src / components / train / Train.jsx View on Github external
for (let row in this.trainData) {
            let val = this.trainData[row]
            if (val.target + "" === 1 + "") { 
                if (trainEcg.length < this.state.trainDataSize) {
                    trainEcg.push(val)
                } else {
                    break
                }
            }  
        }

        // console.log(maxAbnormalCount, "abnormal samples",  abnormalCount, "Total", trainEcg.length);
        

        // Create train tensor from json array
        this.xsTrain = tf.tensor2d(trainEcg.map(item => item.data
        ), [trainEcg.length, trainEcg[0].data.length])
        this.setState({ trainDataShape: this.xsTrain.shape })


        // Create test data TENSOR from test data json array 
        let testData = this.testData.slice(0, this.state.testDataSize)
        this.xsTest = tf.tensor2d(testData.map(item => item.data
        ), [testData.length, testData[0].data.length])

        // Create yLabel Tensor
        this.yTest = testData.map(item => item.target + "" === 1 + "" ? 0 : 1)

        this.setState({ testDataShape: this.xsTest.shape })

    }
github studentinsights / studentinsights / app / assets / javascripts / student_profile / LightNotesDetails.js View on Github external
// const input = tf.tensor2d(sequence, [1, 13000]);
    log('starting...');

    // const input = tf.tensor2d(sequence);
    // const paddedSequence = padSequences([sequence], 13000);
    // log('paddedSequence', paddedSequence);
    // sequence.forEach(index => paddedSequence[index] = 1);
    // log('ones', _.flatMap(paddedSequence, (val, i) => val === 1 ? [i] : []));
    const paddedSequence = _.range(0, 13000).map(index => {
      return (sequence.indexOf(index) !== -1) ? 1 : 0;
    });
    log('ones', _.flatMap(paddedSequence, (val, i) => val === 1 ? [i] : []));

    // do prediction
    const input = tf.tensor2d(paddedSequence, [1, 13000]);
    log('input', input);
    const prediction = model.predict(input);
    log('prediction', prediction);
    const score = prediction.dataSync()[0];
    log('score', score);
    prediction.dispose();
    return score;
  } catch(err) {
    console.error('caught');
    console.error(err);
    return null;
  }
}
github tensorflow / tfjs-models / speech-commands / src / dataset.ts View on Github external
wordExampleIndices.push(i);
      }
    }
    if (noiseExampleIndices.length === 0) {
      throw new Error(
          `Cannot perform augmentation by mixing with noise when ` +
          `there is no example with label ${BACKGROUND_NOISE_TAG}`);
    }

    const mixedXTensors: Array = [];
    const mixedLabelIndices: number[] = [];
    for (const index of wordExampleIndices) {
      const noiseIndex =  // Randomly sample from the noises, with replacement.
          noiseExampleIndices[getRandomInteger(0, noiseExampleIndices.length)];
      const signalTensor = isTypedArray ?
          tf.tensor1d(xs[index] as Float32Array) :
          xs[index] as tf.Tensor;
      const noiseTensor = isTypedArray ?
          tf.tensor1d(xs[noiseIndex] as Float32Array) :
          xs[noiseIndex] as tf.Tensor;
      const mixed: tf.Tensor =
          tf.tidy(() => normalize(signalTensor.add(noiseTensor.mul(ratio))));
      if (isTypedArray) {
        mixedXTensors.push(mixed.dataSync() as Float32Array);
      } else {
        mixedXTensors.push(mixed);
      }
      mixedLabelIndices.push(labelIndices[index]);
    }
    console.log(
        `Data augmentation: mixing noise: added ${mixedXTensors.length} ` +
        `examples`);
github Machine-Learning-Tokyo / tfjs-workshop / web-js / src / index.js View on Github external
const mobilenetDemo = async () => {
  console.log('Loading model...');

  // Pretrained model
  mobilenet = await tf.loadLayersModel(MOBILENET_MODEL_PATH);

  // Load your own model
  // mobilenet = await tf.loadLayersModel('./mymobilenet/mode.json');

  // Warmup the model. This isn't necessary, but makes the first prediction
  // faster. Call `dispose` to release the WebGL memory allocated for the return
  // value of `predict`.
  mobilenet.predict(tf.zeros([1, IMAGE_SIZE, IMAGE_SIZE, 3])).dispose();

  // Make a prediction through the locally hosted cat.jpg.
  const catElement = document.getElementById('img');
  if (catElement.complete && catElement.naturalHeight !== 0) {
    predict(catElement);
    catElement.style.display = '';
  } else {
    catElement.onload = () => {
github IBM / tfjs-web-app / src / pages / Classify.js View on Github external
updateModel = async () => {
    // Get the latest model from the server and refresh the one saved in IndexedDB.
    console.log('Updating the model: ' + INDEXEDDB_KEY);
    this.setState({ isDownloadingModel: true });
    this.model = await tf.loadLayersModel(MODEL_PATH);
    await this.model.save('indexeddb://' + INDEXEDDB_KEY);
    this.setState({
      isDownloadingModel: false,
      modelUpdateAvailable: false,
      showModelUpdateAlert: false,
      showModelUpdateSuccess: true
    });
  }