【发布时间】:2020-12-18 02:15:40
【问题描述】:
我正在使用 ml5.js,它是 tensorflowjs 的包装器。我想在浏览器中训练一个神经网络,下载权重,在 pyTorch 中将它们作为张量处理,然后将它们加载回浏览器的 tensorflowjs 模型中。如何在这些格式之间转换tfjs <-> pytorch?
浏览器模型有一个save() 函数,它生成三个文件。特定于 ml5.js 的元数据文件 (json)、描述模型架构的拓扑文件 (json) 和二进制权重文件 (bin)。
// Browser
model.save()
// HTTP/Download
model_meta.json (needed by ml5.js)
model.json (needed by tfjs)
model.weights.bin (needed by tfjs)
# python backend
import json
with open('model.weights.bin', 'rb') as weights_file:
with open('model.json', 'rb') as model_file:
weights = weights_file.read()
model = json.loads(model_file.read())
####
pytorch_tensor = convert2tensor(weights, model) # whats in this function?
####
# Do some processing in pytorch
####
new_weights_bin = convert2bin(pytorch_tensor, model) # and in this?
####
Here is sample javascript code 在浏览器中生成并加载这 3 个文件。要加载,请在对话框中一次选择所有 3 个文件。如果它们是正确的,弹出窗口将显示一个示例预测。
【问题讨论】:
标签: javascript pytorch tensorflow.js ml5