text: """
  Copyright (c) Facebook, Inc. and its affiliates.
  
  This source code is licensed under the MIT license found in the
  LICENSE file in the root directory of this source tree.
  """
  
  import torch
  
  
  class Normalizer(object):
      """Normalize a Tensor and restore it later. """
  
      def __init__(self, tensor=None, mean=None, std=None, device=None):
          """tensor is taken as a sample to calculate the mean and std"""
          if tensor is None and mean is None:
              return
  
          if device is None:
              device = "cpu"
  
          if tensor is not None:
              self.mean = torch.mean(tensor, dim=0).to(device)
              self.std = torch.std(tensor, dim=0).to(device)
              return
  
          if mean is not None and std is not None:
              self.mean = torch.tensor(mean).to(device)
              self.std = torch.tensor(std).to(device)
  
      def to(self, device):
          self.mean = self.mean.to(device)
          self.std = self.std.to(device)
  
      def norm(self, tensor):
          return (tensor - self.mean) / self.std
  
      def denorm(self, normed_tensor):
          return normed_tensor * self.std + self.mean
  
      def state_dict(self):
          return {"mean": self.mean, "std": self.std}
  
      def load_state_dict(self, state_dict):
          self.mean = state_dict["mean"].to(self.mean.device)
          self.std = state_dict["std"].to(self.mean.device)
  