Getting Started with Netsaur
Installation
To use Netsaur with JSR, you don't need to install anything. Simply import it in your code:
import { Sequential, DenseLayer, SigmoidLayer, Cost, CPU, tensor2D, setupBackend } from "jsr:@denosaurs/netsaur";
Basic Usage
Here's a simple example to get you started:
import { Sequential, DenseLayer, SigmoidLayer, Cost, CPU, tensor2D, setupBackend } from "jsr:@denosaurs/netsaur";
// Set up the CPU backend
await setupBackend(CPU);
// Create a simple neural network
const nn = new Sequential({
size: [2, 1],
layers: [
DenseLayer({ size: [4] }),
SigmoidLayer(),
DenseLayer({ size: [1] }),
SigmoidLayer(),
],
cost: Cost.MSE,
});
// Train the network
nn.train(
[
{
inputs: tensor2D([[0, 0], [0, 1], [1, 0], [1, 1]]),
outputs: tensor2D([[0], [1], [1], [0]]),
},
],
1000
);
// Make a prediction
const prediction = nn.forward(tensor2D([[1, 1]]));
console.log(prediction.data);
Advanced Configuration
Netsaur allows for deep customization of your neural networks. Here's an example of a more complex setup:
import { Sequential, DenseLayer, ReLULayer, SoftmaxLayer, Cost, CPU, tensor2D, setupBackend } from "jsr:@denosaurs/netsaur";
await setupBackend(CPU);
const nn = new Sequential({
size: [784, 10],
layers: [
DenseLayer({ size: [256] }),
ReLULayer(),
DenseLayer({ size: [128] }),
ReLULayer(),
DenseLayer({ size: [10] }),
SoftmaxLayer(),
],
cost: Cost.CrossEntropy,
});
// Train with MNIST data (not shown here)
nn.train(mnistData, 50);
This configuration creates a neural network suitable for classifying MNIST handwritten digits.
Data Preprocessing
Netsaur works with tensor data. You can create tensors from your raw data:
import { tensor2D } from "jsr:@denosaurs/netsaur";
const rawData = [[1, 2, 3], [4, 5, 6]];
const tensorData = tensor2D(rawData);
For more complex preprocessing, you might need to implement custom functions or use additional libraries.