npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smallneuron

v1.1.2

Published

Small backpropagation engine to build simple neural networks, written in Typescript.

Downloads

13

Readme

SmallNeuron Library

SmallNeuron is a TypeScript library for building simple neural networks and performing basic tensor operations. It provides classes for tensors, neurons, and layers, along with methods for mathematical operations and activations commonly used in neural networks.

Installation

To use SmallNeuron in your TypeScript project, you can install it via npm:

npm install smallneuron

Usage

Here's how you can use SmallNeuron to create a simple neural network:

import { tensor, neuron, layer } from 'smallneuron';

// Create a tensor
const inputTensor = tensor(3.0);

// Create a neuron
const singleNeuron = neuron(1);

// Create a layer with 1 input and 1 output neuron
const singleLayer = layer(1, 1);

// Call the layer with input tensor
const output = singleLayer.call([inputTensor]);

console.log(output); // Output will be an array of tensors with single value

Classes

Tensor

Represents a tensor object with data, gradient, children, and operation.

Constructor:

  • data: number: Initial data value of the tensor.
  • children: Tensor[] = []: Array of child tensors (default empty).
  • operation: Operation = 'leaf': Operation used to create the tensor (default 'leaf').

Methods:

  • clear_gradients(): void: Clears gradients of the tensor and its children.
  • backward(): void: Performs backward propagation to calculate gradients.
  • Operations:
    • add(other: Tensorable): Tensor: Addition operation.
    • neg(): Tensor: Negation operation.
    • sub(other: Tensorable): Tensor: Subtraction operation.
    • mul(other: Tensorable): Tensor: Multiplication operation.
    • pow(other: number): Tensor: Power operation.
    • exp(): Tensor: Exponential operation.
    • inv(): Tensor: Inverse operation.
    • div(other: Tensorable): Tensor: Division operation.
    • tanh(): Tensor: Hyperbolic tangent operation.
    • relu(): Tensor: Rectified Linear Unit (ReLU) operation.

Neuron

Represents a neuron with weights, bias, and activation function.

Constructor:

  • input_width: number: Width of the input.
  • activation: 'relu' | 'tanh' = 'relu': Activation function type (default 'relu').
  • nobias: boolean = false: Indicates whether the neuron has bias (default false).

Methods:

  • call(inputs: Tensorable | Tensorable[]): Tensor: Calls the neuron with input tensor(s) and returns the output tensor.

Layer

Represents a layer of neurons.

Constructor:

  • input_width: number: Width of the input.
  • output_width: number: Width of the output.
  • activation: 'relu' | 'tanh' = 'relu': Activation function type (default 'relu').
  • nobias: boolean = false: Indicates whether the layer has bias (default false).

Methods:

  • call(inputs: Tensorable[]): Tensor[]: Calls the layer with input tensors and returns an array of output tensors.

Creator Functions

  • tensor(data: number, children: Tensor[] = [], operation: Operation = 'leaf'): Tensor: Creates a tensor object.
  • neuron(input_width: number, activation: 'relu' | 'tanh' = 'relu', nobias: boolean = false): Neuron: Creates a neuron object.
  • layer(input_width: number, output_width: number, activation: 'relu' | 'tanh' = 'relu', nobias: boolean = false): Layer: Creates a layer object.

SmallNeuron-Graph Extension

SmallNeuron-Graph is an extension of the SmallNeuron library that enables the creation and training of neural network models directly from graph data structures. This extension leverages the capabilities of the SmallNeuron library and the graphology library to dynamically construct neural network architectures from directed acyclic graphs (DAGs).

Usage with Graph Data

Here's how you can use SmallNeuron-Graph to create and train a neural network model from a directed acyclic graph:

import * as sn from 'smallneuron';
import { DirectedGraph } from 'graphology';
import { ModelMetadata, TrainingData, GraphModel } from 'smallneuron-graph';

// Define model metadata
const metadata = new ModelMetadata(2, 1, 0.01, 'relu', 'meanSquaredError', 100, 32);

// Create a directed acyclic graph representing the neural network architecture
const graph = new DirectedGraph();
graph.addNode('input1', { type: 'Input', output_ids: ['neuron1'], input_index: 0 });
graph.addNode('input2', { type: 'Input', output_ids: ['neuron1'], input_index: 1 });
graph.addNode('neuron1', {
	type: 'Neuron',
	input_width: 2, 
	input_ids: ['input1', 'input2'], 
	output_ids: ['output'], 
	activation: 'relu' 
	});
graph.addNode('output', {
	type: 'Neuron', 
	input_width: 1, 
	input_ids: ['neuron1'], 
	output_ids: [], 
	activation: 'relu' 
});
graph.addDirectedEdge('input1', 'neuron1');
graph.addDirectedEdge('input2', 'neuron1');
graph.addDirectedEdge('neuron1', 'output');

// Create training data
const trainingData: TrainingData = {
    inputs: [[0, 0], [0, 1], [1, 0], [1, 1]],
    outputs: [[0], [1], [1], [0]]
};

// Create and train the model
const model = new GraphModel(metadata, graph);
const losses = model.train(trainingData, true);

console.log(losses); // Array of losses during training

Classes and Interfaces

ModelMetadata

Metadata defining the model externally, including input and output widths, hyperparameters, and activation type.

TrainingData

Interface representing training data containing inputs and corresponding outputs.

GraphModel

Class representing a neural network model constructed from a directed acyclic graph. It provides methods for forward pass, training, and evaluation.