class_docstr: Implementation of https://arxiv.org/abs/1905.04899
class_docstr_tok: ["Implementation","of","https",":","\/\/arxiv.org\/abs\/1905.04899"]
docstr: Add `rand_bbox` patches with size based on `lam` and location chosen randomly.
docstr_tok: ["Add","`","rand_bbox","`","patches","with","size","based","on","`","lam","`","and","location","chosen","randomly","."]
code: def before_batch(self):
      "Add `rand_bbox` patches with size based on `lam` and location chosen randomly."
      bs, _, H, W = self.x.size()
      self.lam = self.distrib.sample((1,)).to(self.x.device)
      shuffle = torch.randperm(bs).to(self.x.device)
      xb1, self.yb1 = self.x[shuffle], tuple((self.y[shuffle],))
      x1, y1, x2, y2 = self.rand_bbox(W, H, self.lam)
      self.learn.xb[0][..., y1:y2, x1:x2] = xb1[..., y1:y2, x1:x2]
      self.lam = 1 - ((x2 - x1) * (y2 - y1)) / float(W * H)
      if not self.stack_y:
          ny_dims = len(self.y.size())
          self.learn.yb = tuple(
              L(self.yb1, self.yb).map_zip(
                  torch.lerp, weight=unsqueeze(self.lam, n=ny_dims - 1)
              )
          )
  
imports: from __future__ import annotations
  from ..basics import *
  from torch.distributions.beta import Beta
label_desc: Use image node_id 2 for calling the CutMix obj's underlying member method code with example usage: obj.before_batch() without return types