Skip to main content
Home

Built and signed on GitHub Actions

A simple neural network library for JavaScript and TypeScript.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
4 weeks ago (1.0.0)
class Dense
implements TrainableLayer

Represents a fully connected (dense) layer in a neural network. Implements both Layer and TrainableLayer interfaces.

Examples

Example 1

const denseLayer = new Dense(784, 128); // A dense layer with 784 inputs and 128 outputs
const input = [[...Array(784).keys()].map(x => x / 784)]; // Example batch of 1 sample
const output = denseLayer.forward(input);

Constructors

new
Dense(
inputUnits: number,
outputUnits: number,
)

Creates an instance of a Dense layer.

Properties

private
biases: number[]
private
lastInput: number[][]
private
weights: number[][]

Methods

backward(outputGradient: number[][]): number[][]

Performs the backward pass (backpropagation) through the dense layer. Calculates the gradient of the loss with respect to the layer's input.

forward(input: number[][]): number[][]

Performs the forward pass through the dense layer. Output = Input * Weights + Biases

getConfig(): Record<string, unknown>

Returns the configuration of the layer.

Returns the number of input units.

Returns the name of the layer.

Returns the number of output units.

getWeightGradients(
outputGradient: number[][],
layerInput: number[][],
): Map<string, number[] | number[][]>

Calculates the gradients of the loss with respect to the layer's weights and biases. dL/dW_ij = dL/dY_j * X_i dL/dB_j = dL/dY_j

Retrieves the current weights and biases of the layer.

Initializes the biases for the layer. Biases are initialized with random values between 0 and 1.

Initializes the weights for the layer. Weights are initialized with random values between 0 and 1.

updateWeights(updatedWeights: Map<string, number[] | number[][]>): void

Updates the layer's weights and biases.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";
or

Import directly with a jsr specifier

import { Dense } from "jsr:@am/neuralnetwork/layers";

Add Package

pnpm i jsr:@am/neuralnetwork
or (using pnpm 10.8 or older)
pnpm dlx jsr add @am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";

Add Package

yarn add jsr:@am/neuralnetwork
or (using Yarn 4.8 or older)
yarn dlx jsr add @am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";

Add Package

vlt install jsr:@am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";

Add Package

npx jsr add @am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";

Add Package

bunx jsr add @am/neuralnetwork

Import symbol

import { Dense } from "@am/neuralnetwork/layers";