# Netsaur

## 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).
• 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)

### 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:  }),
SigmoidLayer(),
DenseLayer({ size:  }),
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([, , , ]),
},
],
/**
* 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} (should be close to 0)`);

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

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

const out4 = (await net.predict(tensor1D([1, 1]))).data;
console.log(`1 xor 1 = \${out4} (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:  }),
SigmoidLayer(),
DenseLayer({ size:  }),
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([, , , ]),
},
],
/**
* 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} (should be close to 0)`);

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

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

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

### Documentation

The full documentation for Netsaur can be found here.
{"ft_lang_label":"__label__en","ft_lang_prob":0.67101765,"math_prob":0.9515705,"size":5027,"snap":"2023-40-2023-50","text_gpt3_token_len":1391,"char_repetition_ratio":0.12064503,"word_repetition_ratio":0.70098037,"special_character_ratio":0.3284265,"punctuation_ratio":0.20892495,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9911819,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-27T21:04:17Z\",\"WARC-Record-ID\":\"<urn:uuid:cc53bd38-4f24-409f-a797-d3176612e1fe>\",\"Content-Length\":\"144407\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c6e03e30-cbc9-4146-b182-ea85c8383714>\",\"WARC-Concurrent-To\":\"<urn:uuid:89a0d69b-7559-4d86-bf5f-c2f7c3c2b145>\",\"WARC-IP-Address\":\"34.120.54.55\",\"WARC-Target-URI\":\"https://deno.land/x/netsaur@0.2.8\",\"WARC-Payload-Digest\":\"sha1:5YD2UD3U2GIUUIFFGIN2GMTKYXNF36IC\",\"WARC-Block-Digest\":\"sha1:FD653PRAYATPLLXUHS7KK2J5VHTF4V4M\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510326.82_warc_CC-MAIN-20230927203115-20230927233115-00767.warc.gz\"}"}