class_docstr: An implementation for an explainable neural network for natural
  language processing and generating rationales used by that network.
  Based on the paper "Rethinking Cooperative Rationalization: Introspective
  Extraction and Complement Control" by Yu et. al.
docstr: Compute regularization loss, based on a given rationale sequence
  Use Yujia's formulation
  Inputs:
      z -- torch variable, "binary" rationale, (batch_size,
          sequence_length)
      percentage -- the percentage of words to keep
  Outputs:
      a loss value that contains two parts:
      continuity_loss -- sum_{i} | z_{i-1} - z_{i} |
      sparsity_loss -- |mean(z_{i}) - percent|
docstr_tok: ["Compute","regularization","loss",",","based","on","a","given","rationale","sequence","Use","Yujia","'s","formulation","Inputs",":","z","--","torch","variable",",","``","binary","''","rationale",",","(","batch_size",",","sequence_length",")","percentage","--","the","percentage","of","words","to","keep","Outputs",":","a","loss","value","that","contains","two","parts",":","continuity_loss","--","sum_","{","i","}","|","z_","{","i-1","}","-","z_","{","i","}","|","sparsity_loss","--","|mean","(","z_","{","i","}",")","-","percent|"]
code: def _count_regularization_baos_for_both(
      self, z, target_sparsity, count_pieces, mask=None
  ):
      """
      Compute regularization loss, based on a given rationale sequence
      Use Yujia's formulation
      Inputs:
          z -- torch variable, "binary" rationale, (batch_size,
              sequence_length)
          percentage -- the percentage of words to keep
      Outputs:
          a loss value that contains two parts:
          continuity_loss -- sum_{i} | z_{i-1} - z_{i} |
          sparsity_loss -- |mean(z_{i}) - percent|
      """
  
      # mask_z dimensions: (batch_size,)
      if mask is not None:
          mask_z = z * mask
          seq_lengths = torch.sum(mask, dim=1)
      else:
          mask_z = z
          seq_lengths = torch.sum(z - z + 1.0, dim=1)
  
      mask_z_ = torch.cat([mask_z[:, 1:], mask_z[:, -1:]], dim=-1)
  
      continuity_ratio = (
          torch.sum(torch.abs(mask_z - mask_z_), dim=-1) / seq_lengths
      )  # dimensions: (batch_size,)
      percentage = count_pieces * 2 / seq_lengths
      continuity_loss = torch.abs(continuity_ratio - percentage)
  
      sparsity_ratio = (
          torch.sum(mask_z, dim=-1) / seq_lengths
      )  # dimensions: (batch_size,)
      percentage = target_sparsity  # dimensions: (batch_size,)
      sparsity_loss = torch.abs(sparsity_ratio - percentage)
  
      return continuity_loss, sparsity_loss
  
imports: import logging
  import os
  import random
  from collections import deque
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from interpret_text.experimental.common.base_text_model import BaseTextModel
  from torch.autograd import Variable
  from tqdm import tqdm
  from interpret_text.experimental.common.utils_introspective_rationale import generate_data
label_desc: Use image node_id 5 for calling the IntrospectiveRationaleModel obj's underlying member method code with example usage: obj._count_regularization_baos_for_both(z, target_sparsity, count_pieces, mask) and returns: continuity_loss,  sparsity_loss