Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Tfjs Activations

From Leeroopedia
Revision as of 16:50, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tensorflow_Tfjs_Activations.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Deep_Learning, Layers_API
Last Updated 2026-02-10 06:00 GMT

Overview

The activations.ts module defines the core activation function classes used throughout the TensorFlow.js Layers API. Each activation extends the abstract Activation base class (itself extending serialization.Serializable) and implements the apply(tensor, axis?) method. The module also provides serialization/deserialization utilities and a registry lookup function (getActivation) that resolves string identifiers to activation instances.

Supported activations: Elu, Selu, Relu, Relu6, Linear, Sigmoid, HardSigmoid, Softplus, Softsign, Tanh, Softmax, LogSoftmax, Gelu, GeluNew, Mish, and Swish. Each class is registered via serialization.registerClass() for model persistence.

Code Reference

Source Location

tfjs-layers/src/activations.ts (View on GitHub)

Key Signatures

export abstract class Activation extends serialization.Serializable {
  abstract apply(tensor: Tensor, axis?: number): Tensor;
  getConfig(): serialization.ConfigDict;
}

export function serializeActivation(activation: Activation): string;

export function deserializeActivation(
    config: serialization.ConfigDict,
    customObjects?: serialization.ConfigDict): Activation;

export function getActivation(
    identifier: ActivationIdentifier | serialization.ConfigDict | Activation
): Activation;

Import

import { Activation, getActivation, serializeActivation, deserializeActivation } from './activations';

Activation Classes

Class className Formula / Behavior
Elu elu alpha * (exp(x) - 1) for x < 0, else x
Selu selu Self-normalizing ELU (Klambauer et al., 2017)
Relu relu max(0, x)
Relu6 relu6 min(6, max(0, x))
Linear linear Identity (no-op)
Sigmoid sigmoid 1 / (1 + exp(-x))
HardSigmoid hardSigmoid Piecewise linear approximation of sigmoid
Softplus softplus log(1 + exp(x))
Softsign softsign x / (abs(x) + 1)
Tanh tanh Hyperbolic tangent
Softmax softmax Normalized exponentials along axis
LogSoftmax logSoftmax Log of softmax
Gelu gelu x * Phi(x) using erf
GeluNew gelu_new Approximate GELU using tanh
Mish mish x * tanh(softplus(x))
Swish swish x * sigmoid(alpha * x)

I/O Contract

Input

  • apply(tensor, axis?): Accepts a tf.Tensor of any shape. The optional axis parameter is used by Softmax and LogSoftmax (defaults to -1). Elu and Swish accept an alpha scaling parameter.

Output

  • Returns a tf.Tensor of the same shape as the input, with the activation function applied element-wise.

getActivation

  • Input: A string identifier (e.g., 'relu'), a ConfigDict, or an existing Activation instance. If null, defaults to 'linear'.
  • Output: An Activation instance ready for use.

Usage Examples

import { getActivation } from './activations';

// Resolve an activation by string identifier
const relu = getActivation('relu');
const output = relu.apply(tf.tensor1d([-1, 0, 1]));
// output: [0, 0, 1]

// Use softmax with axis
const softmax = getActivation('softmax');
const probs = softmax.apply(tf.tensor2d([[1, 2, 3]]), -1);

// Serialize and deserialize
const serialized = serializeActivation(relu); // 'relu'

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment