class_docstr: ## Trainer
docstr: ### Sampling function to generate samples periodically while training
docstr_tok: ["#","#","#","Sampling","function","to","generate","samples","periodically","while","training"]
code: def sample(self):
      """
      ### Sampling function to generate samples periodically while training
      """
  
      # Starting prompt
      prompt = "It is"
      # Collect output for printing
      log = [(prompt, Text.subtle)]
      # Sample 25 tokens
      for i in monit.iterate("Sample", 25):
          # Tokenize the prompt
          data = self.dataset.text_to_i(prompt).unsqueeze(-1)
          data = data.to(self.device)
          # Get the model output
          output = self.model(data)
          # Get the model prediction (greedy)
          output = output.argmax(dim=-1).squeeze()
          # Add the prediction to prompt
          prompt += self.dataset.itos[output[-1].item()]
          # Add the prediction for logging
          log += [(self.dataset.itos[output[-1].item()], Text.value)]
  
      # Print the sampled output
      logger.log(log)
  
imports: import dataclasses
  import torch
  from labml_helpers.module import Module
  from torch import nn
  from torch.utils.data import Dataset, DataLoader
  from labml import experiment, lab, tracker, monit, logger
  from labml.logger import Text
  from labml.utils.download import download_file
  from labml_nn.experiments.nlp_autoregression import transpose_batch
  from labml_nn.optimizers.noam import Noam
  from labml_nn.transformers import Encoder, MultiHeadAttention
  from labml_nn.transformers.feed_forward import FeedForward
  from labml_nn.transformers.models import EmbeddingsWithPositionalEncoding, TransformerLayer
  from labml_nn.transformers.utils import subsequent_mask
label_desc: Use image node_id 2 for calling the Trainer obj's underlying member method code with example usage: obj.sample() without return types