docstr: Return cross-entropy across all tokens in batch, and the number of target tokens
  in this batch.
docstr_tok: ["Return","cross-entropy","across","all","tokens","in","batch",",","and","the","number","of","target","tokens","in","this","batch","."]
code: def cross_entropy_batch(
      self, batch: List[str]
  ) -> List[Tuple[float, int]]:
      """
      Return cross-entropy across all tokens in batch, and the number of target tokens
      in this batch.
      """
      h_encodings = self.ppl_tokenizer(
          batch,
          return_tensors="pt",
          padding=True,
      )
      input_ids = h_encodings.input_ids
      attn_mask = h_encodings.attention_mask
  
      # find token count for each string, to be used for averaging later
      token_counts = attn_mask.sum(-1).tolist()
  
      # add start token
      bos_tokens_tensor = torch.tensor(
          [[self.ppl_tokenizer.bos_token_id]] * input_ids.size(dim=0)
      )
      input_ids = torch.cat([bos_tokens_tensor, input_ids], dim=1)
      attn_mask = torch.cat(
          [
              torch.ones(bos_tokens_tensor.size(), dtype=torch.int64),
              attn_mask,
          ],
          dim=1,
      )
  
      losses = self.cross_entropy_strided(
          input_ids=input_ids, attention_mask=attn_mask
      )
  
      assert len(losses) == len(
          batch
      ), "Losses should be separated by example."
  
      return list(zip(losses, token_counts))
  
imports: from collections import defaultdict
  import itertools
  import math
  import numpy
  from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
  import torch
  from torch import nn
  from tqdm import tqdm
  from projects.roscoe.utils import cosine_similarity_scaled, embedding_alignment, al_mean
label_desc: Use image node_id 20 for calling the Evaluator obj's underlying member method code with example usage: obj.cross_entropy_batch(batch) and returns: list