class_docstr: This class defines the forward method for vector quantization. As VQ is not differentiable, it approximates the gradient of the VQ as in https://arxiv.org/abs/1711.00937.
docstr: Estimates gradient assuming vector quantization as identity function. (https://arxiv.org/abs/1711.00937)
docstr_tok: ["Estimates","gradient","assuming","vector","quantization","as","identity","function",".","(","https",":","\/\/arxiv.org\/abs\/1711.00937",")"]
code: def backward(
      ctx,
      grad_output,
      grad_indices,
      labels=None,
      num_classes=None,
      activate_class_partitioning=True,
      shared_keys=10,
      training=True,
  ):
      """
      Estimates gradient assuming vector quantization as identity function. (https://arxiv.org/abs/1711.00937)
      """
      grad_inputs, grad_codebook = None, None
  
      if ctx.needs_input_grad[0]:
          # Straight-through estimator
          grad_inputs = grad_output.clone()
      if ctx.needs_input_grad[1]:
          # Gradient wrt. the codebook
          indices, codebook = ctx.saved_tensors
          embedding_size = codebook.size(1)
  
          grad_output_flatten = grad_output.contiguous().view(
              -1, embedding_size
          )
          grad_codebook = torch.zeros_like(codebook)
          grad_codebook.index_add_(0, indices, grad_output_flatten)
  
      return (grad_inputs, grad_codebook, None, None, None, None, None)
  
imports: import torch
  import torch.nn
  from torch.autograd import Function
label_desc: Use image node_id 2 for calling the VectorQuantizationStraightThrough obj's underlying member method code with example usage: obj.backward(ctx, grad_output, grad_indices, labels, num_classes, activate_class_partitioning, shared_keys, training) and returns: grad_inputs,  grad_codebook,  None,  None,  None,  None,  None