Coder Social home page Coder Social logo

denosaurs / netsaur Goto Github PK

View Code? Open in Web Editor NEW
194.0 7.0 5.0 25.63 MB

Powerful machine learning, accelerated by WebGPU

Home Page: https://deno.land/x/netsaur

License: MIT License

TypeScript 24.79% JavaScript 13.17% Python 0.54% Rust 61.50%
deno webgpu neural-network ai machine-learning ml artificial-intelligence typescript deep-learning deep-neural-networks

netsaur's Introduction


Netsaur


netsaur stars netsaur releases netsaur License


Powerful Machine Learning library for Deno.

Features

  • Lightweight and easy-to-use neural network library for Deno.
  • Blazingly fast and efficient.
  • Provides a simple API for creating and training neural networks.
  • Can run on both the CPU and the GPU (WIP).
  • Allows you to simply run the code without downloading any prior dependencies.
  • Perfect for serverless environments.
  • Allows you to quickly build and deploy machine learning models for a variety of applications with just a few lines of code.
  • Suitable for both beginners and experienced machine learning practitioners.

Backends

  • CPU - Native backend written in Rust.
  • WASM - WebAssembly backend written in Rust.
  • GPU (TODO)

Examples

Maintainers

QuickStart

This example shows how to train a neural network to predict the output of the XOR function our speedy CPU backend written in Rust.

import {
  Cost,
  CPU,
  DenseLayer,
  Sequential,
  setupBackend,
  SigmoidLayer,
  tensor1D,
  tensor2D,
} from "https://deno.land/x/netsaur/mod.ts";

/**
 * Setup the CPU backend. This backend is fast but doesn't work on the Edge.
 */
await setupBackend(CPU);

/**
 * Creates a sequential neural network.
 */
const net = new Sequential({
  /**
   * The number of minibatches is set to 4 and the output size is set to 2.
   */
  size: [4, 2],

  /**
   * The silent option is set to true, which means that the network will not output any logs during trainin
   */
  silent: true,

  /**
   * Defines the layers of a neural network in the XOR function example.
   * The neural network has two input neurons and one output neuron.
   * The layers are defined as follows:
   * - A dense layer with 3 neurons.
   * - sigmoid activation layer.
   * - A dense layer with 1 neuron.
   * -A sigmoid activation layer.
   */
  layers: [
    DenseLayer({ size: [3] }),
    SigmoidLayer(),
    DenseLayer({ size: [1] }),
    SigmoidLayer(),
  ],

  /**
   * The cost function used for training the network is the mean squared error (MSE).
   */
  cost: Cost.MSE,
});

const time = performance.now();

/**
 * Train the network on the given data.
 */
net.train(
  [
    {
      inputs: tensor2D([
        [0, 0],
        [1, 0],
        [0, 1],
        [1, 1],
      ]),
      outputs: tensor2D([[0], [1], [1], [0]]),
    },
  ],
  /**
   * The number of iterations is set to 10000.
   */
  10000,
);

console.log(`training time: ${performance.now() - time}ms`);

/**
 * Predict the output of the XOR function for the given inputs.
 */
const out1 = (await net.predict(tensor1D([0, 0]))).data;
console.log(`0 xor 0 = ${out1[0]} (should be close to 0)`);

const out2 = (await net.predict(tensor1D([1, 0]))).data;
console.log(`1 xor 0 = ${out2[0]} (should be close to 1)`);

const out3 = (await net.predict(tensor1D([0, 1]))).data;
console.log(`0 xor 1 = ${out3[0]} (should be close to 1)`);

const out4 = (await net.predict(tensor1D([1, 1]))).data;
console.log(`1 xor 1 = ${out4[0]} (should be close to 0)`);

Use the WASM Backend

By changing the CPU backend to the WASM backend we sacrifice some speed but this allows us to run on the edge.

import {
  Cost,
  DenseLayer,
  Sequential,
  setupBackend,
  SigmoidLayer,
  tensor1D,
  tensor2D,
  WASM,
} from "https://deno.land/x/netsaur/mod.ts";

/**
 * Setup the WASM backend. This backend is slower than the CPU backend but works on the Edge.
 */
await setupBackend(WASM);

/**
 * Creates a sequential neural network.
 */
