class_docstr: Sample from a set of points according to their max posterior value.
  
  Example:
      >>> MPS = MaxPosteriorSampling(model)  # model w/ feature dim d=3
      >>> X = torch.rand(2, 100, 3)
      >>> sampled_X = MPS(X, num_samples=5)
code: def maximize_samples(
      self, X: Tensor, samples: Tensor, num_samples: int = 1
  ):
      obj = self.objective(
          samples, X=X
      )  # num_samples x batch_shape x N
      if self.replacement:
          # if we allow replacement then things are simple(r)
          idcs = torch.argmax(obj, dim=-1)
      else:
          # if we need to deduplicate we have to do some tensor acrobatics
          # first we get the indices associated w/ the num_samples top samples
          _, idcs_full = torch.topk(obj, num_samples, dim=-1)
          # generate some indices to smartly index into the lower triangle of
          # idcs_full (broadcasting across batch dimensions)
          ridx, cindx = torch.tril_indices(num_samples, num_samples)
          # pick the unique indices in order - since we look at the lower triangle
          # of the index matrix and we don't sort, this achieves deduplication
          sub_idcs = idcs_full[ridx, ..., cindx]
          if sub_idcs.ndim == 1:
              idcs = _flip_sub_unique(sub_idcs, num_samples)
          elif sub_idcs.ndim == 2:
              # TODO: Find a better way to do this
              n_b = sub_idcs.size(-1)
              idcs = torch.stack(
                  [
                      _flip_sub_unique(sub_idcs[:, i], num_samples)
                      for i in range(n_b)
                  ],
                  dim=-1,
              )
          else:
              # TODO: Find a general way to do this efficiently.
              raise NotImplementedError(
                  "MaxPosteriorSampling without replacement for more than a single "
                  "batch dimension is not yet implemented."
              )
      # idcs is num_samples x batch_shape, to index into X we need to permute for it
      # to have shape batch_shape x num_samples
      if idcs.ndim > 1:
          idcs = idcs.permute(*range(1, idcs.ndim), 0)
      # in order to use gather, we need to repeat the index tensor d times
      idcs = idcs.unsqueeze(-1).expand(*idcs.shape, X.size(-1))
      # now if the model is batched batch_shape will not necessarily be the
      # batch_shape of X, so we expand X to the proper shape
      Xe = X.expand(*obj.shape[1:], X.size(-1))
      # finally we can gather along the N dimension
      return torch.gather(Xe, -2, idcs)
  
imports: from __future__ import annotations
  from abc import ABC, abstractmethod
  from typing import Any, Optional, Union
  import torch
  from botorch.acquisition.acquisition import AcquisitionFunction
  from botorch.acquisition.objective import IdentityMCObjective, MCAcquisitionObjective, PosteriorTransform
  from botorch.generation.utils import _flip_sub_unique
  from botorch.models.model import Model
  from botorch.models.model_list_gp_regression import ModelListGP
  from botorch.models.multitask import MultiTaskGP
  from botorch.utils.sampling import batched_multinomial
  from botorch.utils.transforms import standardize
  from torch import Tensor
  from torch.nn import Module
label_desc: Use image node_id 3 for calling the MaxPosteriorSampling obj's underlying member method code with example usage: obj.maximize_samples(X, samples, num_samples) and returns: torch