How to use the @tensorflow/tfjs.layers function in @tensorflow/tfjs

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 tensorflow / tfjs-examples / simple-object-detection / train.js View on Github external
function buildNewHead(inputShape) {
  const newHead = tf.sequential();
  newHead.add(tf.layers.flatten({inputShape}));
  newHead.add(tf.layers.dense({units: 200, activation: 'relu'}));
  // Five output units:
  //   - The first is a shape indictor: predicts whether the target
  //     shape is a triangle or a rectangle.
  //   - The remaining four units are for bounding-box prediction:
  //     [left, right, top, bottom] in the unit of pixels.
  newHead.add(tf.layers.dense({units: 5}));
  return newHead;
}
github cstefanache / tfjs-model-view / app / tiny / tiny.js View on Github external
export default async () => {

  // Define the topology of the model: two dense layers.
  const model = tf.sequential();
  model.add(tf.layers.dense({
    units: 2,
    activation: 'tanh',
    inputShape: [1]
  }));

  model.add(tf.layers.dense({
    units: 2,
    activation: 'relu',
    inputShape: [2]
  }));

  model.add(tf.layers.dense({
    units: 3,
    activation: 'softplus',
    inputShape: [2]
  }));
github tensorflow / tfjs-examples / translation / translation.ts View on Github external
// Next: inference mode (sampling).
  // Here's the drill:
  // 1) encode input and retrieve initial decoder state
  // 2) run one step of decoder with this initial state
  // and a "start of sequence" token as target.
  // Output will be the next target token
  // 3) Repeat with the current target token and current states

  // Define sampling models
  const encoderModel = tf.model({
    inputs: encoderInputs,
    outputs: encoderStates,
    name: 'encoderModel',
  });

  const decoderStateInputH = tf.layers.input({
    shape: [args.latent_dim],
    name: 'decoderStateInputHidden',
  });
  const decoderStateInputC = tf.layers.input({
    shape: args.latent_dim,
    name: 'decoderStateInputCell',
  });
  const decoderStatesInputs = [decoderStateInputH, decoderStateInputC];
  let [decoderOutputs, stateH, stateC] = decoderLstm.apply(
      [decoderInputs, ...decoderStatesInputs]
  ) as tf.SymbolicTensor[];

  const decoderStates = [stateH, stateC];
  decoderOutputs = decoderDense.apply(decoderOutputs) as tf.SymbolicTensor;
  const decoderModel = tf.model({
    inputs: [decoderInputs, ...decoderStatesInputs],
github rodrigopivi / aida / typescript / src / pipelines / zebraWings / models / classification.ts View on Github external
private static setup(
        config: types.IClassificationModelParams & types.IDefaultModelParams,
        { maxWordsPerSentence: maxWords, intents }: types.IDatasetParams
    ) {
        const numClasses = intents.length;
        const LEARNING_RATE = 0.0012; // use 1e-4 as default as alternative starting point
        const ADAM_BETA_1 = 0.0008;
        const ADAM_BETA_2 = 0.03;
        const optimizer = tf.train.adam(LEARNING_RATE, ADAM_BETA_1, ADAM_BETA_2);
        // Layer 1: Convolution + max pool
        const input = tf.input({
            dtype: 'float32',
            name: 'embedded_words',
            shape: [maxWords, config.embeddingDimensions]
        });
        const convLayer1 = tf.layers
            .conv1d({
                activation: 'relu',
                filters: config.numFilters,
                inputShape: [maxWords, config.embeddingDimensions],
                kernelInitializer: 'randomNormal',
                kernelSize: [config.filterSizes[0]],
                name: 'classConv1',
                padding: 'valid'
            })
            .apply(input);
        const maxpool1 = tf.layers
            .maxPooling1d({
                padding: 'valid',
                poolSize: maxWords - config.filterSizes[0] + 1
            })
            .apply(convLayer1) as tf.SymbolicTensor;
github cpury / pong-rl / src / js / controllers / visual_dqn.js View on Github external
maxPoolingSize: 2,
      nDenseLayers: 2,
      nHiddenUnits: 100,
      dropout: 0.2,
      lr: 0.01,
      batchSize: 80,
      ...(options || {}),
    };
    Object.assign(this, options);

    // Build a CNN model with three outputs (one for each action)

    this.model = tf.sequential();

    this.model.add(
      tf.layers.conv2d({
        inputShape: [this.inputWidth, this.inputHeight, 1],
        kernelSize: this.kernelSize,
        filters: 40,
        padding: 'same',
        strides: 1,
        activation: 'relu',
        kernelInitializer: 'varianceScaling',
      }),
    );
    this.model.add(tf.layers.dropout(this.dropout));
    this.model.add(tf.layers.batchNormalization());
    this.model.add(
      tf.layers.maxPooling2d({ poolSize: this.maxPoolingSize, strides: this.maxPoolingSize }),
    );

    for (let i = 0; i < this.nConvLayers - 1; i++) {
github zxch3n / PomodoroLogger / src / main / learner / learner.ts View on Github external
async function createModel(
    inputSize: number,
    outputSize: number,
    hiddenLayer: number,
    hiddenSize: number
): Promise {
    const model = tf.sequential();
    model.add(
        tf.layers.dense({
            inputShape: [inputSize],
            units: hiddenSize * hiddenLayer ? hiddenSize : outputSize,
            useBias: true,
            name: 'hidden_0'
        })
    );
    for (let i = 1; i < hiddenLayer; i += 1) {
        model.add(
            tf.layers.dense({
                units: hiddenSize,
                useBias: true,
                name: `hidden_${i}`
            })
        );
    }
github tensorflow / tfjs-examples / addition-rnn / index.js View on Github external
function createAndCompileModel(
    layers, hiddenSize, rnnType, digits, vocabularySize) {
  const maxLen = digits + 1 + digits;

  const model = tf.sequential();
  switch (rnnType) {
    case 'SimpleRNN':
      model.add(tf.layers.simpleRNN({
        units: hiddenSize,
        recurrentInitializer: 'glorotNormal',
        inputShape: [maxLen, vocabularySize]
      }));
      break;
    case 'GRU':
      model.add(tf.layers.gru({
        units: hiddenSize,
        recurrentInitializer: 'glorotNormal',
        inputShape: [maxLen, vocabularySize]
      }));
      break;
    case 'LSTM':
      model.add(tf.layers.lstm({
        units: hiddenSize,
        recurrentInitializer: 'glorotNormal',
github rodrigopivi / aida / typescript / src / pipelines / zebraWings / TimeSeriesAttention.ts View on Github external
public build(inputShape: tf.Shape): void {
        const dimensions = inputShape[2] as number;
        const timed = tf.sequential({ name: 'per_time_step' });
        timed.add(
            tf.layers.dense({
                activation: 'softmax',
                inputShape: [dimensions],
                kernelInitializer: 'zeros',
                name: 'att_dense1',
                units: dimensions
            })
        );
        timed.add(tf.layers.dense({ units: dimensions, kernelInitializer: 'glorotNormal', activation: 'tanh', name: 'att_dense2' }));
        this.timed = tf.layers.timeDistributed({ layer: timed, name: 'att_td' });
        this.timed.build(inputShape);
        this.trainableWeights = this.timed.trainableWeights;
        this.nonTrainableWeights = this.timed.nonTrainableWeights;
        this.built = true;
    }
github tensorflow / tfjs-models / speech-commands / src / browser_fft_recognizer.ts View on Github external
let layerIndex = layers.length - 2;
    while (layerIndex >= 0) {
      if (layers[layerIndex].getClassName().toLowerCase() === 'dense') {
        break;
      }
      layerIndex--;
    }
    if (layerIndex < 0) {
      throw new Error('Cannot find a hidden dense layer in the base model.');
    }
    this.secondLastBaseDenseLayer = layers[layerIndex];
    const truncatedBaseOutput =
        this.secondLastBaseDenseLayer.output as tf.SymbolicTensor;

    this.transferHead = tf.sequential();
    this.transferHead.add(tf.layers.dense({
      units: this.words.length,
      activation: 'softmax',
      inputShape: truncatedBaseOutput.shape.slice(1),
      name: 'NewHeadDense'
    }));
    const transferOutput =
        this.transferHead.apply(truncatedBaseOutput) as tf.SymbolicTensor;
    this.model =
        tf.model({inputs: this.baseModel.inputs, outputs: transferOutput});
  }
github rodrigopivi / aida / typescript / src / pipelines / zebraWings / TimeSeriesAttention.ts View on Github external
public build(inputShape: tf.Shape): void {
        const dimensions = inputShape[2] as number;
        const timed = tf.sequential({ name: 'per_time_step' });
        timed.add(
            tf.layers.dense({
                activation: 'softmax',
                inputShape: [dimensions],
                kernelInitializer: 'zeros',
                name: 'att_dense1',
                units: dimensions
            })
        );
        timed.add(tf.layers.dense({ units: dimensions, kernelInitializer: 'glorotNormal', activation: 'tanh', name: 'att_dense2' }));
        this.timed = tf.layers.timeDistributed({ layer: timed, name: 'att_td' });
        this.timed.build(inputShape);
        this.trainableWeights = this.timed.trainableWeights;
        this.nonTrainableWeights = this.timed.nonTrainableWeights;
        this.built = true;
    }