【发布时间】:2020-11-27 14:47:46
【问题描述】:
好吧,实际上我想做的是在 chrome 扩展中测试 tensorFlow.js,所以我在网页上使用 tensorFlow.js 制作了一个机器学习应用程序,然后在扩展中实现它。
我认为它好像没有识别 tensorFlow.js 的语法,因为 tf.sequential(); 已经是 tfjs 库的现成函数了。
有人可以帮我解决这个问题吗?
代码如下:
清单.json:
{
"name":"Teste implementacao extension",
"version":"1.0",
"description":"NULL for While",
"browser_action":{
"default_popup":"index.html"
},
"manifest_version":2,
"content_security_policy": "script-src 'self' https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js 'sha256-AJlm0gxYKWeBb5choQtViwczPIiEm/8RF2tRQOdGGR4='; object-src 'self'"
}
index.html:
<!DOCTYPE html>
<html>
<head>
<script src = "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.0/dist/tf.min.js">
</script>
</head>
<body>
<div id="output_field">
</div>
</body>
<script src="script.js"></script>
script.js
/*
Rede neural com javascript no navegador.
e treinou ela para prever uma regressão linear.
*/
async function learnLinear(){
//alert("dsds");
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({
loss: 'meanSquaredError',
optimizer: 'sgd'
});
const xs = tf.tensor2d([-1,0,1,2,3,4], [6,1]);
const ys = tf.tensor2d([-3,-1,1,3,5,7], [6,1]);
//treinando o modelo
await model.fit(xs, ys, {epochs: 100});//tempo pra corrigir os erros
//valor inicial: 250
document.getElementById('output_field').innerText =
model.predict(tf.tensor2d([20], [1,1]));
}
learnLinear();
【问题讨论】:
标签: javascript tensorflow google-chrome-extension tensorflow.js