code: def try_query(self, sentences, swapped_words, batch_size=32):
      # TODO use caching
      sentence_length = len(sentences[0])
      if any(len(s) != sentence_length for s in sentences):
          raise ValueError("Only same length batches are allowed")
  
      log_probs = []
      for start in range(0, len(sentences), batch_size):
          swapped_words_batch = swapped_words[
              start : min(len(sentences), start + batch_size)
          ]
          batch = sentences[
              start : min(len(sentences), start + batch_size)
          ]
          raw_idx_list = [[] for i in range(sentence_length + 1)]
          for i, s in enumerate(batch):
              s = [word for word in s if word in self.word_to_idx]
              words = ["<S>"] + s
              word_idxs = [self.word_to_idx[w] for w in words]
              for t in range(sentence_length + 1):
                  if t < len(word_idxs):
                      raw_idx_list[t].append(word_idxs[t])
          orig_num_idxs = len(raw_idx_list)
          raw_idx_list = [x for x in raw_idx_list if len(x)]
          num_idxs_dropped = orig_num_idxs - len(raw_idx_list)
          all_raw_idxs = torch.tensor(
              raw_idx_list, device=self.device, dtype=torch.long
          )
          word_idxs = self.mapto[all_raw_idxs]
          hidden = self.model.init_hidden(len(batch))
          source = word_idxs[:-1, :]
          target = word_idxs[1:, :]
          if (not len(source)) or not len(hidden):
              return [float("-inf")] * len(batch)
          decode, hidden = self.model(source, hidden)
          decode = decode.view(
              sentence_length - num_idxs_dropped, len(batch), -1
          )
          for i in range(len(batch)):
              if swapped_words_batch[i] not in self.word_to_idx:
                  log_probs.append(float("-inf"))
              else:
                  log_probs.append(
                      sum(
                          [
                              decode[t, i, target[t, i]].item()
                              for t in range(
                                  sentence_length - num_idxs_dropped
                              )
                          ]
                      )
                  )
      return log_probs
  
file_path: TextAttack/textattack/constraints/grammaticality/language_models/learning_to_write/language_model_helpers.py
imports: import os
  import numpy
  import torch
  from textattack.shared.utils import LazyLoader
  from .rnn_model import RNNModel
label_desc: Use image node_id 3 for calling the QueryHandler obj's underlying member method code with example usage: obj.try_query(sentences, swapped_words, batch_size) and returns: log_probs, unknown