• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript deeplearn.scalar函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中deeplearn.scalar函数的典型用法代码示例。如果您正苦于以下问题:TypeScript scalar函数的具体用法?TypeScript scalar怎么用?TypeScript scalar使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了scalar函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: onePlusOne

async function onePlusOne() {
  const a = dl.scalar(1);
  const b = dl.scalar(1);

  const result = await a.add(b).data();

  document.getElementById('output').innerText = result.toString();
}
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:8,代码来源:one_plus_one.ts


示例2:

    await dl.tidy(async () => {
      /**
       * Inference
       */
      // Now we ask the dl.Graph to evaluate (infer) and give us the result when
      // providing a value 4 for "x".
      // NOTE: "a", "b", and "c" are randomly initialized, so this will give us
      // something random.
      let result = session.eval(y, [{tensor: x, data: dl.scalar(4)}]);
      console.log(await result.data());

      /**
       * Training
       */
      // Now let's learn the coefficients of this quadratic given some data.
      // To do this, we need to provide examples of x and y.
      // The values given here are for values a = 3, b = 2, c = 1, with random
      // noise added to the output so it's not a perfect fit.
      const xs = [dl.scalar(0), dl.scalar(1), dl.scalar(2), dl.scalar(3)];
      const ys =
          [dl.scalar(1.1), dl.scalar(5.9), dl.scalar(16.8), dl.scalar(33.9)];
      // When training, it's important to shuffle your data!
      const shuffledInputProviderBuilder =
          new dl.InCPUMemoryShuffledInputProviderBuilder([xs, ys]);
      const [xProvider, yProvider] =
          shuffledInputProviderBuilder.getInputProviders();

      // Training is broken up into batches.
      const NUM_BATCHES = 20;
      const BATCH_SIZE = xs.length;
      // Before we start training, we need to provide an optimizer. This is the
      // object that is responsible for updating weights. The learning rate
      // param is a value that represents how large of a step to make when
      // updating weights. If this is too big, you may overstep and oscillate.
      // If it is too small, the model may take a long time to train.
      const LEARNING_RATE = .01;
      const optimizer = dl.train.sgd(LEARNING_RATE);
      for (let i = 0; i < NUM_BATCHES; i++) {
        // Train takes a cost dl.Tensor to minimize; this call trains one batch
        // and returns the average cost of the batch as a dl.Scalar.
        const costValue = session.train(
            cost,
            // Map input providers to Tensors on the dl.Graph.
            [{tensor: x, data: xProvider}, {tensor: yLabel, data: yProvider}],
            BATCH_SIZE, optimizer, dl.CostReduction.MEAN);

        console.log(`average cost: ${await costValue.data()}`);
      }

      // Now print the value from the trained model for x = 4, should be ~57.0.
      result = session.eval(y, [{tensor: x, data: dl.scalar(4)}]);
      console.log('result should be ~57.0:');
      console.log(await result.data());
    });
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:54,代码来源:ml_beginners.ts


示例3:

  await dl.tidy(async () => {
    const forgetBias = dl.scalar(1.0);
    const lstm1 = (data: dl.Tensor2D, c: dl.Tensor2D, h: dl.Tensor2D) =>
        dl.basicLSTMCell(forgetBias, lstmKernel1, lstmBias1, data, c, h);
    const lstm2 = (data: dl.Tensor2D, c: dl.Tensor2D, h: dl.Tensor2D) =>
        dl.basicLSTMCell(forgetBias, lstmKernel2, lstmBias2, data, c, h);

    let c: dl.Tensor2D[] = [
      dl.zeros([1, lstmBias1.shape[0] / 4]),
      dl.zeros([1, lstmBias2.shape[0] / 4])
    ];
    let h: dl.Tensor2D[] = [
      dl.zeros([1, lstmBias1.shape[0] / 4]),
      dl.zeros([1, lstmBias2.shape[0] / 4])
    ];

    let input = primerData;
    for (let i = 0; i < expected.length; i++) {
      const onehot = dl.oneHot(dl.tensor1d([input]), 10);

      const output = dl.multiRNNCell([lstm1, lstm2], onehot, c, h);

      c = output[0];
      h = output[1];

      const outputH = h[1];
      const logits =
          outputH.matMul(fullyConnectedWeights).add(fullyConnectedBiases);

      const result = await dl.argMax(logits).val();
      results.push(result);
      input = result;
    }
  });
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:34,代码来源:lstm.ts


示例4: getUnaryOp

