def test_get_embedding(self): """Tests :func:`~texar.torch.modules.embedder.embedder_utils.get_embedding`. """ vocab_size = 100 emb = embedder_utils.get_embedding(num_embeds=vocab_size) self.assertEqual(emb.size(0), vocab_size) self.assertEqual(emb.size(1), embedder_utils.default_embedding_hparams()["dim"]) hparams = { "initializer": { "type": "torch.nn.init.uniform_", "kwargs": { 'a': -0.1, 'b': 0.1 } } } emb = embedder_utils.get_embedding( hparams=hparams, num_embeds=vocab_size, ) self.assertEqual(emb.size(0), vocab_size) self.assertEqual(emb.size(1), embedder_utils.default_embedding_hparams()["dim"])
def default_hparams(): r"""Returns a dictionary of hyperparameters with default values. .. code-block:: python { "dim": 100, "initializer": { "type": "random_uniform_initializer", "kwargs": { "minval": -0.1, "maxval": 0.1, "seed": None } }, "dropout_rate": 0, "name": "position_embedder" } The hyperparameters have the same meaning as those in :meth:`texar.torch.modules.WordEmbedder.default_hparams`. """ hparams = embedder_utils.default_embedding_hparams() hparams["name"] = "position_embedder" return hparams
def default_hparams(): # TODO Shibiao: add regularizer r"""Returns a dictionary of hyperparameters with default values. .. code-block:: python { "dim": 100, "dropout_rate": 0, "dropout_strategy": 'element', "initializer": { "type": "random_uniform_initializer", "kwargs": { "minval": -0.1, "maxval": 0.1, "seed": None } }, "name": "word_embedder", } Here: `"dim"`: int or list Embedding dimension. Can be a list of integers to yield embeddings with dimensionality > 1. Ignored if :attr:`init_value` is given to the embedder constructor. `"dropout_rate"`: float The dropout rate between 0 and 1. For example, ``dropout_rate=0.1`` would zero out 10% of the embeddings. Set to 0 to disable dropout. `"dropout_strategy"`: str The dropout strategy. Can be one of the following - ``"element"``: The regular strategy that drops individual elements in the embedding vectors. - ``"item"``: Drops individual items (e.g., words) entirely. For example, for the word sequence "the simpler the better", the strategy can yield "_ simpler the better", where the first "the" is dropped. - ``"item_type"``: Drops item types (e.g., word types). For example, for the above sequence, the strategy can yield "_ simpler _ better", where the word type "the" is dropped. The dropout will never yield "_ simpler the better" as in the ``"item"`` strategy. `"initializer"`: dict or None Hyperparameters of the initializer for embedding values. See :func:`~texar.torch.core.get_initializer` for the details. Ignored if :attr:`init_value` is given to the embedder constructor. `"name"`: str Name of the embedding variable. """ hparams = embedder_utils.default_embedding_hparams() hparams["name"] = "word_embedder" return hparams