Implementation:Tensorflow Tfjs Activations
| 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.Tensorof any shape. The optionalaxisparameter is used by Softmax and LogSoftmax (defaults to -1). Elu and Swish accept analphascaling parameter.
Output
- Returns a
tf.Tensorof the same shape as the input, with the activation function applied element-wise.
getActivation
- Input: A string identifier (e.g.,
'relu'), aConfigDict, or an existingActivationinstance. Ifnull, defaults to'linear'. - Output: An
Activationinstance 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
- Tensorflow_Tfjs_Advanced_Activations - Layer-based activation implementations (LeakyReLU, PReLU, ELU, ThresholdedReLU, Softmax as layers)
- Tensorflow_Tfjs_Backend_Operations - Low-level backend functions used by activations (e.g.,
K.elu,K.hardSigmoid,K.softsign) - Tensorflow_Tfjs_Exports_Layers - Public API factory functions that create activation layers