class_docstr: Class that combines a list of DeepConvexNet and ActNorm
  layers and provides energy score computation
  
  This class is based on SequentialFlow of the CP-Flow repo
  (https://github.com/CW-Huang/CP-Flow)
  
  Parameters
  ----------
  networks
      list of DeepConvexNet and/or ActNorm instances
class_docstr_tok: ["Class","that","combines","a","list","of","DeepConvexNet","and","ActNorm","layers","and","provides","energy","score","computation","This","class","is","based","on","SequentialFlow","of","the","CP-Flow","repo","(","https",":","\/\/github.com\/CW-Huang\/CP-Flow",")","Parameters","--","--","--","--","--","networks","list","of","DeepConvexNet","and\/or","ActNorm","instances"]
docstr: Computes the (approximated) energy score sum_i ES(g,z_i), where
  ES(g,z_i) =
  
  -1/(2*es_num_samples^2) * sum_{w,w'} ||w-w'||_2^beta
  + 1/es_num_samples * sum_{w''} ||w''-z_i||_2^beta,
  w's are samples drawn from the
  quantile function g(., h_i) (gradient of picnn),
  h_i is the hidden state associated with z_i,
  and es_num_samples is the number of samples drawn
  for each of w, w', w'' in energy score approximation
  
  Parameters
  ----------
  z
      Observations (numel_batch, dimension)
  hidden_state
      Hidden state (numel_batch, hidden_size)
  es_num_samples
      Number of samples drawn for each of w, w', w''
      in energy score approximation
  beta
      Hyperparameter of the energy score, see the formula above
  Returns
  -------
  loss
      energy score (numel_batch)
docstr_tok: ["Computes","the","(","approximated",")","energy","score","sum_i","ES","(","g",",","z_i",")",",","where","ES","(","g",",","z_i",")","=","-1\/","(","2","*","es_num_samples^2",")","*","sum_","{","w",",","w","'","}","||w-w'||_2^beta","+","1\/es_num_samples","*","sum_","{","w","''","}","||w","''","-z_i||_2^beta",",","w","'s","are","samples","drawn","from","the","quantile","function","g","(",".",",","h_i",")","(","gradient","of","picnn",")",",","h_i","is","the","hidden","state","associated","with","z_i",",","and","es_num_samples","is","the","number","of","samples","drawn","for","each","of","w",",","w","'",",","w","''","in","energy","score","approximation","Parameters","--","--","--","--","--","z","Observations","(","numel_batch",",","dimension",")","hidden_state","Hidden","state","(","numel_batch",",","hidden_size",")","es_num_samples","Number","of","samples","drawn","for","each","of","w",",","w","'",",","w","''","in","energy","score","approximation","beta","Hyperparameter","of","the","energy","score",",","see","the","formula","above","Returns","--","--","--","-","loss","energy","score","(","numel_batch",")"]
code: def energy_score(
      self,
      z: torch.Tensor,
      hidden_state: torch.Tensor,
      es_num_samples: int = 50,
      beta: float = 1.0,
  ) -> torch.Tensor:
      """
      Computes the (approximated) energy score sum_i ES(g,z_i), where
      ES(g,z_i) =
  
      -1/(2*es_num_samples^2) * sum_{w,w'} ||w-w'||_2^beta
      + 1/es_num_samples * sum_{w''} ||w''-z_i||_2^beta,
      w's are samples drawn from the
      quantile function g(., h_i) (gradient of picnn),
      h_i is the hidden state associated with z_i,
      and es_num_samples is the number of samples drawn
      for each of w, w', w'' in energy score approximation
  
      Parameters
      ----------
      z
          Observations (numel_batch, dimension)
      hidden_state
          Hidden state (numel_batch, hidden_size)
      es_num_samples
          Number of samples drawn for each of w, w', w''
          in energy score approximation
      beta
          Hyperparameter of the energy score, see the formula above
      Returns
      -------
      loss
          energy score (numel_batch)
      """
  
      numel_batch, dimension = z.shape[0], z.shape[1]
  
      hidden_state_repeat = hidden_state.repeat_interleave(
          repeats=es_num_samples, dim=0
      )
  
      w = self.es_sample(hidden_state_repeat, dimension)
      w_prime = self.es_sample(hidden_state_repeat, dimension)
  
      first_term = (
          torch.norm(
              w.view(numel_batch, 1, es_num_samples, dimension)
              - w_prime.view(numel_batch, es_num_samples, 1, dimension),
              dim=-1,
          )
          ** beta
      )
  
      mean_first_term = torch.mean(
          first_term.view(numel_batch, -1), dim=-1
      )
  
      # since both tensors are huge (numel_batch*es_num_samples, dimension),
      # delete to free up GPU memories
      del w, w_prime
  
      z_repeat = z.repeat_interleave(repeats=es_num_samples, dim=0)
      w_bar = self.es_sample(hidden_state_repeat, dimension)
  
      second_term = (
          torch.norm(
              w_bar.view(numel_batch, es_num_samples, dimension)
              - z_repeat.view(numel_batch, es_num_samples, dimension),
              dim=-1,
          )
          ** beta
      )
  
      mean_second_term = torch.mean(
          second_term.view(numel_batch, -1), dim=-1
      )
  
      loss = -0.5 * mean_first_term + mean_second_term
  
      return loss
  
file_path: gluonts/src/gluonts/torch/model/mqf2/icnn_utils.py
imports: from typing import Optional, Tuple, List, Union
  from cpflows.flows import SequentialFlow, DeepConvexFlow
  import torch
  import torch.nn.functional
  from torch.distributions.normal import Normal
label_desc: Use image node_id 4 for calling the SequentialNet obj's underlying member method code with example usage: obj.energy_score(z, hidden_state, es_num_samples, beta) and returns: loss