class_docstr: Table Question Answering pipeline using a `ModelForTableQuestionAnswering`. This pipeline is only available in
  PyTorch.
  
  Example:
  
  ```python
  >>> from transformers import pipeline
  
  >>> oracle = pipeline(model="google/tapas-base-finetuned-wtq")
  >>> table = {
  ...     "Repository": ["Transformers", "Datasets", "Tokenizers"],
  ...     "Stars": ["36542", "4512", "3934"],
  ...     "Contributors": ["651", "77", "34"],
  ...     "Programming language": ["Python", "Python", "Rust, Python and NodeJS"],
  ... }
  >>> oracle(query="How many stars does the transformers repository have?", table=table)
  {'answer': 'AVERAGE > 36542', 'coordinates': [(0, 1)], 'cells': ['36542'], 'aggregator': 'AVERAGE'}
  ```
  
  Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)
  
  This tabular question answering pipeline can currently be loaded from [`pipeline`] using the following task
  identifier: `"table-question-answering"`.
  
  The models that this pipeline can use are models that have been fine-tuned on a tabular question answering task.
  See the up-to-date list of available models on
  [huggingface.co/models](https://huggingface.co/models?filter=table-question-answering).
class_docstr_tok: ["Table","Question","Answering","pipeline","using","a","`","ModelForTableQuestionAnswering","`",".","This","pipeline","is","only","available","in","PyTorch",".","Example",":","``","`","python",">",">",">","from","transformers","import","pipeline",">",">",">","oracle","=","pipeline","(","model=","''","google\/tapas-base-finetuned-wtq","''",")",">",">",">","table","=","{","...","``","Repository","''",":","[","``","Transformers","''",",","``","Datasets","''",",","``","Tokenizers","''","]",",","...","``","Stars","''",":","[","``","36542","''",",","``","4512","''",",","``","3934","''","]",",","...","``","Contributors","''",":","[","``","651","''",",","``","77","''",",","``","34","''","]",",","...","``","Programming","language","''",":","[","``","Python","''",",","``","Python","''",",","``","Rust",",","Python","and","NodeJS","''","]",",","...","}",">",">",">","oracle","(","query=","''","How","many","stars","does","the","transformers","repository","have","?","``",",","table=table",")","{","'answer","'",":","'AVERAGE",">","36542","'",",","'coordinates","'",":","[","(","0",",","1",")","]",",","'cells","'",":","[","'36542","'","]",",","'aggregator","'",":","'AVERAGE","'","}","``","`","Learn","more","about","the","basics","of","using","a","pipeline","in","the","[","pipeline","tutorial","]","(","..","\/pipeline_tutorial",")","This","tabular","question","answering","pipeline","can","currently","be","loaded","from","[","`","pipeline","`","]","using","the","following","task","identifier",":","`","``","table-question-answering","''","`",".","The","models","that","this","pipeline","can","use","are","models","that","have","been","fine-tuned","on","a","tabular","question","answering","task",".","See","the","up-to-date","list","of","available","models","on","[","huggingface.co\/models","]","(","https",":","\/\/huggingface.co\/models","?","filter=table-question-answering",")","."]
docstr: Inference used for models that need to process sequences in a sequential fashion, like the SQA models which
  handle conversational query related to a table.
docstr_tok: ["Inference","used","for","models","that","need","to","process","sequences","in","a","sequential","fashion",",","like","the","SQA","models","which","handle","conversational","query","related","to","a","table","."]
code: def sequential_inference(self, **inputs):
      """
      Inference used for models that need to process sequences in a sequential fashion, like the SQA models which
      handle conversational query related to a table.
      """
      if self.framework == "pt":
          all_logits = []
          all_aggregations = []
          prev_answers = None
          batch_size = inputs["input_ids"].shape[0]
  
          input_ids = inputs["input_ids"].to(self.device)
          attention_mask = inputs["attention_mask"].to(self.device)
          token_type_ids = inputs["token_type_ids"].to(self.device)
          token_type_ids_example = None
  
          for index in range(batch_size):
              # If sequences have already been processed, the token type IDs will be created according to the previous
              # answer.
              if prev_answers is not None:
                  prev_labels_example = token_type_ids_example[
                      :, 3
                  ]  # shape (seq_len,)
                  model_labels = np.zeros_like(
                      prev_labels_example.cpu().numpy()
                  )  # shape (seq_len,)
  
                  token_type_ids_example = token_type_ids[
                      index
                  ]  # shape (seq_len, 7)
                  for i in range(model_labels.shape[0]):
                      segment_id = token_type_ids_example[
                          :, 0
                      ].tolist()[i]
                      col_id = (
                          token_type_ids_example[:, 1].tolist()[i] - 1
                      )
                      row_id = (
                          token_type_ids_example[:, 2].tolist()[i] - 1
                      )
  
                      if (
                          row_id >= 0
                          and col_id >= 0
                          and segment_id == 1
                      ):
                          model_labels[i] = int(
                              prev_answers[(col_id, row_id)]
                          )
  
                  token_type_ids_example[:, 3] = (
                      torch.from_numpy(model_labels)
                      .type(torch.long)
                      .to(self.device)
                  )
  
              input_ids_example = input_ids[index]
              attention_mask_example = attention_mask[
                  index
              ]  # shape (seq_len,)
              token_type_ids_example = token_type_ids[
                  index
              ]  # shape (seq_len, 7)
              outputs = self.model(
                  input_ids=input_ids_example.unsqueeze(0),
                  attention_mask=attention_mask_example.unsqueeze(0),
                  token_type_ids=token_type_ids_example.unsqueeze(0),
              )
              logits = outputs.logits
  
              if self.aggregate:
                  all_aggregations.append(outputs.logits_aggregation)
  
              all_logits.append(logits)
  
              dist_per_token = torch.distributions.Bernoulli(
                  logits=logits
              )
              probabilities = (
                  dist_per_token.probs
                  * attention_mask_example.type(torch.float32).to(
                      dist_per_token.probs.device
                  )
              )
  
              coords_to_probs = collections.defaultdict(list)
              for i, p in enumerate(probabilities.squeeze().tolist()):
                  segment_id = token_type_ids_example[:, 0].tolist()[i]
                  col = token_type_ids_example[:, 1].tolist()[i] - 1
                  row = token_type_ids_example[:, 2].tolist()[i] - 1
                  if col >= 0 and row >= 0 and segment_id == 1:
                      coords_to_probs[(col, row)].append(p)
  
              prev_answers = {
                  key: np.array(coords_to_probs[key]).mean() > 0.5
                  for key in coords_to_probs
              }
  
          logits_batch = torch.cat(tuple(all_logits), 0)
  
          return (
              (logits_batch,)
              if not self.aggregate
              else (logits_batch, torch.cat(tuple(all_aggregations), 0))
          )
      else:
          all_logits = []
          all_aggregations = []
          prev_answers = None
          batch_size = inputs["input_ids"].shape[0]
  
          input_ids = inputs["input_ids"]
          attention_mask = inputs["attention_mask"]
          token_type_ids = inputs["token_type_ids"].numpy()
          token_type_ids_example = None
  
          for index in range(batch_size):
              # If sequences have already been processed, the token type IDs will be created according to the previous
              # answer.
              if prev_answers is not None:
                  prev_labels_example = token_type_ids_example[
                      :, 3
                  ]  # shape (seq_len,)
                  model_labels = np.zeros_like(
                      prev_labels_example, dtype=np.int32
                  )  # shape (seq_len,)
  
                  token_type_ids_example = token_type_ids[
                      index
                  ]  # shape (seq_len, 7)
                  for i in range(model_labels.shape[0]):
                      segment_id = token_type_ids_example[
                          :, 0
                      ].tolist()[i]
                      col_id = (
                          token_type_ids_example[:, 1].tolist()[i] - 1
                      )
                      row_id = (
                          token_type_ids_example[:, 2].tolist()[i] - 1
                      )
  
                      if (
                          row_id >= 0
                          and col_id >= 0
                          and segment_id == 1
                      ):
                          model_labels[i] = int(
                              prev_answers[(col_id, row_id)]
                          )
  
                  token_type_ids_example[:, 3] = model_labels
  
              input_ids_example = input_ids[index]
              attention_mask_example = attention_mask[
                  index
              ]  # shape (seq_len,)
              token_type_ids_example = token_type_ids[
                  index
              ]  # shape (seq_len, 7)
              outputs = self.model(
                  input_ids=np.expand_dims(input_ids_example, axis=0),
                  attention_mask=np.expand_dims(
                      attention_mask_example, axis=0
                  ),
                  token_type_ids=np.expand_dims(
                      token_type_ids_example, axis=0
                  ),
              )
              logits = outputs.logits
  
              if self.aggregate:
                  all_aggregations.append(outputs.logits_aggregation)
  
              all_logits.append(logits)
  
              dist_per_token = tfp.distributions.Bernoulli(
                  logits=logits
              )
              probabilities = (
                  dist_per_token.probs_parameter()
                  * tf.cast(attention_mask_example, tf.float32)
              )
  
              coords_to_probs = collections.defaultdict(list)
              token_type_ids_example = token_type_ids_example
              for i, p in enumerate(
                  tf.squeeze(probabilities).numpy().tolist()
              ):
                  segment_id = token_type_ids_example[:, 0].tolist()[i]
                  col = token_type_ids_example[:, 1].tolist()[i] - 1
                  row = token_type_ids_example[:, 2].tolist()[i] - 1
                  if col >= 0 and row >= 0 and segment_id == 1:
                      coords_to_probs[(col, row)].append(p)
  
              prev_answers = {
                  key: np.array(coords_to_probs[key]).mean() > 0.5
                  for key in coords_to_probs
              }
  
          logits_batch = tf.concat(tuple(all_logits), 0)
  
          return (
              (logits_batch,)
              if not self.aggregate
              else (logits_batch, tf.concat(tuple(all_aggregations), 0))
          )
  
file_path: transformers/src/transformers/pipelines/table_question_answering.py
imports: import collections
  import types
  import numpy
  from ..utils import add_end_docstrings, is_tensorflow_probability_available, is_tf_available, is_torch_available, requires_backends
  from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException
label_desc: Use image node_id 3 for calling the TableQuestionAnsweringPipeline obj's underlying member method code with example usage: obj.sequential_inference() and returns: unknown, unknown