function getUnaryOp(option: string) {
  switch (option) {
    case 'log':
      return (x: dl.Tensor) => x.log();
    case 'exp':
      return (x: dl.Tensor) => x.exp();
    case 'neg':
      return (x: dl.Tensor) => x.neg();
    case 'ceil':
      return (x: dl.Tensor) => x.ceil();
    case 'floor':
      return (x: dl.Tensor) => x.floor();
    case 'log1p':
      return (x: dl.Tensor) => x.log1p();
    case 'sqrt':
      return (x: dl.Tensor) => x.sqrt();
    case 'square':
      return (x: dl.Tensor) => x.square();
    case 'abs':
      return (x: dl.Tensor) => x.abs();
    case 'relu':
      return (x: dl.Tensor) => x.relu();
    case 'elu':
      return (x: dl.Tensor) => x.elu();
    case 'selu':
      return (x: dl.Tensor) => x.selu();
    case 'leakyRelu':
      return (x: dl.Tensor) => x.leakyRelu();
    case 'prelu':
      // TODO: Configurable from UI
      const alpha = dl.scalar(0.1);
      return (x: dl.Tensor) => x.prelu(alpha);
    case 'sigmoid':
      return (x: dl.Tensor) => x.sigmoid();
    case 'sin':
      return (x: dl.Tensor) => x.sin();
    case 'cos':
      return (x: dl.Tensor) => x.cos();
    case 'tan':
      return (x: dl.Tensor) => x.tan();
    case 'asin':
      return (x: dl.Tensor) => x.asin();
    case 'acos':
      return (x: dl.Tensor) => x.acos();
    case 'atan':
      return (x: dl.Tensor) => x.atan();
    case 'sinh':
      return (x: dl.Tensor) => x.sinh();
    case 'cosh':
      return (x: dl.Tensor) => x.cosh();
    case 'tanh':
      return (x: dl.Tensor) => x.tanh();
    case 'step':
      return (x: dl.Tensor) => x.step();
    default:
      throw new Error(`Not found such ops: ${option}`);
  }
}
开发者ID:kodamatomohiro,项目名称:tfjs-core,代码行数:58,代码来源:unary_ops_benchmark.ts


示例5:

    const img = dl.tidy(() => {
      const conv1 = this.convLayer(preprocessedInput.toFloat(), 1, true, 0);
      const conv2 = this.convLayer(conv1, 2, true, 3);
      const conv3 = this.convLayer(conv2, 2, true, 6);
      const resid1 = this.residualBlock(conv3, 9);
      const resid2 = this.residualBlock(resid1, 15);
      const resid3 = this.residualBlock(resid2, 21);
      const resid4 = this.residualBlock(resid3, 27);
      const resid5 = this.residualBlock(resid4, 33);
      const convT1 = this.convTransposeLayer(resid5, 64, 2, 39);
      const convT2 = this.convTransposeLayer(convT1, 32, 2, 42);
      const convT3 = this.convLayer(convT2, 1, false, 45);

      return convT3.tanh()
                 .mul(this.timesScalar)
                 .add(this.plusScalar)
                 .clip(0, 255)
                 .div(dl.scalar(255)) as dl.Tensor3D;
    });
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:19,代码来源:net.ts


示例6: resetRnn

function resetRnn() {
  c = [
    dl.zeros([1, lstmBias1.shape[0] / 4]),
    dl.zeros([1, lstmBias2.shape[0] / 4]),
    dl.zeros([1, lstmBias3.shape[0] / 4]),
  ];
  h = [
    dl.zeros([1, lstmBias1.shape[0] / 4]),
    dl.zeros([1, lstmBias2.shape[0] / 4]),
    dl.zeros([1, lstmBias3.shape[0] / 4]),
  ];
  if (lastSample != null) {
    lastSample.dispose();
  }
  lastSample = dl.scalar(PRIMER_IDX);
  currentPianoTimeSec = piano.now();
  pianoStartTimestampMs = performance.now() - currentPianoTimeSec * 1000;
  currentLoopId++;
  generateStep(currentLoopId);
}
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:20,代码来源:performance_rnn.ts


示例7: intro

// This file parallels (some of) the code in the introduction tutorial.

/**
 * 'Math with WebGL backend' section of tutorial
 */
