当我尝试运行代码时,我正在跟踪有关如何使用brain.js使用神经网络的教程,它为我提供了ReferenceError:未定义大脑,我已经通过npm安装并在HTML上安装了CDN。
const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
const trainingData = [
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] }
];
net.train(trainingData);
console.log(net.run([0, 0]));
这是index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="//unpkg.com/brain.js"></script>
<script src="index.js"></script>
</head>
<body>
</body>
</html>
````
答案 0 :(得分:1)
尝试添加显式协议https://
,而不仅仅是使用//
。当我这样做时,您的代码对我有用。
const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
const trainingData = [
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] }
];
net.train(trainingData);
console.log(net.run([0, 0]));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/brain.js"></script>
<script src="index.js"></script>
</head>
<body>
</body>
</html>