const net = new Sequential({
  /**
   * The number of minibatches is set to 4 and the output size is set to 2.
   */
  size: [4, 2],

  /**
   * The silent option is set to true, which means that the network will not output any logs during trainin
   */
  silent: true,

  /**
   * Defines the layers of a neural network in the XOR function example.
   * The neural network has two input neurons and one output neuron.
   * The layers are defined as follows:
   * - A dense layer with 3 neurons.
   * - sigmoid activation layer.
   * - A dense layer with 1 neuron.
   * -A sigmoid activation layer.
   */
  layers: [
    DenseLayer({ size: [3] }),
    SigmoidLayer(),
    DenseLayer({ size: [1] }),
    SigmoidLayer(),
  ],

  /**
   * The cost function used for training the network is the mean squared error (MSE).
   */
  cost: Cost.MSE,
});

const time = performance.now();

/**
 * Train the network on the given data.
 */
net.train(
  [
    {
      inputs: tensor2D([
        [0, 0],
        [1, 0],
        [0, 1],
        [1, 1],
      ]),
      outputs: tensor2D([[0], [1], [1], [0]]),
    },
  ],
  /**
   * The number of iterations is set to 10000.
   */
  10000,
);

console.log(`training time: ${performance.now() - time}ms`);

/**
 * Predict the output of the XOR function for the given inputs.
 */
const out1 = (await net.predict(tensor1D([0, 0]))).data;
console.log(`0 xor 0 = ${out1[0]} (should be close to 0)`);

const out2 = (await net.predict(tensor1D([1, 0]))).data;
console.log(`1 xor 0 = ${out2[0]} (should be close to 1)`);

const out3 = (await net.predict(tensor1D([0, 1]))).data;
console.log(`0 xor 1 = ${out3[0]} (should be close to 1)`);

const out4 = (await net.predict(tensor1D([1, 1]))).data;
console.log(`1 xor 1 = ${out4[0]} (should be close to 0)`);

Documentation

The full documentation for Netsaur can be found here.

License

Netsaur is licensed under the MIT License.

netsaur's People

Contributors

carrotzrule123 avatar djdeveloperr avatar load1n9 avatar retraigo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

netsaur's Issues

Serialisation Format

Use safetensors to serialise the data.
Support conversion to and from other model formats.

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar

Unable to use more than 255 output neurons on the CPU backend

Hi.

Here is a simple neural network with an output of 2 neurons:

import { Cost, CPU, DenseLayer, Sequential, setupBackend, SigmoidLayer, tensor1D, tensor2D, } from "https://deno.land/x/netsaur/mod.ts";

await setupBackend(CPU);

const net = new Sequential({
    size: [4, 2],
    layers: [
      DenseLayer({ size: [3] }),
      SigmoidLayer(),
      DenseLayer({ size: [2] }),
      SigmoidLayer(),
    ],
    cost: Cost.MSE,
  });

const outputs = await net.predict(tensor1D([1, 0]));
console.log(outputs);
$ deno run -A --unstable test.ts

Tensor {                                                                                                                  
    shape: [ 2 ],
    data: Float32Array(2) [ 0.3955751657485962, 0.6075010895729065 ]
}

However, if I update the last layer to 256 (or more) neurons, this happens:

$ deno run -A --unstable test.ts

thread '<unnamed>' panicked at 'source slice length (256) does not match destination slice length (0)', rust\src\ffi.rs:74:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: Rust cannot catch foreign exceptions

With 255 neurons or less, this error does not happen.

It also looks like this only happens when using the CPU backend, not the WASM backend. There is no error if I use the WASM backend.

I am running deno 1.37.1 on Windows 10.

Thanks in advance!

Rust WGPU Backend

Base it off neo, but written in rust.
Port all CPU layers to GPU.

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar

Tests

Create unit tests for each layer.

Funding

  • You can sponsor this specific effort via a Polar.sh pledge below
  • We receive the pledge once the issue is completed & verified
Fund with Polar

Implement CPU Layers

Current checklist of layers that we want to implement:

  • Activation Layer
  • Conv2D Layer
  • Dense Layer
  • Dropout1D Layer
  • Dropout2D Layer
  • Flatten Layer
  • Pool2D Layer
  • BatchNorm Layer
  • BatchNorm2D Layer
  • ConvTranspose2D Layer

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.