4 Star 18 Fork 1

Hans / infers

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

infers

Machine learning and Matrix operation library by TypeScript.

Installed

Make sure NPM is installed, Switch to the project directory then execute the following command.

$ npm install infers@latest

Reference in project:

import { Matrix, BPNet } from 'infers'

Examples

Matrix transpose:

let m = new Matrix([
  [1, 5, 0],
  [2, 4 , -1],
  [0, -2, 0]
])
m.T.print()
// Matrix 3x3 [
//  1, 2, 0, 
//  5, 4, -2, 
//  0, -1, 0, 
// ]

BP neural network example of XOR, three-layer network:

let xs = new Matrix([[1, 0], [0, 1], [0, 0], [1, 1]])
let ys = new Matrix([[1], [1], [0], [0]])
let model = new BPNet([2, [6, 'Tanh'], [1, 'Sigmoid']], { rate: 0.1 })
model.fit(xs, ys, {
  epochs: 5000, onEpoch: (epoch, loss) => {
    if (epoch % 100 === 0) console.log('epoch:' + epoch, 'loss:', loss)
  }
})
model.predict(xs).print()
// Matrix 4x1 [
//  0.9862025352830867, 
//  0.986128496195502, 
//  0.01443800549676924, 
//  0.014425871504885788, 
// ]

BP neural network example of addition, four-layer network:

let xs = new Matrix([[1, 4], [3, 2], [6, 5], [4, 7]])
let ys = new Matrix([[5], [5], [11], [11]])
let model = new BPNet([2, 6, 6, 1], { mode: 'bgd', rate: 0.01 })
model.fit(xs, ys, {
  epochs: 500, onEpoch: (epoch, loss) => {
    console.log('epoch:' + epoch, 'loss:', loss)
  }
})
let xs2 = new Matrix([[5, 8], [22, 6], [-5, 9], [-5, -4]])
model.predict(xs2).print()
// Matrix 4x1 [
//  12.994745740521667, 
//  27.99134620596921, 
//  3.9987224114576856, 
//  -9.000000644547901,
// ]

RNN: Recurrent neural network example:

let trainData = ['hello rnn', 'good morning', 'I love 🍎!', 'I eat 🍊!']
let net = new RNN({ trainData })
net.fit({
  epochs: 1500, onEpochs: (epoch, loss) => {
    if (epoch % 10 === 0) console.log('epoch: ', epoch, 'loss: ', loss)
  }
})
console.log(net.predict('I love'))
console.log(net.predict('I eat'))
console.log(net.predict('hel'))
console.log(net.predict('good'))
//  🍊!/n
//  🍎!/n
// lo rnn/n
//  morning/n

API

  • NetShape: [number, (number | [number, ActivationFunction]), ...(number | [number, ActivationFunction])[]]
    The hierarchical structure of the network model, It includes the number of neurons in each layer, the type of activation function and the total number of layers.
  • rate: number
    The learning rate is the update step of every gradient descent, generally between 0 and 1.
  • epochs: number
    All the data of the whole training set are iterated once.
  • ActivationFunction: 'Sigmoid' | 'Relu' | 'Tanh' | 'Softmax'
  • Mode: 'sgd' | 'bgd' | 'mbgd'

Different learning rates, iterations and network shapes are needed to deal with different problems, which need to be adjusted according to the cost function. Parameter optimization is also the process of model optimization.

Export

  • class Matrix
    • Mathematical operation of matrix
    • addition, multiply, transpose, determinant, inverse
  • class BPNet
    • Fully connected neural network
    • Multi-layer network model
  • class RNN
    • Recurrent neural network
    • Used natural language processing
MIT License Copyright (c) 2021 ounana Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Machine learning and Matrix operation library by TypeScript. 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
TypeScript
1
https://gitee.com/hans_s/infers.git
git@gitee.com:hans_s/infers.git
hans_s
infers
infers
main

搜索帮助