如何从2D张量获取数据? [英] How to get data from 2D tensor?

查看:48
本文介绍了如何从2D张量获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 tensorflow.js 从2D张量获取数据.我试图像这样使用 data()方法:

I would like to get the data from a 2D tensor with tensorflow.js. I tried to use the data() method like this:

const X = tf.tensor2d([[1, 2, 3, 4], [2, 2, 5, 3]]);
X.data().then(X => console.log(X)};

但是结果是一个扁平的一维数组:

But the result is a flatten 1D array:

Float32Array(8) [1, 2, 3, 4, 2, 2, 5, 3]

有没有办法保持阵列的形状?

Is there a way to keep the shape of the array?

推荐答案

您可以在张量对象上使用arraySync方法.它以与同步之前相同的形状返回数组.

You can use arraySync method on the tensor object. It returns the array as the same shape it was before synchronously.

const X = tf.tensor2d([[1, 2, 3, 4], [2, 2, 5, 3]]); 
console.log(X.arraySync())

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.7/dist/tf.min.js"></script>

这篇关于如何从2D张量获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