我正在批量训练模型,因此将其权重保存到 JSON 中以存储/发送。
我现在需要将它们加载回张量 - 有正确的方法吗?
tensor.data().then(d => JSON.stringify(d));
// returns
{"0":0.000016666666851961054,"1":-0.00019999999494757503,"2":-0.000183333337190561}
我可以迭代这个并手动转换回数组 - 但感觉 API 中可能有一些东西可以让这个更干净?
最佳答案
不需要对 data() 的结果进行字符串化。为了保存张量并在以后恢复它,需要两件事:数据形状和数据扁平化数组。
s = tensor.shape
// get the tensor from backend
saved = {data: await s.data, shape: shape}
retrievedTensor = tf.tensor(saved.data, saved.shape)
使用 array 或 arraySync 时给出两条信息 - 生成的 typedarray 与张量具有相同的结构
saved = await tensor.array()
retrievedTensor = tf.tensor(saved)
关于javascript - Tensorflow JS - 将张量转换为 JSON,然后再转换回张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207573/