---
local: retribert
sections:
- local: overview
  title: Overview
- local: transformers.RetriBertConfig
  title: RetriBertConfig
- local: transformers.RetriBertTokenizer
  title: RetriBertTokenizer
- local: transformers.RetriBertTokenizerFast
  title: RetriBertTokenizerFast
- local: transformers.RetriBertModel
  title: RetriBertModel
title: RetriBERT
---
<script>
import Tip from "./Tip.svelte";
import Youtube from "./Youtube.svelte";
import Docstring from "./Docstring.svelte";
import CodeBlock from "./CodeBlock.svelte";
import CodeBlockFw from "./CodeBlockFw.svelte";
import DocNotebookDropdown from "./DocNotebookDropdown.svelte";
import IconCopyLink from "./IconCopyLink.svelte";
export let fw: "pt" | "tf"
</script>
<!--Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

<h1 id="retribert">RetriBERT</h1>

<h2 id="overview">Overview</h2>

The RetriBERT model was proposed in the blog post [Explain Anything Like I'm Five: A Model for Open Domain Long Form
Question Answering](https://yjernite.github.io/lfqa.html). RetriBERT is a small model that uses either a single or
pair of BERT encoders with lower-dimension projection for dense semantic indexing of text.

This model was contributed by [yjernite](https://huggingface.co/yjernite). Code to train and use the model can be
found [here](https://github.com/huggingface/transformers/tree/master/examples/research-projects/distillation).


<h2 id="transformers.RetriBertConfig">RetriBertConfig</h2>

<div class="docstring">

<docstring><name>class transformers.RetriBertConfig</name><anchor>transformers.RetriBertConfig</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/retribert/configuration_retribert.py#L29</source><parameters>[{"name": "vocab_size", "val": " = 30522"}, {"name": "hidden_size", "val": " = 768"}, {"name": "num_hidden_layers", "val": " = 8"}, {"name": "num_attention_heads", "val": " = 12"}, {"name": "intermediate_size", "val": " = 3072"}, {"name": "hidden_act", "val": " = 'gelu'"}, {"name": "hidden_dropout_prob", "val": " = 0.1"}, {"name": "attention_probs_dropout_prob", "val": " = 0.1"}, {"name": "max_position_embeddings", "val": " = 512"}, {"name": "type_vocab_size", "val": " = 2"}, {"name": "initializer_range", "val": " = 0.02"}, {"name": "layer_norm_eps", "val": " = 1e-12"}, {"name": "share_encoders", "val": " = True"}, {"name": "projection_dim", "val": " = 128"}, {"name": "pad_token_id", "val": " = 0"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **vocab_size** (`int`, *optional*, defaults to 30522) --
  Vocabulary size of the RetriBERT model. Defines the number of different tokens that can be represented by
  the `inputs_ids` passed when calling [RetriBertModel](/docs/transformers/pr_15592/en/model_doc/retribert#transformers.RetriBertModel)
- **hidden_size** (`int`, *optional*, defaults to 768) --
  Dimensionality of the encoder layers and the pooler layer.
- **num_hidden_layers** (`int`, *optional*, defaults to 12) --
  Number of hidden layers in the Transformer encoder.
- **num_attention_heads** (`int`, *optional*, defaults to 12) --
  Number of attention heads for each attention layer in the Transformer encoder.
- **intermediate_size** (`int`, *optional*, defaults to 3072) --
  Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
- **hidden_act** (`str` or `function`, *optional*, defaults to `"gelu"`) --
  The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  `"relu"`, `"silu"` and `"gelu_new"` are supported.
- **hidden_dropout_prob** (`float`, *optional*, defaults to 0.1) --
  The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
- **attention_probs_dropout_prob** (`float`, *optional*, defaults to 0.1) --
  The dropout ratio for the attention probabilities.
- **max_position_embeddings** (`int`, *optional*, defaults to 512) --
  The maximum sequence length that this model might ever be used with. Typically set this to something large
  just in case (e.g., 512 or 1024 or 2048).
- **type_vocab_size** (`int`, *optional*, defaults to 2) --
  The vocabulary size of the *token_type_ids* passed into [BertModel](/docs/transformers/pr_15592/en/model_doc/bert#transformers.BertModel).
- **initializer_range** (`float`, *optional*, defaults to 0.02) --
  The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
- **layer_norm_eps** (`float`, *optional*, defaults to 1e-12) --
  The epsilon used by the layer normalization layers.
- **share_encoders** (`bool`, *optional*, defaults to `True`) --
  Whether or not to use the same Bert-type encoder for the queries and document
- **projection_dim** (`int`, *optional*, defaults to 128) --
  Final dimension of the query and document representation after projection</paramsdesc><paramgroups>0</paramgroups></docstring>

This is the configuration class to store the configuration of a [RetriBertModel](/docs/transformers/pr_15592/en/model_doc/retribert#transformers.RetriBertModel). It is used to instantiate a
RetriBertModel model according to the specified arguments, defining the model architecture.

Configuration objects inherit from [PretrainedConfig](/docs/transformers/pr_15592/en/main_classes/configuration#transformers.PretrainedConfig) and can be used to control the model outputs. Read the
documentation from [PretrainedConfig](/docs/transformers/pr_15592/en/main_classes/configuration#transformers.PretrainedConfig) for more information.





</div>

<h2 id="transformers.RetriBertTokenizer">RetriBertTokenizer</h2>

<div class="docstring">

<docstring><name>class transformers.RetriBertTokenizer</name><anchor>transformers.RetriBertTokenizer</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/retribert/tokenization_retribert.py#L41</source><parameters>[{"name": "vocab_file", "val": ""}, {"name": "do_lower_case", "val": " = True"}, {"name": "do_basic_tokenize", "val": " = True"}, {"name": "never_split", "val": " = None"}, {"name": "unk_token", "val": " = '[UNK]'"}, {"name": "sep_token", "val": " = '[SEP]'"}, {"name": "pad_token", "val": " = '[PAD]'"}, {"name": "cls_token", "val": " = '[CLS]'"}, {"name": "mask_token", "val": " = '[MASK]'"}, {"name": "tokenize_chinese_chars", "val": " = True"}, {"name": "strip_accents", "val": " = None"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Constructs a RetriBERT tokenizer.

`RetroBertTokenizer` is identical to [BertTokenizer](/docs/transformers/pr_15592/en/model_doc/bert#transformers.BertTokenizer) and runs end-to-end tokenization: punctuation splitting
and wordpiece.

Refer to superclass [BertTokenizer](/docs/transformers/pr_15592/en/model_doc/bert#transformers.BertTokenizer) for usage examples and documentation concerning parameters.


</div>

<h2 id="transformers.RetriBertTokenizerFast">RetriBertTokenizerFast</h2>

<div class="docstring">

<docstring><name>class transformers.RetriBertTokenizerFast</name><anchor>transformers.RetriBertTokenizerFast</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/retribert/tokenization_retribert_fast.py#L45</source><parameters>[{"name": "vocab_file", "val": " = None"}, {"name": "tokenizer_file", "val": " = None"}, {"name": "do_lower_case", "val": " = True"}, {"name": "unk_token", "val": " = '[UNK]'"}, {"name": "sep_token", "val": " = '[SEP]'"}, {"name": "pad_token", "val": " = '[PAD]'"}, {"name": "cls_token", "val": " = '[CLS]'"}, {"name": "mask_token", "val": " = '[MASK]'"}, {"name": "tokenize_chinese_chars", "val": " = True"}, {"name": "strip_accents", "val": " = None"}, {"name": "**kwargs", "val": ""}]</parameters></docstring>

Construct a "fast" RetriBERT tokenizer (backed by HuggingFace's *tokenizers* library).

[RetriBertTokenizerFast](/docs/transformers/pr_15592/en/model_doc/retribert#transformers.RetriBertTokenizerFast) is identical to [BertTokenizerFast](/docs/transformers/pr_15592/en/model_doc/bert#transformers.BertTokenizerFast) and runs end-to-end tokenization: punctuation
splitting and wordpiece.

Refer to superclass [BertTokenizerFast](/docs/transformers/pr_15592/en/model_doc/bert#transformers.BertTokenizerFast) for usage examples and documentation concerning parameters.


</div>

<h2 id="transformers.RetriBertModel">RetriBertModel</h2>

<div class="docstring">

<docstring><name>class transformers.RetriBertModel</name><anchor>transformers.RetriBertModel</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/retribert/modeling_retribert.py#L88</source><parameters>[{"name": "config", "val": ""}]</parameters><paramsdesc>- **config** ([RetriBertConfig](/docs/transformers/pr_15592/en/model_doc/retribert#transformers.RetriBertConfig)) -- Model configuration class with all the parameters of the model.
  Initializing with a config file does not load the weights associated with the model, only the
  configuration. Check out the [from_pretrained()](/docs/transformers/pr_15592/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring>
Bert Based model to embed queries or document for document retrieval.

This model inherits from [PreTrainedModel](/docs/transformers/pr_15592/en/main_classes/model#transformers.PreTrainedModel). Check the superclass documentation for the generic methods the
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
etc.)

This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
and behavior.





<div class="docstring">
<docstring><name>forward</name><anchor>transformers.RetriBertModel.forward</anchor><source>https://github.com/huggingface/transformers/blob/pr_15592/src/transformers/models/retribert/modeling_retribert.py#L176</source><parameters>[{"name": "input_ids_query", "val": ""}, {"name": "attention_mask_query", "val": ""}, {"name": "input_ids_doc", "val": ""}, {"name": "attention_mask_doc", "val": ""}, {"name": "checkpoint_batch_size", "val": " = -1"}]</parameters><paramsdesc>- **input_ids_query** (`torch.LongTensor` of shape `(batch_size, sequence_length)`) --
  Indices of input sequence tokens in the vocabulary for the queries in a batch.

  Indices can be obtained using [RetriBertTokenizer](/docs/transformers/pr_15592/en/model_doc/retribert#transformers.RetriBertTokenizer). See [PreTrainedTokenizer.encode()](/docs/transformers/pr_15592/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode) and
  [PreTrainedTokenizer.__call__()](/docs/transformers/pr_15592/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.__call__) for details.

  [What are input IDs?](../glossary#input-ids)
- **attention_mask_query** (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*) --
  Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

  - 1 for tokens that are **not masked**,
  - 0 for tokens that are **masked**.

  [What are attention masks?](../glossary#attention-mask)
- **input_ids_doc** (`torch.LongTensor` of shape `(batch_size, sequence_length)`) --
  Indices of input sequence tokens in the vocabulary for the documents in a batch.
- **attention_mask_doc** (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*) --
  Mask to avoid performing attention on documents padding token indices.
- **checkpoint_batch_size** (`int`, *optional*, defaults to ```-1`) --
  If greater than 0, uses gradient checkpointing to only compute sequence representation on
  `checkpoint_batch_size` examples at a time on the GPU. All query representations are still compared to
  all document representations in the batch.</paramsdesc><paramgroups>0</paramgroups><rettype>`torch.FloatTensor``</rettype><retdesc>The bidirectional cross-entropy loss obtained while trying to match each query to its
corresponding document and each document to its corresponding query in the batch</retdesc></docstring>








</div></div>
