Inverse transform

Viewed 631

I am not able to find an efficient way to give batch input to this function and return the batch output. I want to do this during the training of my neural network.

Inverse_Norm = transforms.Normalize(
   mean = [-m/s for m, s in zip(mean, std)],
   std = [1/s for s in std]
)
inverse_norm_input = Inverse_Norm(input)
1 Answers

Assuming a tensor of shape (B, C, ...) wheremean and std are iterables of length C then you can use broadcasting semantics to operate across a batch tensor. For example

import torch

def batch_inverse_normalize(x, mean, std):
    # represent mean and std to 1, C, 1, ... tensors for broadcasting
    reshape_shape = [1, -1] + ([1] * (len(x.shape) - 2))
    mean = torch.tensor(mean, device=x.device, dtype=x.dtype).reshape(*reshape_shape)
    std = torch.tensor(std, device=x.device, dtype=x.dtype).reshape(*reshape_shape)
    return x * std + mean
Related