Skip to main content
Deno 2 is finally here 🎉️
Learn more


Netsaur


netsaur stars netsaur releases netsaur License


neural network deno module using neo

Maintainers

Usage

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

await setupBackend(CPU);

const net = new NeuralNetwork({
  silent: true,
  layers: [
    DenseLayer({ size: [3] }),
    SigmoidLayer(),
    DenseLayer({ size: [1] }),
    SigmoidLayer(),
  ],
  cost: "crossentropy",
});

await net.train(
  [
    {
      inputs: tensor2D([
        [0, 0],
        [1, 0],
        [0, 1],
        [1, 1],
      ]),
      outputs: tensor1D([0, 1, 1, 0]),
    },
  ],
  10000,
);

console.log(`training time: ${performance.now() - time}ms`);
console.log((await net.predict(tensor1D([0, 0]))).data);
console.log((await net.predict(tensor1D([1, 0]))).data);
console.log((await net.predict(tensor1D([0, 1]))).data);
console.log((await net.predict(tensor1D([1, 1]))).data);

Use the Native Backend

import {
  DenseLayer,
  NeuralNetwork,
  setupBackend,
  SigmoidLayer,
} from "https://deno.land/x/netsaur/mod.ts";
import {
  Matrix,
  Native,
} from "https://deno.land/x/netsaur/backends/native/mod.ts";

await setupBackend(Native);

const net = new NeuralNetwork({
  silent: true,
  layers: [
    DenseLayer({ size: [3] }),
    SigmoidLayer(),
    DenseLayer({ size: [1] }),
    SigmoidLayer(),
  ],
  cost: "crossentropy",
});

network.train(
  [
    {
      inputs: Matrix.of([
        [0, 0],
        [0, 1],
        [1, 0],
        [1, 1],
      ]),
      outputs: Matrix.column([0, 1, 1, 0]),
    },
  ],
  5000,
  0.1,
);

console.log(
  await network.predict(
    Matrix.of([
      [0, 0],
      [0, 1],
      [1, 0],
      [1, 1],
    ]),
  ),
);

Saving Models

import {
  DenseLayer,
  NeuralNetwork,
  SigmoidLayer,
  tensor1D,
  tensor2D,
} from "https://deno.land/x/netsaur/mod.ts";
import { Model } from "https://deno.land/x/netsaur/model/mod.ts";

const net = new NeuralNetwork({
  silent: true,
  layers: [
    DenseLayer({ size: [3] }),
    SigmoidLayer(),
    DenseLayer({ size: [1] }),
    SigmoidLayer(),
  ],
  cost: "crossentropy",
});

await net.train(
  [
    {
      inputs: await tensor2D([
        [0, 0],
        [1, 0],
        [0, 1],
        [1, 1],
      ]),
      outputs: await tensor1D([0, 1, 1, 0]),
    },
  ],
  5000,
);

await Model.save("./network.json", net);

Loading & Running Models

import { tensor1D } from "https://deno.land/x/netsaur/mod.ts";
import { Model } from "https://deno.land/x/netsaur/model/mod.ts";

const net = await Model.load("./network.json");

console.log((await net.predict(tensor1D([0, 0]))).data);
console.log((await net.predict(tensor1D([1, 0]))).data);
console.log((await net.predict(tensor1D([0, 1]))).data);
console.log((await net.predict(tensor1D([1, 1]))).data);