已经移植到TensorFlow.js的预先训练过的Tensorflow模型的权重是否可以在运行时通过模型对象访问?

问题描述 投票:1回答:2

如何在调试期间访问模型的权重?

当我在调试器中执行期间检查model.model.weights['dense_3/bias'][0]时,实际权重不存在。然而,当我console.log表达权重打印。似乎有某种延迟执行正在进行中?

我在下面创建了一个基于有毒分类器medium article的片段,它显示了如何访问特定图层的权重对象。

const threshold = 0.9;

// Which toxicity labels to return.
const labelsToInclude = ['identity_attack', 'insult', 'threat'];

toxicity.load(threshold, labelsToInclude).then(model => {
    // Now you can use the `model` object to label sentences. 
    model.classify(['you suck']).then(predictions => {
    console.log("Specific weights: "+ model.model.weights['dense_3/bias'][0])
      document.getElementById("predictions").innerHTML =  JSON.stringify(predictions, null, 2);
    });
});
<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="UTF-8">
  <title>Activity 1: Basic HTML Bio</title>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>
</head>

<body>
<div id="predictions">
Will be populated by prebuilt toxicity model
</div>
</body>

</html>
javascript tensorflow tensorflow.js
2个回答
1
投票

张量数组中的每一层。可以通过遍历数组来访问层的权重。

const t = model.model.weights['dense_3/bias'][0] // t is a tensor
t.print() // will display the tensor in the console
// to add value to the weight
t.add(tf.scalar(0.5))

console.log(model.model.weights ['dense_3 / bias'] [0])将显示一个对象,而不是张量的值。原因是张量是TypeScript中的一个类,它在js中被转换为Function类型的对象。这就是为什么console.log(model.model.weights['dense_3/bias'][0])会打印一个对象,其中键是类张量的属性。需要调用print方法来查看张量的基础值

const threshold = 0.9;

// Which toxicity labels to return.
const labelsToInclude = ['identity_attack', 'insult', 'threat'];

toxicity.load(threshold, labelsToInclude).then(model => {
    // print weights
    model.model.weights['dense_3/bias'][0].print()
    // continue processing
});
<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="UTF-8">
  <title>Activity 1: Basic HTML Bio</title>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>
</head>

<body>
</body>

</html>

如果你想获得cpu上的张量值并使用dom元素的innerHTML显示它,你可以考虑使用datadataSync


0
投票

从另一个post我能够得出如何访问权重。

对于每一层,都有一个data承诺可以访问权重。

const threshold = 0.9;

// Which toxicity labels to return.
const labelsToInclude = ['identity_attack', 'insult', 'threat'];

toxicity.load(threshold, labelsToInclude).then(model => {
    // Now you can use the `model` object to label sentences. 
    model.classify(['you suck']).then(predictions => {
      model.model.weights['dense_3/bias'][0].data().then(
        function(value) {
        document.getElementById("specific_weights").innerHTML = JSON.stringify(value);
        });
      
      document.getElementById("predictions").innerHTML =  JSON.stringify(predictions, null, 2);
    });
});
<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="UTF-8">
  <title>Activity 1: Basic HTML Bio</title>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>
</head>

<body>
<div id="predictions">
Will be populated by prebuilt toxicity model
</div>
<div id="specific_weights">
Will contain weights for specific layer
</div>

</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.