tensorflow - 如何在 tensorflow.js 中获取/设置监督模型的权重?

标签 tensorflow tensorflow.js

我想更改受监督模型的权重,但在更改权重后我得到了完全相同的结果。我做错了什么?

const model = tf.sequential();
model.add(tf.layers.dense({...}));
model.add(tf.layers.dense({...}));
model.add(tf.layers.dense({...}));
model.compile({...});
model.fit({});

const result1 = model.predict(tf.tensor2d(...)).dataSync();

const newWeights = [];
model.layers.map((layer, i) => {
  newWeights[i] = []
  const weights = layer.getWeights();
  newWeights[i][0] = weights[0].arraySync()
  newWeights[i][1] = weights[1].arraySync()

  newWeights[i][0].map(tensor => tensor.map(x => {
    if (random(1) < 0.5) {
      return x + offset();
    }

    return x;
  })

  layer.setWeights([tf.tensor2d(newWeights[i][0], [newWeights[i][0].length, newWeights[i][0][0].length]), tf.tensor(newWeights[i][1])])
})

const result2 = model.predict(tf.tensor2d(...)).dataSync();

代码片段:

const random = (max) => {
  return floor(Math.random() * Math.floor(max), 2);
} 

const floor = (num, toDecimal) => {
  let  dec = Math.pow(10, toDecimal);
  return Number(Math.floor(num * dec) / dec);
}

const offset = () => {
  randomGaussian() * 0.5
}

let previous = false;
let y2 = 0;
const randomGaussian = (mean, sd) => {
  let y1, x1, x2, w;
  if (previous) {
    y1 = y2;
    previous = false;
  } else {
    do {
      x1 = random(2) - 1;
      x2 = random(2) - 1;
      w = x1 * x1 + x2 * x2;
    } while (w >= 1);
    w = Math.sqrt(-2 * Math.log(w) / w);
    y1 = x1 * w;
    y2 = x2 * w;
    previous = true;
  }

  let m = mean || 0;
  let s = sd || 1;
  return y1 * s + m;
};

result1 === result2 但为什么呢?

最佳答案

新权重很可能与第一个模型的权重相同。

示例:更改模型权重的简单示例

(async() => {
const model = tf.sequential({
        layers: [tf.layers.dense({units: 1, inputShape: [10]})]
    });
    model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
    for (let i = 1; i < 5 ; ++i) {
      const h = await model.fit(tf.ones([8, 10]), tf.ones([8, 1]), {
          batchSize: 4,
          epochs: 3
      });
      console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
    }
    
    const p = await model.predict(tf.zeros([1, 10]))
    p.print()
    const layers = model.layers

    layers[0].setWeights([tf.zeros([10, 1]), tf.zeros([1])])
    
    const q = await model.predict(tf.zeros([1, 10]))
    q.print()


})()
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"> </script>
  </head>

  <body>
  </body>
</html>

代码问题

创建的 newWeights 未分配给 newWeightsmap 不是就地运算符。 map 返回的数组应分配回 newWeights

newWeights[i][0] = newWeights[i][0].map(tensor => tensor.map(x => {
    if (random(1) < 0.5) {
      return x + offset();
    }

    return x;
  })

关于tensorflow - 如何在 tensorflow.js 中获取/设置监督模型的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917450/

相关文章:

javascript - Tensorflow JS tfjs |无法使用 tf.loadLayersModel 加载模型

python - 为什么 TensorFlow 的 `tf.data` 包会减慢我的代码速度?

ubuntu - 在 Ubuntu 18.04 上构建具有 OpenCL 支持的 TensorFlow 失败

python - 为什么要使用 tf.data?

Tensorflow.js 错误 : Unknown layer: Functional

javascript - Uncaught Error : Based on the provided shape, [1024,3],张量应该有 3072 个值但有 30

node.js - 如何在 tensorflow.js 中构建和训练 lstm 网络

python - 使用Tensorflow对图像进行分类时如何获取识别对象的像素矩阵?

python - TF Keras 如何在加载模型时获得预期的输入形状?

javascript - 迁移学习 Tensorflow.js 大小/形状错误