Tensorflow JS - 将张量转换为 JSON 并返回张量
Tensorflow JS - converting tensor to JSON and back to tensor
我正在分批训练一个模型,因此我将其权重保存到 JSON 到 store/send。
我现在需要将它们重新加载到张量中 - 有正确的方法吗?
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)
下面这个可以解决这个问题,因为你可以导出文本格式的权重 'showWeights' 以将其保存在数据库中,例如文本文件或浏览器存储,然后你可以再次应用到你的模型中'setWeightsFromString'.
showWeights() {
tf.tidy(() => {
const weights = this.model.getWeights();
let pesos = '';
let shapes = '';
for (let i = 0; i < weights.length; i++) {
let tensor = weights[i];
let shape = weights[i].shape;
let values = tensor.dataSync().slice();
if (pesos) pesos += ';';
if (shapes) shapes += ';';
pesos += values;
shapes += shape;
}
console.log(pesos); // sValues for setWeightsFromString
console.log(shapes); // sShapes for setWeightsFromString
});
}
setWeightsFromString(sValues,sShapes) {
tf.tidy(() => {
const aValues = sValues.split(';');
const aShapes = sShapes.split(';');
const loadedWeights = [];
for (let i = 0 ; i < aValues.length ; i++) {
const anValues = aValues[i].split(',').map((e) => {return Number(e)});
const newValues = new Float32Array(anValues);
const newShapes = aShapes[i].split(',').map((e) => {return Number(e)});
loadedWeights[i] = tf.tensor(newValues, newShapes);
}
this.model.setWeights(loadedWeights);
});
}
我正在分批训练一个模型,因此我将其权重保存到 JSON 到 store/send。
我现在需要将它们重新加载到张量中 - 有正确的方法吗?
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)
下面这个可以解决这个问题,因为你可以导出文本格式的权重 'showWeights' 以将其保存在数据库中,例如文本文件或浏览器存储,然后你可以再次应用到你的模型中'setWeightsFromString'.
showWeights() {
tf.tidy(() => {
const weights = this.model.getWeights();
let pesos = '';
let shapes = '';
for (let i = 0; i < weights.length; i++) {
let tensor = weights[i];
let shape = weights[i].shape;
let values = tensor.dataSync().slice();
if (pesos) pesos += ';';
if (shapes) shapes += ';';
pesos += values;
shapes += shape;
}
console.log(pesos); // sValues for setWeightsFromString
console.log(shapes); // sShapes for setWeightsFromString
});
}
setWeightsFromString(sValues,sShapes) {
tf.tidy(() => {
const aValues = sValues.split(';');
const aShapes = sShapes.split(';');
const loadedWeights = [];
for (let i = 0 ; i < aValues.length ; i++) {
const anValues = aValues[i].split(',').map((e) => {return Number(e)});
const newValues = new Float32Array(anValues);
const newShapes = aShapes[i].split(',').map((e) => {return Number(e)});
loadedWeights[i] = tf.tensor(newValues, newShapes);
}
this.model.setWeights(loadedWeights);
});
}