我用来model.predict()通过tensorflow.js输出张量A(大小:512 * 512 * 3),然后将其重塑为A.reshape(512 * 512 * 3)。但现在我想将此张量转换为数组,以便我可以将它与three.js. 如何解决这个问题呢?
要将张量转换为数组,您可以使用
data()或者dataSync()有一个扁平化的类型数组但目前支持的类型有float32, int32; 因此相应的 typedArray 将是 Float32Array 和 Int32Array。typedarray 构造函数可用于更改 typedarray 的类型
a = tf.tensor([1, 2, 3, 4])
buffer = a.dataSync().buffer
console.log(new Uint8Array(buffer))
console.log(new Uint16Array(buffer))
console.log(new Float32Array(buffer))
// To retrieve easily uint8 type, one can cast the tensor to `int32`
a = tf.tensor([1, 2, 3, 4], undefined, 'int32')
console.log(a.dtype)
buffer = a.dataSync().buffer
console.log(new Uint8Array(buffer))
console.log(new Float32Array(buffer))Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0"> </script>
</head>
<body>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2001 次 |
| 最近记录: |