Implementation:Tensorflow Tfjs CPU ResizeBilinearGrad Kernel
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, CPU_Backend |
| Last Updated | 2026-02-10 06:00 GMT |
Overview
The ResizeBilinearGrad kernel computes the gradient of bilinear image resizing for the CPU backend. During backpropagation, it distributes each gradient value from the resized output (dy) back to the four source pixels in the original image that contributed to it, weighted by the same bilinear interpolation coefficients used in the forward pass. The reference implementation follows the TensorFlow C++ kernel.
Code Reference
Source Location
tfjs-backend-cpu/src/kernels/ResizeBilinearGrad.ts (GitHub)
Signature
export function resizeBilinearGrad(args: {
inputs: ResizeBilinearGradInputs,
backend: MathBackendCPU,
attrs: ResizeBilinearGradAttrs
}): TensorInfo
Imports
import {KernelConfig, KernelFunc, ResizeBilinearGrad, ResizeBilinearGradAttrs, ResizeBilinearGradInputs, TensorInfo, TypedArray, util} from '@tensorflow/tfjs-core';
import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';
Kernel Config
export const resizeBilinearGradConfig: KernelConfig = {
kernelName: ResizeBilinearGrad,
backendName: 'cpu',
kernelFunc: resizeBilinearGrad 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 float32 dtype and shape [batch, xWidth, xHeight, depth], containing the accumulated gradients for the original image pixels.
Algorithm
- Computes effective input (
xHeight,xWidth) and output (yHeight,yWidth) sizes withalignCornersadjustments. - Calculates
heightScaleandwidthScaleratios. - For each output pixel in
dyat position[b, r, c]:- Computes the corresponding fractional source position (
dxR,dxC). - Determines floor and ceil indices for both row and column, clamped to valid bounds.
- Computes four interpolation weights:
inverseDxRLerp * inverseDxCLerp,inverseDxRLerp * dxCLerp,dxRLerp * inverseDxCLerp,dxRLerp * dxCLerp. - For each depth channel, distributes the
dyvalue to the four corresponding input positions weighted by these coefficients (accumulating via+=).
- Computes the corresponding fractional source position (
Usage Example
This kernel is invoked automatically during backpropagation through tf.image.resizeBilinear:
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.resizeBilinear(img, [4, 4]).sum());
const [dx] = grads([image]);
dx.print();