【问题标题】:How can I convert a pre-existing tensor to another data type in Tensorflow.js?如何将预先存在的张量转换为 Tensorflow.js 中的另一种数据类型?
【发布时间】:2021-02-05 18:52:40
【问题描述】:

我正在使用 Tensorflow.js,我想将一个 预先存在的张量转换为另一种数据类型。例如,我可能有一个 int32 张量,我想将其转换为 float32 张量。

据记载,可以在创建时设置张量的类型,如下所示:

let a = tf.tensor([1,2,3], null, "int32");

....但我无法找到在预先存在的张量上运行的 API 调用 in the documentation

【问题讨论】:

    标签: javascript tensor tensorflow.js


    【解决方案1】:

    它隐藏在 API 文档中,但确实存在这样的方法。您可以像这样将张量从一种类型转换为另一种类型:

    let example_tensor = tf.tensor([1,2,3]); // dtype is currently "float32"
    
    example_tensor.cast("int32"); // now the dtype is "int32"
    example_tensor.cast("bool"); // now the btype is "bool"
    

    API 文档链接:https://js.tensorflow.org/api/latest/#cast

    作为参考,据我所知,存在以下 dtypes(“数据类型”的缩写):

    • 'float32'
    • 'int32'
    • '布尔'
    • 'complex64'
    • '字符串'

    (source)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2023-02-07
      相关资源
      最近更新 更多