📜  Tensorflow.js tf.keep()函数

📅  最后修改于: 2022-05-13 01:56:20.204000             🧑  作者: Mango

Tensorflow.js tf.keep()函数

Tensorflow.js 是一个由谷歌开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。

.keep()函数用于防止在 tf.tidy() 方法中形成的张量输入被自发丢弃。

句法:

tf.keep(result)

参数:

  • 结果:要防止丢弃的是规定的张量输入。

返回值:返回 tf.Tensor 对象。

示例 1:

Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring a variable
let res1;
  
// Calling tidy method
const res2 = tf.tidy(() => {
     
  // Defining result parameter
  const result = tf.scalar(121);
     
  // Calling tf.keep() method
  res1 = tf.keep(result.sqrt());
});
  
// Printing output
res1.print();


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring a variable
let res1;
  
// Calling tidy method
const res2 = tf.tidy(() => {
     
  // Calling tf.keep() method with its
  // parameter
  res1 = tf.keep(tf.tensor1d(
    [1.3, 0.5, 0, NaN, null, -.5]).cos());
});
  
// Printing output
res1.print();


输出:

Tensor
    11

示例 2:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring a variable
let res1;
  
// Calling tidy method
const res2 = tf.tidy(() => {
     
  // Calling tf.keep() method with its
  // parameter
  res1 = tf.keep(tf.tensor1d(
    [1.3, 0.5, 0, NaN, null, -.5]).cos());
});
  
// Printing output
res1.print();

输出:

Tensor
    [0.2675007, 0.8775977, 1, NaN, 1, 0.8775977]

参考: https://js.tensorflow.org/api/latest/#keep