當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript tfjs.layers類代碼示例

本文整理匯總了TypeScript中@tensorflow/tfjs.layers的典型用法代碼示例。如果您正苦於以下問題:TypeScript layers類的具體用法?TypeScript layers怎麽用?TypeScript layers使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了layers類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: constructor

  constructor() {
    super('strike-zone');

    this.fields = [
      {key : 'px', min : PX_MIN, max : PX_MAX},
      {key : 'pz', min : PZ_MIN, max : PZ_MAX},
      {key : 'sz_top', min : SZ_TOP_MIN, max : SZ_TOP_MAX},
      {key : 'sz_bot', min : SZ_BOT_MIN, max : SZ_BOT_MAX},
      {key : 'left_handed_batter'}
    ];

    this.data =
        new PitchData('dist/strike_zone_training_data.json', 50, this.fields, 2,
                      (pitch) => pitch.type === 'S' ? 0 : 1);

    const model = tf.sequential();
    model.add(tf.layers.dense({
      units : 20,
      activation : 'relu',
      inputShape : [ this.fields.length ]
    }));
    model.add(tf.layers.dense({units : 10, activation : 'relu'}));
    model.add(tf.layers.dense({units : 2, activation : 'softmax'}));
    model.compile({
      optimizer : tf.train.adam(),
      loss : 'categoricalCrossentropy',
      metrics : [ 'accuracy' ]
    });
    this.model = model;
  }
開發者ID:AIED-ECNU,項目名稱:tfjs-examples,代碼行數:30,代碼來源:strike-zone-model.ts

示例2: densenet

function densenet(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
    const depth = 8
    const width = 8
    for (var i = 0; i < depth; ++i) {
        let y = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'sigmoid' }).apply(x) as tf.SymbolicTensor
        x = tf.layers.concatenate({}).apply([x, y]) as tf.SymbolicTensor
    }

    return x
}
開發者ID:gyngyn1234,項目名稱:blog,代碼行數:10,代碼來源:model.ts

示例3: perceptron

function perceptron(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
    const depth = 8
    const width = 32
    for (var i = 0; i < depth; ++i) {
        x = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'tanh' }).apply(x) as tf.SymbolicTensor
    }

    return x
}
開發者ID:gyngyn1234,項目名稱:blog,代碼行數:9,代碼來源:model.ts

示例4: constructor

  constructor() {
    super('pitch-type');

    this.fields = [
      {key: 'vx0', min: VX0_MIN, max: VX0_MAX},
      {key: 'vy0', min: VY0_MIN, max: VY0_MAX},
      {key: 'vz0', min: VZ0_MIN, max: VZ0_MAX},
      {key: 'ax', min: AX_MIN, max: AX_MAX},
      {key: 'ay', min: AY_MIN, max: AY_MAX},
      {key: 'az', min: AZ_MIN, max: AZ_MAX},
      {key: 'start_speed', min: START_SPEED_MIN, max: START_SPEED_MAX},
      {key: 'left_handed_pitcher'}
    ];

    this.data = new PitchData(
        'dist/pitch_type_training_data.json', 100, this.fields, 7,
        (pitch) => pitch.pitch_code);

    const model = tf.sequential();
    model.add(tf.layers.dense(
        {units: 250, activation: 'relu', inputShape: [this.fields.length]}));
    model.add(tf.layers.dense({units: 175, activation: 'relu'}));
    model.add(tf.layers.dense({units: 150, activation: 'relu'}));
    model.add(tf.layers.dense({units: 7, activation: 'softmax'}));
    model.compile({
      optimizer: tf.train.adam(),
      loss: 'categoricalCrossentropy',
      metrics: ['accuracy']
    });

    this.model = model;

    // All pitch data is stored sequentially (pitch_code 0-6) in the training
    // files. Load the training and validation data file and glob all pitches of
    // the same code together in batches. These will be used for calculating the
    // class accuracy.
    this.trainingClassTensors = concatPitchClassTensors(
        'dist/pitch_type_training_data.json', this.fields, NUM_PITCH_CLASSES,
        TRAINING_DATA_PITCH_CLASS_SIZE);
    this.validationClassTensors = concatPitchClassTensors(
        'dist/pitch_type_validation_data.json', this.fields, NUM_PITCH_CLASSES,
        TRAINING_DATA_PITCH_CLASS_SIZE);
  }
開發者ID:AIED-ECNU,項目名稱:tfjs-examples,代碼行數:43,代碼來源:pitch-type-model.ts

