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 DepthwiseConv2D Layer

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


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

Overview

This module implements the depthwise 2D convolution layer for TensorFlow.js Layers. Depthwise convolution applies a separate filter to each input channel, producing inputChannels * depthMultiplier output channels. This is a key building block for efficient architectures like MobileNet. The module includes both a standalone depthwiseConv2d function and the DepthwiseConv2D layer class extending BaseConv.

Code Reference

Source Location

tfjs-layers/src/layers/convolutional_depthwise.ts (GitHub)

Key Imports

import * as tfc from '@tensorflow/tfjs-core';
import {serialization, Tensor, Tensor4D, tidy} from '@tensorflow/tfjs-core';
import {imageDataFormat} from '../backend/common';
import {BaseConv, BaseConvLayerArgs, ConvLayerArgs, preprocessConv2DInput} from './convolutional';

Standalone Function

depthwiseConv2d

Performs 2D depthwise convolution on a 4D input tensor.

export function depthwiseConv2d(
    x: Tensor,
    depthwiseKernel: Tensor,
    strides: [number, number] = [1, 1],
    padding = 'valid',
    dataFormat?: DataFormat,
    dilationRate?: [number, number]): Tensor

Validates that both input and kernel are 4D, converts input to NHWC format if needed, performs the convolution, and transposes back for channelsFirst format.

Layer Class

DepthwiseConv2D

export class DepthwiseConv2D extends BaseConv {
  static className = 'DepthwiseConv2D';
  constructor(args: DepthwiseConv2DLayerArgs);
  override build(inputShape: Shape | Shape[]): void;
  override call(inputs: Tensor | Tensor[], kwargs: Kwargs): Tensor | Tensor[];
  override computeOutputShape(inputShape: Shape | Shape[]): Shape | Shape[];
  override getConfig(): serialization.ConfigDict;
}

DepthwiseConv2DLayerArgs

export interface DepthwiseConv2DLayerArgs extends BaseConvLayerArgs {
  kernelSize: number | [number, number];
  depthMultiplier?: number;            // default: 1
  depthwiseInitializer?: InitializerIdentifier | Initializer;
  depthwiseConstraint?: ConstraintIdentifier | Constraint;
  depthwiseRegularizer?: RegularizerIdentifier | Regularizer;
}

I/O Contract

Operation Input Output
depthwiseConv2d 4D Tensor [batch, height, width, channels] 4D Tensor [batch, outH, outW, channels * depthMultiplier]
DepthwiseConv2D.call 4D Tensor (rank validated in build) 4D Tensor with depthwise-convolved spatial dimensions
computeOutputShape Input shape array Output shape array

Usage Example

import * as tf from '@tensorflow/tfjs';

const model = tf.sequential();
model.add(tf.layers.depthwiseConv2d({
  kernelSize: 3,
  depthMultiplier: 1,
  strides: [1, 1],
  padding: 'same',
  activation: 'relu',
  inputShape: [28, 28, 1]
}));

Related Pages

Page Connections

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