docstr: Create one-hot encoded discrete sequences and flatten them.
  Compute the pairwise Hamming distance $d$ between the sequences.
  Check the result of the kernel evaluation is
  $((1 + lpha) / (lpha + d))^{eta}$.
docstr_tok: ["Create","one-hot","encoded","discrete","sequences","and","flatten","them",".","Compute","the","pairwise","Hamming","distance","$","d","$","between","the","sequences",".","Check","the","result","of","the","kernel","evaluation","is","$","(","(","1","+","\u0007lpha",")","\/","(","\u0007lpha","+","d",")",")","^","{","\beta","}","$","."]
code: def test_computes_hamming_imq_function(self):
      """
      Create one-hot encoded discrete sequences and flatten them.
      Compute the pairwise Hamming distance $d$ between the sequences.
      Check the result of the kernel evaluation is
      $((1 + \alpha) / (\alpha + d))^{\beta}$.
      """
      vocab_size = 8
      seq_len = 4
      alpha = 2.0
      beta = 0.5
      kernel = HammingIMQKernel(vocab_size=vocab_size)
      kernel.initialize(alpha=alpha, beta=beta)
      kernel.eval()
  
      # Create two discrete sequences with some matches.
      a = torch.tensor([[7, 7, 7, 7], [5, 7, 3, 4]])
      b = torch.tensor(
          [
              [7, 5, 7, 4],
              [6, 7, 3, 7],
              [5, 7, 3, 4],
          ]
      )
  
      # Convert to one-hot representation.
      a_one_hot = torch.zeros(*a.shape, vocab_size)
      a_one_hot.scatter_(index=a.unsqueeze(-1), dim=-1, value=1)
      b_one_hot = torch.zeros(*b.shape, vocab_size)
      b_one_hot.scatter_(index=b.unsqueeze(-1), dim=-1, value=1)
  
      # Flatten the one-hot representations.
      a_one_hot_flat = a_one_hot.view(a.size(0), -1)
      b_one_hot_flat = b_one_hot.view(b.size(0), -1)
  
      # Compute the Hamming distance.
      d = seq_len - (
          a_one_hot.unsqueeze(-3) * b_one_hot.unsqueeze(-4)
      ).sum(dim=(-1, -2))
  
      # Compute the kernel evaluation.
      actual = ((1 + alpha) / (alpha + d)) ** beta
      res = kernel(a_one_hot_flat, b_one_hot_flat).to_dense()
  
      # Check the result.
      self.assertLess(torch.norm(res - actual), 1e-5)
  
file_path: gpytorch/test/kernels/test_hamming_kernel.py
imports: import pickle
  import unittest
  import torch
  from gpytorch.kernels import HammingIMQKernel
  from gpytorch.priors import GammaPrior
label_desc: Use image node_id 3 for calling the TestHammingIMQKernel obj's underlying member method code with example usage: obj.test_computes_hamming_imq_function() without return types