如何在调试期间访问模型的权重?
当我在调试器中执行期间检查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>
张量数组中的每一层。可以通过遍历数组来访问层的权重。
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>
从另一个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>