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

时间:2019-09-13 04:51:21

标签: 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,为什么?

1 个答案:

答案 0 :(得分:1)

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

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

(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;
  })