async function intro() {
  const a = dl.tensor2d([1.0, 2.0, 3.0, 4.0], [2, 2]);
  const b = dl.tensor2d([0.0, 2.0, 4.0, 6.0], [2, 2]);

  const size = dl.scalar(a.size);

  // Non-blocking math calls.
  const average = a.sub(b).square().sum().div(size);

  console.log(`mean squared difference: ${await average.val()}`);

  /**
   * 'Graphs and Tensors' section of tutorial
   */

  const g = new dl.Graph();

  // Placeholders are input containers. This is the container for where we
  // will feed an input Tensor when we execute the graph.
  const inputShape = [3];
  const inputTensor = g.placeholder('input', inputShape);

  const labelShape = [1];
  const labelTensor = g.placeholder('label', labelShape);

  // Variables are containers that hold a value that can be updated from
  // training.
  // Here we initialize the multiplier variable randomly.
  const multiplier = g.variable('multiplier', dl.randomNormal([1, 3]));

  // Top level graph methods take Tensors and return Tensors.
  const outputTensor = g.matmul(multiplier, inputTensor);
  const costTensor = g.meanSquaredCost(labelTensor, outputTensor);

  // Tensors, like Tensors, have a shape attribute.
  console.log(outputTensor.shape);

  /**
   * 'dl.Session and dl.FeedEntry' section of the tutorial.
   */

  const learningRate = .00001;
  const batchSize = 3;

  const session = new dl.Session(g, dl.ENV.math);
  const optimizer = dl.train.sgd(learningRate);

  const inputs: dl.Tensor1D[] = [
    dl.tensor1d([1.0, 2.0, 3.0]), dl.tensor1d([10.0, 20.0, 30.0]),
    dl.tensor1d([100.0, 200.0, 300.0])
  ];

  const labels: dl.Tensor1D[] =
      [dl.tensor1d([4.0]), dl.tensor1d([40.0]), dl.tensor1d([400.0])];

  // Shuffles inputs and labels and keeps them mutually in sync.
  const shuffledInputProviderBuilder =
      new dl.InCPUMemoryShuffledInputProviderBuilder([inputs, labels]);
  const [inputProvider, labelProvider] =
      shuffledInputProviderBuilder.getInputProviders();

  // Maps tensors to InputProviders.
  const feedEntries: dl.FeedEntry[] = [
    {tensor: inputTensor, data: inputProvider},
    {tensor: labelTensor, data: labelProvider}
  ];

  const NUM_BATCHES = 10;
  for (let i = 0; i < NUM_BATCHES; i++) {
    // Wrap session.train in a scope so the cost gets cleaned up
    // automatically.
    await dl.tidy(async () => {
      // Train takes a cost tensor to minimize. Trains one batch. Returns the
      // average cost as a dl.Scalar.
      const cost = session.train(
          costTensor, feedEntries, batchSize, optimizer, dl.CostReduction.MEAN);

      console.log(`last average cost (${i}): ${await cost.val()}`);
    });
  }

  const testInput = dl.tensor1d([0.1, 0.2, 0.3]);

  // session.eval can take Tensors as input data.
  const testFeedEntries: dl.FeedEntry[] =
      [{tensor: inputTensor, data: testInput}];

  const testOutput = session.eval(outputTensor, testFeedEntries);

  console.log('---inference output---');
  console.log(`shape: ${testOutput.shape}`);
  console.log(`value: ${await testOutput.val(0)}`);
}
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:98,代码来源:intro.ts


示例8: require

import {KeyboardElement} from './keyboard_element';

// tslint:disable-next-line:no-require-imports
const Piano = require('tone-piano').Piano;

let lstmKernel1: dl.Tensor2D;
let lstmBias1: dl.Tensor1D;
let lstmKernel2: dl.Tensor2D;
let lstmBias2: dl.Tensor1D;
let lstmKernel3: dl.Tensor2D;
let lstmBias3: dl.Tensor1D;
let c: dl.Tensor2D[];
let h: dl.Tensor2D[];
let fcB: dl.Tensor1D;
let fcW: dl.Tensor2D;
const forgetBias = dl.scalar(1.0);
const activeNotes = new Map<number, number>();

// How many steps to generate per generateStep call.
// Generating more steps makes it less likely that we'll lag behind in note
// generation. Generating fewer steps makes it less likely that the browser UI
// thread will be starved for cycles.
const STEPS_PER_GENERATE_CALL = 10;
// How much time to try to generate ahead. More time means fewer buffer
// underruns, but also makes the lag from UI change to output larger.
const GENERATION_BUFFER_SECONDS = .5;
// If we're this far behind, reset currentTime time to piano.now().
const MAX_GENERATION_LAG_SECONDS = 1;
// If a note is held longer than this, release it.
const MAX_NOTE_DURATION_SECONDS = 3;
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:30,代码来源:performance_rnn.ts


