Skip to content

Latest commit

 

History

History
89 lines (64 loc) · 1.61 KB

README.md

File metadata and controls

89 lines (64 loc) · 1.61 KB

ts-ml

Setup

Uses Vite

yarn install
yarn dev

Create a network

const network = new Network(
  // Inputs
  2,
  // Output
  { neurons: 1, activationFunction: ActivationFunction.Sigmoid },
  // Hidden layers
  [
    { neurons: 3, activationFunction: ActivationFunction.Relu, bias: 1 },
    { neurons: 3, bias: 0.5 } // Default function is Sigmoid
  ]
)

Train network

const trainer = new Backpropagation(network, 0.3 /* Learning Rate */)

const xorSamples: TrainingSample[] = [
  { inputs: [0, 0], outputs: [0] },
  { inputs: [0, 1], outputs: [1] },
  { inputs: [1, 0], outputs: [1] },
  { inputs: [1, 1], outputs: [0] },
];

Train one sample

trainer.train(xorSamples[0])

Train batch (randomized)

trainer.trainBatch(xorSamples)

Compute

network.compute(xorSamples[0].inputs)

Importing and Exporting Networks

Export

Exporting the network will maintain all weights, activation functions, and biases when importing again.

const networkExport = network.export()
const networkJson = JSON.stringify(networkExport)

Import

const networkExport = JSON.parse(networkJson)
const network = Network.fromNetworkExport(networkExport)

Visualizing

const renderer = new Renderer(document.getElementById("graph")!)
renderer.draw(network)

Sample XOR Network with 2 hidden layers

XOR Network

Currently, the graph library doesn't do a great job of fitting the whole network, so adjusting some of the coordinates in the renderer is needed. For larger networks, it's not very feasible to render.