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 CPU ResizeNearestNeighborGrad Kernel

From Leeroopedia


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

Overview

The ResizeNearestNeighborGrad kernel computes the gradient of nearest-neighbor image resizing for the CPU backend. It determines which input pixels contributed to each output pixel in the forward pass and accumulates the upstream gradient values back to those input positions.

Code Reference

Source Location

tfjs-backend-cpu/src/kernels/ResizeNearestNeighborGrad.ts (GitHub)

Signature

export function resizeNearestNeighborGrad(args: {
  inputs: ResizeNearestNeighborGradInputs,
  backend: MathBackendCPU,
  attrs: ResizeNearestNeighborGradAttrs
}): TensorInfo

Imports

import {KernelConfig, KernelFunc, ResizeNearestNeighborGrad, ResizeNearestNeighborGradAttrs, ResizeNearestNeighborGradInputs, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';

Kernel Config

export const resizeNearestNeighborGradConfig: KernelConfig = {
  kernelName: ResizeNearestNeighborGrad,
  backendName: 'cpu',
  kernelFunc: resizeNearestNeighborGrad as unknown as KernelFunc
};

I/O Contract

Inputs

Name Type Description
dy TensorInfo Upstream gradient of shape [batch, yHeight, yWidth, depth]
images TensorInfo Original input images of shape [batch, xHeight, xWidth, depth]

Attributes

Name Type Description
alignCorners boolean If true, aligns the corner pixels of input and output

Output

Returns a TensorInfo with the same shape and dtype as images, containing accumulated gradients.

Algorithm

  1. Computes effective input and output sizes with alignCorners adjustments.
  2. Calculates forward and inverse height/width scaling ratios.
  3. Computes search window dimensions: winHeight = ceil(invHeightScale) * 2 + 2 and winWidth = ceil(invWidthScale) * 2 + 2.
  4. For each input pixel [b, r, c, d]:
    1. Determines a search window in dy centered around the expected contributing output pixels.
    2. For each dy pixel in the search window:
      1. Computes the nearest-neighbor source row/column using Math.round (if alignCorners) or Math.floor.
      2. If the source row and column match the current input position, accumulates the dy value.
    3. Stores the accumulated gradient in the output.

Usage Example

This kernel is invoked automatically during backpropagation through tf.image.resizeNearestNeighbor:

import * as tf from '@tensorflow/tfjs-core';
import '@tensorflow/tfjs-backend-cpu';

const image = tf.tensor4d([1, 2, 3, 4], [1, 2, 2, 1]);
const grads = tf.grads(
    (img) => tf.image.resizeNearestNeighbor(img, [4, 4]).sum());
const [dx] = grads([image]);
dx.print();

Related Pages

Page Connections

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