示例9: constructor

 constructor(private style: string) {
   this.variableDictionary = {};
   this.timesScalar = dl.scalar(150);
   this.plusScalar = dl.scalar(255. / 2);
   this.epsilonScalar = dl.scalar(1e-3);
 }
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:6,代码来源:net.ts


示例10: mlBeginners

async function mlBeginners() {
  const math = dl.ENV.math;

  // This file parallels (some of) the code in the ML Beginners tutorial.
  {
    const matrixShape: [number, number] = [2, 3];  // 2 rows, 3 columns.
    const matrix = dl.tensor2d([10, 20, 30, 40, 50, 60], matrixShape);
    const vector = dl.tensor1d([0, 1, 2]);
    const result = dl.matrixTimesVector(matrix, vector);

    console.log('result shape:', result.shape);
    console.log('result', await result.data());
  }

  {
    const g = new dl.Graph();
    // Make a new input in the dl.Graph, called 'x', with shape [] (a
    // dl.Scalar).
    const x = g.placeholder('x', []);
    // Make new variables in the dl.Graph, 'a', 'b', 'c' with shape [] and
    // random initial values.
    const a = g.variable('a', dl.scalar(Math.random()));
    const b = g.variable('b', dl.scalar(Math.random()));
    const c = g.variable('c', dl.scalar(Math.random()));
    // Make new tensors representing the output of the operations of the
    // quadratic.
    const order2 = g.multiply(a, g.square(x));
    const order1 = g.multiply(b, x);
    const y = g.add(g.add(order2, order1), c);

    // When training, we need to provide a label and a cost function.
    const yLabel = g.placeholder('y label', []);
    // Provide a mean squared cost function for training. cost = (y - yLabel)^2
    const cost = g.meanSquaredCost(y, yLabel);

    // At this point the dl.Graph is set up, but has not yet been evaluated.
    // **deeplearn.js** needs a dl.Session object to evaluate a dl.Graph.
    const session = new dl.Session(g, math);

    await dl.tidy(async () => {
      /**
       * Inference
       */
      // Now we ask the dl.Graph to evaluate (infer) and give us the result when
      // providing a value 4 for "x".
      // NOTE: "a", "b", and "c" are randomly initialized, so this will give us
      // something random.
      let result = session.eval(y, [{tensor: x, data: dl.scalar(4)}]);
      console.log(await result.data());

      /**
       * Training
       */
      // Now let's learn the coefficients of this quadratic given some data.
      // To do this, we need to provide examples of x and y.
      // The values given here are for values a = 3, b = 2, c = 1, with random
      // noise added to the output so it's not a perfect fit.
      const xs = [dl.scalar(0), dl.scalar(1), dl.scalar(2), dl.scalar(3)];
      const ys =
          [dl.scalar(1.1), dl.scalar(5.9), dl.scalar(16.8), dl.scalar(33.9)];
      // When training, it's important to shuffle your data!
      const shuffledInputProviderBuilder =
          new dl.InCPUMemoryShuffledInputProviderBuilder([xs, ys]);
      const [xProvider, yProvider] =
          shuffledInputProviderBuilder.getInputProviders();

      // Training is broken up into batches.
      const NUM_BATCHES = 20;
      const BATCH_SIZE = xs.length;
      // Before we start training, we need to provide an optimizer. This is the
      // object that is responsible for updating weights. The learning rate
      // param is a value that represents how large of a step to make when
      // updating weights. If this is too big, you may overstep and oscillate.
      // If it is too small, the model may take a long time to train.
      const LEARNING_RATE = .01;
      const optimizer = dl.train.sgd(LEARNING_RATE);
      for (let i = 0; i < NUM_BATCHES; i++) {
        // Train takes a cost dl.Tensor to minimize; this call trains one batch
        // and returns the average cost of the batch as a dl.Scalar.
        const costValue = session.train(
            cost,
            // Map input providers to Tensors on the dl.Graph.
            [{tensor: x, data: xProvider}, {tensor: yLabel, data: yProvider}],
            BATCH_SIZE, optimizer, dl.CostReduction.MEAN);

        console.log(`average cost: ${await costValue.data()}`);
      }

      // Now print the value from the trained model for x = 4, should be ~57.0.
      result = session.eval(y, [{tensor: x, data: dl.scalar(4)}]);
      console.log('result should be ~57.0:');
      console.log(await result.data());
    });
  }
}
开发者ID:ScapeQin,项目名称:deeplearnjs,代码行数:95,代码来源:ml_beginners.ts



注:本文中的deeplearn.scalar函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript deeplearn.setBackend函数代码示例发布时间:2022-05-25
下一篇:
TypeScript deeplearn.randomUniform函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap