扫一扫分享
Node.js和浏览器的灵活神经网络库,基本上学习如何进行预测,使用矩阵实现来处理训练数据并启用可配置的网络拓扑。您还可以即插即用已经学过的“思想”,这对您的应用程序非常有用。
yarn add node-mind
使用
const Mind = require('node-mind');
/**
* Letters.
*
* - Imagine these # and . represent black and white pixels.
*/
const a = character(
'.#####.' +
'#.....#' +
'#.....#' +
'#######' +
'#.....#' +
'#.....#' +
'#.....#'
)
const b = character(
'######.' +
'#.....#' +
'#.....#' +
'######.' +
'#.....#' +
'#.....#' +
'######.'
)
const c = character(
'#######' +
'#......' +
'#......' +
'#......' +
'#......' +
'#......' +
'#######'
)
/**
* Learn the letters A through C.
*/
const mind = new Mind({ activator: 'sigmoid' })
.learn([
{ input: a, output: map('a') },
{ input: b, output: map('b') },
{ input: c, output: map('c') }
])
/**
* Predict the letter C, even with a pixel off.
*/
const result = mind.predict(character(
'#######' +
'#......' +
'#......' +
'#......' +
'#......' +
'##.....' +
'#######'
))
console.log(result) // ~ 0.5
/**
* Turn the # into 1s and . into 0s.
*/
function character(string) {
return string
.trim()
.split('')
.map(integer)
function integer(symbol) {
if ('#' === symbol) return 1
if ('.' === symbol) return 0
}
}
/**
* Map letter to a number.
*/
function map(letter) {
if (letter === 'a') return [ 0.1 ]
if (letter === 'b') return [ 0.3 ]
if (letter === 'c') return [ 0.5 ]
return 0
}
手机预览