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

auto-encoder.ts

v1.1.0

Published

A simple Auto Encoder typescript library for experimentation and dimensionality reduction. Supports automatic scaling.

Downloads

36

Readme

auto-encoder.ts

A simple Auto Encoder typescript library for experimentation and dimensionality reduction. Supports automatic scaling.

npm Package Version Minified Package Size Minified and Gzipped Package Size

This is a Typescript wrapper on top of autoencoder.

With additional helper functions: exportAutoEncoder(autoEncoder) and restoreAutoEncoder(json).

Auto Encoder Logo

Features

  • Build embedding model in self-supervised manner (without manually labelling data)
  • Reduce data dimension based on data distribution
  • Support export/restore with JSON
  • Lightweight (without node-gpy, cmake, python, cuda)
  • Automatic scaling/normalizing (can be turned off)
  • Static Type Checking and Completion with Typescript
  • Isomorphic package: works in Node.js and browsers
  • Works with plain Javascript, Typescript is not mandatory

Installation

npm install auto-encoder.ts

You can also install auto-encoder.ts with pnpm, yarn, or slnpm

Usage Example

Create new Auto Encoder

There are two ways to initialize a model:

  • Provide the number of layers, input size, encoder output size (number of latent variables) and the activation function name
import { createAutoEncoder } from 'auto-encoder.ts'

const model = createAutoEncoder({
  nInputs: 10,
  nHidden: 2,
  nLayers: 2, // (default 2) - number of layers in each encoder/decoder
  activation: 'relu', // (default 'relu') - applied to all, but the last layer
})
  • Define each layer separately for both encoder and decoder
import { createAutoEncoder } from 'auto-encoder.ts'

const model = createAutoEncoder({
  encoder: [
    { nOut: 10, activation: 'tanh' },
    { nOut: 2, activation: 'tanh' },
  ],
  decoder: [{ nOut: 2, activation: 'tanh' }, { nOut: 10 }],
  scale: false, // (default true)
})

Activation functions: relu, tanh, sigmoid

Auto Scaling

Similar to other neural networks, auto-encoder is very sensitive to input scaling.

To make it easier the scaling is enabled by default.

you can control it with an extra parameter scale that can be true or false.

Train the Auto Encoder model

model.fit(X, {
  batchSize: 100,
  iterations: 5000,
  method: 'adagrad', // (default 'adagrad')
  stepSize: 0.01, // (default 0.05)
})

Optimization methods: sgd, adagrad, adam

Encode, Decode, Predict

const Y = model.encode(X)
const Xd = model.decode(Y)

// Similar to model.decode(model.encode(X))
const Xp = model.predict(X)

Web demo (dimensionality reduction)

Try the package in the browser on StatSim Vis. Choose a CSV file, change the Projection method to Autoencoder, then click Run.

Typescript Signature

Below are the exported function and types:

import { ActivationFunctionName, OptimizationMethodName } from 'adnn.ts'

function createAutoEncoder(options: AutoEncoderOptions): AutoEncoder

function exportAutoEncoder(autoEncoder: AutoEncoder): AutoEncoderJSON

function restoreAutoEncoder(json: AutoEncoderJSON): AutoEncoder

interface AutoEncoder {
  fit(X: BatchValues, options?: FitOptions): void
  encode(X: BatchValues): BatchValues
  decode(X: BatchValues): BatchValues
  /** @description Similar to this.decode(this.encode(X)) */
  predict(X: BatchValues): BatchValues
}

type AutoEncoderJSON = {
  scale: boolean | undefined
  max: number[]
  min: number[]
  nInputs: number
  nHidden: number
  encoder: unknown
  decoder: unknown
}

type FitOptions = {
  /** @default round(totalSize/50) */
  batchSize?: number
  /** @default 100 */
  iterations?: number
  /** @default 'adagrad' */
  method?: OptimizationMethodName
  /** @default 0.05 */
  stepSize?: number
}

type BatchValues = Values[]

type Values = number[]

type AutoEncoderOptions =
  | {
      /** @default true */
      scale?: boolean
      /** @description number of input features */
      nInputs: number
      /** @description number of embedding features */
      nHidden: number
      /**
       * @description number of layers in each encoder/decoder
       * @default 2
       */
      nLayers?: number
      /**
       * @description applied to all, but the last layer
       * @default 'relu'
       */
      activation?: ActivationFunctionName
    }
  | {
      /** @default true */
      scale?: boolean
      encoder: LayerOptions[]
      decoder: LayerOptions[]
    }

type LayerOptions = {
  nOut: number
  /** @description no activation function in the last layer of decoder gives better result */
  activation?: ActivationFunctionName
}

License

This project is licensed with BSD-2-Clause

This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:

  • The freedom to run the program as you wish, for any purpose
  • The freedom to study how the program works, and change it so it does your computing as you wish
  • The freedom to redistribute copies so you can help others
  • The freedom to distribute copies of your modified versions to others