configuration_luke.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # coding=utf-8
  2. # Copyright Studio Ousia and The HuggingFace Inc. team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """LUKE configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class LukeConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`LukeModel`]. It is used to instantiate a LUKE
  22. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the LUKE
  24. [studio-ousia/luke-base](https://huggingface.co/studio-ousia/luke-base) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 50267):
  29. Vocabulary size of the LUKE model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`LukeModel`].
  31. entity_vocab_size (`int`, *optional*, defaults to 500000):
  32. Entity vocabulary size of the LUKE model. Defines the number of different entities that can be represented
  33. by the `entity_ids` passed when calling [`LukeModel`].
  34. hidden_size (`int`, *optional*, defaults to 768):
  35. Dimensionality of the encoder layers and the pooler layer.
  36. entity_emb_size (`int`, *optional*, defaults to 256):
  37. The number of dimensions of the entity embedding.
  38. num_hidden_layers (`int`, *optional*, defaults to 12):
  39. Number of hidden layers in the Transformer encoder.
  40. num_attention_heads (`int`, *optional*, defaults to 12):
  41. Number of attention heads for each attention layer in the Transformer encoder.
  42. intermediate_size (`int`, *optional*, defaults to 3072):
  43. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  44. hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
  45. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  46. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  47. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  48. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  49. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  50. The dropout ratio for the attention probabilities.
  51. max_position_embeddings (`int`, *optional*, defaults to 512):
  52. The maximum sequence length that this model might ever be used with. Typically set this to something large
  53. just in case (e.g., 512 or 1024 or 2048).
  54. type_vocab_size (`int`, *optional*, defaults to 2):
  55. The vocabulary size of the `token_type_ids` passed when calling [`LukeModel`].
  56. initializer_range (`float`, *optional*, defaults to 0.02):
  57. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  58. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  59. The epsilon used by the layer normalization layers.
  60. use_entity_aware_attention (`bool`, *optional*, defaults to `True`):
  61. Whether or not the model should use the entity-aware self-attention mechanism proposed in [LUKE: Deep
  62. Contextualized Entity Representations with Entity-aware Self-attention (Yamada et
  63. al.)](https://huggingface.co/papers/2010.01057).
  64. classifier_dropout (`float`, *optional*):
  65. The dropout ratio for the classification head.
  66. pad_token_id (`int`, *optional*, defaults to 1):
  67. Padding token id.
  68. bos_token_id (`int`, *optional*, defaults to 0):
  69. Beginning of stream token id.
  70. eos_token_id (`int`, *optional*, defaults to 2):
  71. End of stream token id.
  72. Examples:
  73. ```python
  74. >>> from transformers import LukeConfig, LukeModel
  75. >>> # Initializing a LUKE configuration
  76. >>> configuration = LukeConfig()
  77. >>> # Initializing a model from the configuration
  78. >>> model = LukeModel(configuration)
  79. >>> # Accessing the model configuration
  80. >>> configuration = model.config
  81. ```"""
  82. model_type = "luke"
  83. def __init__(
  84. self,
  85. vocab_size=50267,
  86. entity_vocab_size=500000,
  87. hidden_size=768,
  88. entity_emb_size=256,
  89. num_hidden_layers=12,
  90. num_attention_heads=12,
  91. intermediate_size=3072,
  92. hidden_act="gelu",
  93. hidden_dropout_prob=0.1,
  94. attention_probs_dropout_prob=0.1,
  95. max_position_embeddings=512,
  96. type_vocab_size=2,
  97. initializer_range=0.02,
  98. layer_norm_eps=1e-12,
  99. use_entity_aware_attention=True,
  100. classifier_dropout=None,
  101. pad_token_id=1,
  102. bos_token_id=0,
  103. eos_token_id=2,
  104. **kwargs,
  105. ):
  106. """Constructs LukeConfig."""
  107. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  108. self.vocab_size = vocab_size
  109. self.entity_vocab_size = entity_vocab_size
  110. self.hidden_size = hidden_size
  111. self.entity_emb_size = entity_emb_size
  112. self.num_hidden_layers = num_hidden_layers
  113. self.num_attention_heads = num_attention_heads
  114. self.hidden_act = hidden_act
  115. self.intermediate_size = intermediate_size
  116. self.hidden_dropout_prob = hidden_dropout_prob
  117. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  118. self.max_position_embeddings = max_position_embeddings
  119. self.type_vocab_size = type_vocab_size
  120. self.initializer_range = initializer_range
  121. self.layer_norm_eps = layer_norm_eps
  122. self.use_entity_aware_attention = use_entity_aware_attention
  123. self.classifier_dropout = classifier_dropout
  124. __all__ = ["LukeConfig"]