This is my code to do this operation.
import {
tensor,
tensor2d,
} from '@tensorflow/tfjs-node'
import { readFile, writeFile } from 'node:fs'
const path2File = './SAVED-TENSOR/obj.json'
//------------------------------------- 2D ------------------------------------
const a = [
[
0.9969421029090881,
9.39412784576416,
95.00736999511719
]
]
const inputs2dT = tensor2d(a)
console.log(`@TENSOR >> `, inputs2dT.dataSync())
// @TENSOR >> Float32Array(3) [
// 0.9969421029090881,
// 9.39412784576416,
// 95.00736999511719
// ]
const aa = await inputs2dT.array()
console.log(aa)
// [ [ 0.9969421029090881, 9.39412784576416, 95.00736999511719 ] ]
const aaObj = {
"tensor": aa
}
writeFile(
path2File,
JSON.stringify(aaObj),
(err) => {
if (err) throw err
console.log('@DATA >> Written!')
}
)
readFile(path2File, (err, rawData) => {
if (err) throw err
const obj = JSON.parse(rawData)
console.log('@DATA >> ', obj.tensor)
const t = tensor(obj.tensor)
if (t.constructor.name === 'Tensor') {
t.print()
} else {
console.log('@UNDEFINED >> Tensor')
}
})