<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=windows-1252" http-equiv="Content-Type">
<!-- <script src="//unpkg.com/brain.js"></script> -->
<script src="brain2.js"></script>
<title>javascript深度学习智能ai体验brain.js</title>
</head>
<body>
<h1>javascript深度学习智能ai体验brain.js</h1>
<div id="democolor"></div>
<div id="demo"></div>
<script>
// 根据RGB学习判断颜色深浅的示例
// 创建一个神经网络
const net = new brain.NeuralNetwork();
// 使用 4 个输入对象训练网络
net.train([
// White RGB(255, 255, 255)
{ input: [255 / 255, 255 / 255, 255 / 255], output: { light: 1 } },
// 浅灰色 (192,192,192)
{ input: [192 / 255, 192 / 255, 192 / 255], output: { light: 1 } },
// 深灰色 (64, 64, 64)
{ input: [65 / 255, 65 / 255, 65 / 255], output: { dark: 1 } },
// 黑色 (0, 0, 0)
{ input: [0, 0, 0], output: { dark: 1 } },
// 纠正一些错误的参数
{
"input": [
0.5137254901960784,
0.10588235294117647,
0.8901960784313725
],
"output": {
"dark": 1
}
},
]);
let htmlstr = ''
let resarr = []
for (let i = 0; i < 18; i++) {
const rgb = [
Math.round(Math.random() * 255),
Math.round(Math.random() * 255),
Math.round(Math.random() * 255),
]
const res = net.run([rgb[0] / 255, rgb[1] / 255, rgb[2] / 255]);
let fontcolor = '#ffffff'
let output = 'dark'
if (res.light > res.dark) {
fontcolor = '#000000'
output = 'light'
}
resarr.push({ input: rgb.map((el) => el / 255), output: { [output]: 1 } })
console.log(666.2001, rgb, res)
htmlstr += '<b style=background-color:RGB(' + rgb.join(',') + ');color:' + fontcolor + '>(' + (i + 1) + ') 文字Abc</b>'
}
console.log(666.2002, resarr)
document.getElementById("democolor").innerHTML = htmlstr
// 官方示例
// Create a Neural Network
const network = new brain.NeuralNetwork();
// Train the Network with 4 input objects
network.train([
{ input: [0, 0], output: { zero: 1 } },
{ input: [0, 1], output: { one: 1 } },
{ input: [1, 0], output: { one: 1 } },
{ input: [1, 1], output: { zero: 1 } },
]);
// What is the expected output of [1,0]?
let result = network.run([1, 0]);
// Display the probability for "zero" and "one"
document.getElementById("demo").innerHTML =
"one: " + result["one"] + "<br>" + "zero: " + result["zero"];
</script>
</body>
</html>
javascript深度学习智能ai体验brain.js
最新推荐文章于 2024-04-18 10:12:49 发布