已移植到 TensorFlow.js 的预训练 Tensorflow 模型的权重是否可以在运行时通过模型对象访问?
Are the weights of a pre-trained Tensorflow model that has been ported to TensorFlow.js accessible at runtime through the model object?
如何在调试期间访问模型的权重?
当我在调试器中执行期间检查 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/tfjs@1.0"></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 中是 class,它在 js 中被转换为 Function
类型的对象。这就是为什么 console.log(model.model.weights['dense_3/bias'][0])
将打印一个对象,其键是 class 张量的属性。需要调用 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/tfjs@1.0"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>
</head>
<body>
</body>
</html>
从另一个 我能够推导出如何访问权重。
对于每一层都有一个 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/tfjs@1.0"></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>
如何在调试期间访问模型的权重?
当我在调试器中执行期间检查 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/tfjs@1.0"></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 中是 class,它在 js 中被转换为 Function
类型的对象。这就是为什么 console.log(model.model.weights['dense_3/bias'][0])
将打印一个对象,其键是 class 张量的属性。需要调用 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/tfjs@1.0"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"></script>
</head>
<body>
</body>
</html>
从另一个
对于每一层都有一个 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/tfjs@1.0"></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>