获取张量内特定索引的值?

Getting values of specific indices inside tensor?

我正在学习 tensorflow.js Udemy 课程,老师在张量对象上使用了一个函数 get,并将行和列索引传递给 return 该位置的值。我无法在文档中找到此方法,它在 nodejs 中也不起作用,函数 get() 似乎不存在。

这是他的代码,他在自定义控制台的浏览器中 运行:https://stephengrider.github.io/JSPlaygrounds/

    const data = tf.tensor([
        [10, 20, 30],
        [40, 50, 60]
    ]);
    data.get(1, 2);  // returns 60 in video, in browser

这是我的代码,是我让它工作的唯一方法,但看起来真的很难看:

const tf = require('@tensorflow/tfjs-node');

(async () => {
    const data = tf.tensor([
        [10, 20, 30],
        [40, 50, 60]
    ]);
    let lastIndex = (await data.data())[5];
    console.log(lastIndex) // returns 60
})();

必须有更好的方法来访问特定索引处的值。 data() 方法只是 return 一个包含张量中所有值的数组,我无法找到一种方法来按行、列语法访问值。

getv0.15.0 后已弃用,并已从 v1.0.0 中删除。因此,在特定索引处检索值的唯一方法是使用

  • tf.slice 将 return 特定索引值的张量或

  • 如果您想将值检索为 javascript 数字,则可以使用

    • tf.data 和值的索引或

    • tf.array 和坐标

    • 使用tf.buffer

(async () => {
    const data = tf.tensor([
        [10, 20, 30],
        [40, 50, 60]
    ]);
    console.time()
    let lastIndex = (await data.data())[5];
    console.log(lastIndex) // returns 60
    console.timeEnd()
    
    // using slice
    console.time()
    data.slice([1, 2], [1, 1]).print()
    console.timeEnd()
    
    //using array and the coordinates
    console.time()
    const value = (await data.array())[1][2]
    console.log(value)
    console.timeEnd()
    
    // using buffer
    console.time()
    const buffer = await data.buffer()
    const value2 = buffer.get(1, 2)
    console.log(value2)
    console.timeEnd()
})();
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
  </head>

  <body>
  </body>
</html>