示例5:

(async () => {
  const model = tf.sequential({
    layers: [
      tf.layers.dense({ units: 200, inputShape: [400], activation: 'relu' }),
      tf.layers.dense({ units: 200, activation: 'relu' }),
      tf.layers.dense({ units: 200, activation: 'relu' }),
      tf.layers.dense({ units: 1 }),
    ],
  });

  const optimizer = tf.train.sgd(0.001);

  model.compile({ loss: 'meanSquaredError', optimizer });

  const xs = tf.tensor2d(results.map(r => r.input));
  const ys = tf.tensor2d(results.map(r => [r.fitness]));

  const [evaluteX, trainX] = tf.split(xs, 2, 0);
  const [evaluteY, trainY] = tf.split(ys, 2, 0);

  await model
    .fit(trainX, trainY, {
      epochs: 100,
      shuffle: true,
      validationSplit: 0.2,
    })
    .then(h => {
      console.log(h);
      console.log(
        `Learning Loss: ${h.history.loss[h.history.loss.length - 1]}`
      );
      console.log(
        `Validation Loss: ${h.history.val_loss[h.history.val_loss.length - 1]}`
      );
    });

  console.log(tf.memory());
})();
開發者ID:Vakrim,項目名稱:not_so_ragdoll,代碼行數:38,代碼來源:createModelOfSim.ts

示例6: resnet

function resnet(x: tf.SymbolicTensor, init: any): tf.SymbolicTensor {
    const depth = 8
    const width = 32
    for (var i = 0; i < depth; ++i) {
        let y: tf.SymbolicTensor
        if (i != 0)
            y = tf.layers.activation({ activation: 'tanh' }).apply(x) as tf.SymbolicTensor
        else
            y = x

        y = tf.layers.dense({ units: width, kernelInitializer: init, activation: 'tanh' }).apply(x) as tf.SymbolicTensor
        
        let x_ = x
        if (x_.shape[1] != width)
            x_ = tf.layers.dense({ units: width, kernelInitializer: init }).apply(x_) as tf.SymbolicTensor
        
        x = tf.layers.add().apply([x_, y]) as tf.SymbolicTensor
    }

    x = tf.layers.activation({ activation: 'tanh' }).apply(x) as tf.SymbolicTensor

    return x
}
開發者ID:gyngyn1234,項目名稱:blog,代碼行數:23,代碼來源:model.ts

示例7: createModel

export function createModel(type: "densenet" | "perceptron" | "resnet", scale: number, bw = false, seed?: number): tf.Model {
    let init = tf.initializers.varianceScaling({ scale: scale, mode: 'fanIn', distribution: 'normal', seed })
    let binit = tf.initializers.zeros() // tf.initializers.randomNormal({ mean: 0, stddev: 4 })
    let input = tf.input({ shape: [ 4 ] })

    let x: tf.SymbolicTensor = input

    switch (type) {
        case "densenet":
            x = densenet(x, init);
            break;
        case "perceptron":
            x = perceptron(x, init);
            break;
        case "resnet":
            x = resnet(x, init);
            break;
    }

    x = tf.layers.dense({ units: bw ? 1 : 3, activation: 'tanh', kernelInitializer: tf.initializers.glorotNormal({ seed }) }).apply(x) as tf.SymbolicTensor

    return tf.model({ inputs: input, outputs: x })
}
開發者ID:gyngyn1234,項目名稱:blog,代碼行數:23,代碼來源:model.ts

示例8: Scene

window.tf = tf;

const scene = new Scene();
const renderer = new Renderer(scene);

console.time('go!');
for (let i = 0; i < 60 * 10; i++) {}
console.timeEnd('go!');
console.log('constraints', scene.constraints.length);
console.log(scene.getPositions().length);
console.log(scene.fitness);

const model = tf.sequential({
  layers: [
    tf.layers.dense({ units: 32, inputShape: [24], activation: 'relu' }),
    tf.layers.dense({ units: 10 }),
  ],
});

model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' });

window.model = model;

const frame = function() {
  scene.step();

  tf.tidy(() => {
    const output = model.predictOnBatch(
      tf.tensor2d(scene.getPositions(), [1, 24])
    ) as tf.Tensor<tf.Rank.R1>;
開發者ID:Vakrim,項目名稱:not_so_ragdoll,代碼行數:30,代碼來源:index.ts


注:本文中的@tensorflow/tfjs.layers類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。