Coder Social home page Coder Social logo

vitaly-z / wasml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robbiesymonds/wasml

0.0 0.0 0.0 79 KB

WASM-powered reinforcement learning library written in Rust and TypeScript.

Home Page: https://www.npmjs.com/wasml

Rust 8.85% TypeScript 91.15%

wasml's Introduction

WASML

WebAssembly-powered reinforcement learning library written in Rust and TypeScript.

๐Ÿš€ Getting Started

WASML is available as an NPM package, simply install with the package manager of your choice.

# With yarn
yarn add wasml

# With npm
npm install wasml --save

๐Ÿ’พ Usage

WASML can be imported as both an ES and CommonJS module. The syntax takes heavy inspiration from TensorflowJS, so it should familiar to those with some prior experience. The following examples demontrates the basic usage (see /src/tests/ for more).

Basic Usage

import WASML from "wasml"

const wasml = new WASML()

// Create a model with 16 inputs and 3 action states.
await wasml.model(16, 3) // See below for full configuration options.

// Using `wasml.table(16, 3)` instead will solve this game far quicker!
// * Tabular optimisation becomes less feasible as state space grows (only 40x40=1600 states here)

// Add two hidden layers.
wasml.addLayers([
  { units: 32, activation: "sigmoid" },
  { units: 8, activation: "linear" },
])

// Compile the model.
wasml.compile({ loss: "meanSquaredError" })

// Array of a hundred empty samples.
// - It is not neccessary to pre-train the model, but can be useful.
const inputs = Array(100).from(Array.from({ length: 16 }, () => Math.random()))
const outputs = Array(100).from([1, 0, 0])
wasml.train(inputs, outputs)

// Predict the optimal action.
const input = Array.from({ length: 16 }, () => Math.random())
const result = wasml.predict(input)

// [?] Do something with the action.

// Reward the model.
wasml.reward(10.0)

Import/Export

import WASML from "wasml"

const wasml = new WASML()

// Load an exported model and restore the memory.
const model = await fetch('export.json').then(res => res.text())
wasml.import(model)

// Get the memory of the changed model in JSON form.
const json = wasml.export()

Custom Neural Network

import { NeuralNetwork } from "wasml/network"

// Utilise the underlying NN.
const NN = new NeuralNetwork(
  2,
  2,
  [
    {
      activation: "sigmoid",
      units: 8,
    },
    {
      activation: "sigmoid",
      units: 2,
    },
  ],
  {
    loss: "meanSquaredError",
  },
  0.1
)

// Trains a neural network to determine the largest number in a set of 2 numbers.
for (let i = 0; i < 10000; i++) {
  let data: number[] = [Math.random(), Math.random()]
  let target: number[] = data[0] > data[1] ? [1, 0] : [0, 1]

  const result = NN.forward(data)
  NN.backward(target)
}

// Now attempt some static predictions.
console.log("Test 1: ", NN.forward([10, 20]))
console.log("Test 2: ", NN.forward([500, 1]))
console.log("Test 3: ", NN.forward([0.7, 0.99]))

โš™๏ธ Configuration

The following are collection of optional parameters that can be passed as options to WASML.

Name Type Default Description
alpha number 0.1 The learning rate of the model.
gamma number 0.95 The reward discount factor, usually in range (0, 1).
epsilon number 0.1 The probability of performing a random action.
maxMemory number 1000 The size of the experience replay memory.
batchSize number 100 The number of experiences to sample each iteration.
episodeSize number 50 The number of iterations before updating target network.
epsilonDecay number 1000000 The number of iterations over which epsilon tends to zero.
loss Loss meanSquaredError The loss function used in backpropagation.
units number N/A The number of units for a given hidden layer.
activation Activation N/A The activation used in both forward and backwards passes.

wasml's People

Contributors

robbiesymonds avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.