class_docstr: ## Sampler
  
  This samples a sketch from the decoder and plots it
class_docstr_tok: ["#","#","Sampler","This","samples","a","sketch","from","the","decoder","and","plots","it"]
code: def sample(self, data: torch.Tensor, temperature: float):
      # $N_{max}$
      longest_seq_len = len(data)
  
      # Get $z$ from the encoder
      z, _, _ = self.encoder(data)
  
      # Start-of-sequence stroke is $(0, 0, 1, 0, 0)$
      s = data.new_tensor([0, 0, 1, 0, 0])
      seq = [s]
      # Initial decoder is `None`.
      # The decoder will initialize it to $[h_0; c_0] = \tanh(W_{z}z + b_z)$
      state = None
  
      # We don't need gradients
      with torch.no_grad():
          # Sample $N_{max}$ strokes
          for i in range(longest_seq_len):
              # $[(\Delta x, \Delta y, p_1, p_2, p_3); z]$ is the input to the decoder
              data = torch.cat([s.view(1, 1, -1), z.unsqueeze(0)], 2)
              # Get $\Pi$, $\mathcal{N}(\mu_{x}, \mu_{y}, \sigma_{x}, \sigma_{y}, \rho_{xy})$,
              # $q$ and the next state from the decoder
              dist, q_logits, state = self.decoder(data, z, state)
              # Sample a stroke
              s = self._sample_step(dist, q_logits, temperature)
              # Add the new stroke to the sequence of strokes
              seq.append(s)
              # Stop sampling if $p_3 = 1$. This indicates that sketching has stopped
              if s[4] == 1:
                  break
  
      # Create a PyTorch tensor of the sequence of strokes
      seq = torch.stack(seq)
  
      # Plot the sequence of strokes
      self.plot(seq)
  
imports: import math
  from typing import Optional, Tuple, Any
  import numpy
  import torch
  import torch.nn
  from matplotlib import pyplot
  from torch import optim
  from torch.utils.data import Dataset, DataLoader
  import einops
  from labml import lab, experiment, tracker, monit
  from labml_helpers.device import DeviceConfigs
  from labml_helpers.module import Module
  from labml_helpers.optimizer import OptimizerConfigs
  from labml_helpers.train_valid import TrainValidConfigs, hook_model_outputs, BatchIndex
label_desc: Use image node_id 2 for calling the Sampler obj's underlying member method code with example usage: obj.sample(data, temperature) without return types