We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Update documentation for Embedding layer
The documentation corresponding [torch.nn.Embedding](http://pytorch.org/docs/master/nn.html) mentions that ```Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (cuda and cpu), and optim.Adagrad (cpu)```. This is outdated and now `SparseAdam` is also supported.
Update documentation for Embedding layer
The documentation corresponding [torch.nn.Embedding](http://pytorch.org/docs/master/nn.html) mentions that ```Keep in mind that only a limited number of optimizers support sparse gradients: currently it’s optim.SGD (cuda and cpu), and optim.Adagrad (cpu)```. This is outdated and now `SparseAdam` is also supported.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torch/nn/modules/sparse.py`
Content:
```
1 import torch
2 from torch.autograd import Variable
3 from torch.nn.parameter import Parameter
4 
5 from .module import Module
6 from .. import functional as F
7 
8 
9 class Embedding(Module):
10     r"""A simple lookup table that stores embeddings of a fixed dictionary and size.
11 
12     This module is often used to store word embeddings and retrieve them using indices.
13     The input to the module is a list of indices, and the output is the corresponding
14     word embeddings.
15 
16     Args:
17         num_embeddings (int): size of the dictionary of embeddings
18         embedding_dim (int): the size of each embedding vector
19         padding_idx (int, optional): If given, pads the output with zeros whenever it encounters the index.
20         max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this
21         norm_type (float, optional): The p of the p-norm to compute for the max_norm option
22         scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of
23                                                 the words in the mini-batch.
24         sparse (boolean, optional): if ``True``, gradient w.r.t. weight matrix will be a sparse tensor. See Notes for
25                                     more details regarding sparse gradients.
26 
27     Attributes:
28         weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)
29 
30     Shape:
31         - Input: LongTensor `(N, W)`, N = mini-batch, W = number of indices to extract per mini-batch
32         - Output: `(N, W, embedding_dim)`
33 
34     Notes:
35         Keep in mind that only a limited number of optimizers support
36         sparse gradients: currently it's `optim.SGD` (`cuda` and `cpu`),
37         and `optim.Adagrad` (`cpu`)
38 
39     Examples::
40 
41         >>> # an Embedding module containing 10 tensors of size 3
42         >>> embedding = nn.Embedding(10, 3)
43         >>> # a batch of 2 samples of 4 indices each
44         >>> input = Variable(torch.LongTensor([[1,2,4,5],[4,3,2,9]]))
45         >>> embedding(input)
46 
47         Variable containing:
48         (0 ,.,.) =
49          -1.0822  1.2522  0.2434
50           0.8393 -0.6062 -0.3348
51           0.6597  0.0350  0.0837
52           0.5521  0.9447  0.0498
53 
54         (1 ,.,.) =
55           0.6597  0.0350  0.0837
56          -0.1527  0.0877  0.4260
57           0.8393 -0.6062 -0.3348
58          -0.8738 -0.9054  0.4281
59         [torch.FloatTensor of size 2x4x3]
60 
61         >>> # example with padding_idx
62         >>> embedding = nn.Embedding(10, 3, padding_idx=0)
63         >>> input = Variable(torch.LongTensor([[0,2,0,5]]))
64         >>> embedding(input)
65 
66         Variable containing:
67         (0 ,.,.) =
68           0.0000  0.0000  0.0000
69           0.3452  0.4937 -0.9361
70           0.0000  0.0000  0.0000
71           0.0706 -2.1962 -0.6276
72         [torch.FloatTensor of size 1x4x3]
73 
74     """
75 
76     def __init__(self, num_embeddings, embedding_dim, padding_idx=None,
77                  max_norm=None, norm_type=2, scale_grad_by_freq=False,
78                  sparse=False):
79         super(Embedding, self).__init__()
80         self.num_embeddings = num_embeddings
81         self.embedding_dim = embedding_dim
82         if padding_idx is not None:
83             if padding_idx > 0:
84                 assert padding_idx < self.num_embeddings, 'Padding_idx must be within num_embeddings'
85             elif padding_idx < 0:
86                 assert padding_idx >= -self.num_embeddings, 'Padding_idx must be within num_embeddings'
87                 padding_idx = self.num_embeddings + padding_idx
88         self.padding_idx = padding_idx
89         self.max_norm = max_norm
90         self.norm_type = norm_type
91         self.scale_grad_by_freq = scale_grad_by_freq
92         self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))
93         self.sparse = sparse
94 
95         self.reset_parameters()
96 
97     def reset_parameters(self):
98         self.weight.data.normal_(0, 1)
99         if self.padding_idx is not None:
100             self.weight.data[self.padding_idx].fill_(0)
101 
102     def forward(self, input):
103         return F.embedding(
104             input, self.weight, self.padding_idx, self.max_norm,
105             self.norm_type, self.scale_grad_by_freq, self.sparse)
106 
107     def __repr__(self):
108         s = '{name}({num_embeddings}, {embedding_dim}'
109         if self.padding_idx is not None:
110             s += ', padding_idx={padding_idx}'
111         if self.max_norm is not None:
112             s += ', max_norm={max_norm}'
113         if self.norm_type != 2:
114             s += ', norm_type={norm_type}'
115         if self.scale_grad_by_freq is not False:
116             s += ', scale_grad_by_freq={scale_grad_by_freq}'
117         if self.sparse is not False:
118             s += ', sparse=True'
119         s += ')'
120         return s.format(name=self.__class__.__name__, **self.__dict__)
121 
122 
123 class EmbeddingBag(Module):
124     r"""Computes sums or means of 'bags' of embeddings, without instantiating the
125     intermediate embeddings.
126 
127     For bags of constant length,
128         * nn.EmbeddingBag with `mode=sum` is equivalent to nn.Embedding followed by `torch.sum(dim=1)`
129         * with `mode=mean` is equivalent to nn.Embedding followed by `torch.mean(dim=1)`
130 
131     However, nn.EmbeddingBag is much more time and memory efficient than using a chain of these
132     operations.
133 
134     Args:
135         num_embeddings (int): size of the dictionary of embeddings
136         embedding_dim (int): the size of each embedding vector
137         max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this
138         norm_type (float, optional): The p of the p-norm to compute for the max_norm option
139         scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of
140                                                 the words in the dictionary.
141         mode (string, optional): 'sum' | 'mean'. Specifies the way to reduce the bag. Default: 'mean'
142 
143     Attributes:
144         weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)
145 
146     Inputs: input, offsets
147         - **input** (N or BxN): LongTensor containing the indices of the embeddings
148                                 to extract. When `input` is 1D Tensor of shape `N`,
149                                 an `offsets` Tensor is given, that contains the
150                                 starting position of each new sequence in the
151                                 mini-batch.
152         - **offsets** (B or None): LongTensor containing the starting positions of
153                                    each sample in a mini-batch of variable length
154                                    sequences. If `input` is 2D (BxN), then offsets
155                                    does not need to be given, as the `input` is
156                                    treated as a mini-batch of fixed length sequences
157                                    of length `N` each.
158 
159 
160     Shape:
161         - Input: LongTensor `N`, N = number of embeddings to extract
162                  (or) LongTensor `BxN`, B = number of sequences in mini-batch,
163                                         N = number of embeddings per sequence
164         - Offsets: LongTensor `B`, B = number of bags. The values are the
165                    offsets in `input` for each bag, i.e. the cumsum of lengths.
166                    Offsets is not given if Input is 2D `BxN` Tensor,
167                    the input is considered to be of fixed-length sequences
168         - Output: `(B, embedding_dim)`
169 
170     Examples::
171 
172         >>> # an Embedding module containing 10 tensors of size 3
173         >>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')
174         >>> # a batch of 2 samples of 4 indices each
175         >>> input = Variable(torch.LongTensor([1,2,4,5,4,3,2,9]))
176         >>> offsets = Variable(torch.LongTensor([0,4]))
177         >>> embedding_sum(input, offsets)
178 
179         Variable containing:
180         -0.7296 -4.6926  0.3295
181         -0.5186 -0.5631 -0.2792
182         [torch.FloatTensor of size 2x3]
183 
184     """
185 
186     def __init__(self, num_embeddings, embedding_dim,
187                  max_norm=None, norm_type=2, scale_grad_by_freq=False,
188                  mode='mean'):
189         super(EmbeddingBag, self).__init__()
190         self.num_embeddings = num_embeddings
191         self.embedding_dim = embedding_dim
192         self.max_norm = max_norm
193         self.norm_type = norm_type
194         self.scale_grad_by_freq = scale_grad_by_freq
195         self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))
196         self.mode = mode
197 
198         self.reset_parameters()
199 
200     def reset_parameters(self):
201         self.weight.data.normal_(0, 1)
202 
203     def forward(self, input, offsets=None):
204         return F.embedding_bag(self.weight, input, offsets,
205                                self.max_norm, self.norm_type,
206                                self.scale_grad_by_freq, self.mode)
207 
208     def __repr__(self):
209         s = '{name}({num_embeddings}, {embedding_dim}'
210         if self.max_norm is not None:
211             s += ', max_norm={max_norm}'
212         if self.norm_type != 2:
213             s += ', norm_type={norm_type}'
214         if self.scale_grad_by_freq is not False:
215             s += ', scale_grad_by_freq={scale_grad_by_freq}'
216         s += ', mode={mode}'
217         s += ')'
218         return s.format(name=self.__class__.__name__, **self.__dict__)
219 
220 # TODO: SparseLinear
221
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torch/nn/modules/sparse.py b/torch/nn/modules/sparse.py
--- a/torch/nn/modules/sparse.py
+++ b/torch/nn/modules/sparse.py
@@ -34,7 +34,7 @@
     Notes:
         Keep in mind that only a limited number of optimizers support
         sparse gradients: currently it's `optim.SGD` (`cuda` and `cpu`),
-        and `optim.Adagrad` (`cpu`)
+        `optim.SparseAdam` (`cuda` and `cpu`) and `optim.Adagrad` (`cpu`)
 
     Examples::

{"golden_diff": "diff --git a/torch/nn/modules/sparse.py b/torch/nn/modules/sparse.py\n--- a/torch/nn/modules/sparse.py\n+++ b/torch/nn/modules/sparse.py\n@@ -34,7 +34,7 @@\n     Notes:\n         Keep in mind that only a limited number of optimizers support\n         sparse gradients: currently it's `optim.SGD` (`cuda` and `cpu`),\n-        and `optim.Adagrad` (`cpu`)\n+        `optim.SparseAdam` (`cuda` and `cpu`) and `optim.Adagrad` (`cpu`)\n \n     Examples::\n", "issue": "Update documentation for Embedding layer\nThe documentation corresponding [torch.nn.Embedding](http://pytorch.org/docs/master/nn.html) mentions that ```Keep in mind that only a limited number of optimizers support sparse gradients: currently it\u2019s optim.SGD (cuda and cpu), and optim.Adagrad (cpu)```. This is outdated and now `SparseAdam` is also supported.\nUpdate documentation for Embedding layer\nThe documentation corresponding [torch.nn.Embedding](http://pytorch.org/docs/master/nn.html) mentions that ```Keep in mind that only a limited number of optimizers support sparse gradients: currently it\u2019s optim.SGD (cuda and cpu), and optim.Adagrad (cpu)```. This is outdated and now `SparseAdam` is also supported.\n", "before_files": [{"content": "import torch\nfrom torch.autograd import Variable\nfrom torch.nn.parameter import Parameter\n\nfrom .module import Module\nfrom .. import functional as F\n\n\nclass Embedding(Module):\n    r\"\"\"A simple lookup table that stores embeddings of a fixed dictionary and size.\n\n    This module is often used to store word embeddings and retrieve them using indices.\n    The input to the module is a list of indices, and the output is the corresponding\n    word embeddings.\n\n    Args:\n        num_embeddings (int): size of the dictionary of embeddings\n        embedding_dim (int): the size of each embedding vector\n        padding_idx (int, optional): If given, pads the output with zeros whenever it encounters the index.\n        max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this\n        norm_type (float, optional): The p of the p-norm to compute for the max_norm option\n        scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of\n                                                the words in the mini-batch.\n        sparse (boolean, optional): if ``True``, gradient w.r.t. weight matrix will be a sparse tensor. See Notes for\n                                    more details regarding sparse gradients.\n\n    Attributes:\n        weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)\n\n    Shape:\n        - Input: LongTensor `(N, W)`, N = mini-batch, W = number of indices to extract per mini-batch\n        - Output: `(N, W, embedding_dim)`\n\n    Notes:\n        Keep in mind that only a limited number of optimizers support\n        sparse gradients: currently it's `optim.SGD` (`cuda` and `cpu`),\n        and `optim.Adagrad` (`cpu`)\n\n    Examples::\n\n        >>> # an Embedding module containing 10 tensors of size 3\n        >>> embedding = nn.Embedding(10, 3)\n        >>> # a batch of 2 samples of 4 indices each\n        >>> input = Variable(torch.LongTensor([[1,2,4,5],[4,3,2,9]]))\n        >>> embedding(input)\n\n        Variable containing:\n        (0 ,.,.) =\n         -1.0822  1.2522  0.2434\n          0.8393 -0.6062 -0.3348\n          0.6597  0.0350  0.0837\n          0.5521  0.9447  0.0498\n\n        (1 ,.,.) =\n          0.6597  0.0350  0.0837\n         -0.1527  0.0877  0.4260\n          0.8393 -0.6062 -0.3348\n         -0.8738 -0.9054  0.4281\n        [torch.FloatTensor of size 2x4x3]\n\n        >>> # example with padding_idx\n        >>> embedding = nn.Embedding(10, 3, padding_idx=0)\n        >>> input = Variable(torch.LongTensor([[0,2,0,5]]))\n        >>> embedding(input)\n\n        Variable containing:\n        (0 ,.,.) =\n          0.0000  0.0000  0.0000\n          0.3452  0.4937 -0.9361\n          0.0000  0.0000  0.0000\n          0.0706 -2.1962 -0.6276\n        [torch.FloatTensor of size 1x4x3]\n\n    \"\"\"\n\n    def __init__(self, num_embeddings, embedding_dim, padding_idx=None,\n                 max_norm=None, norm_type=2, scale_grad_by_freq=False,\n                 sparse=False):\n        super(Embedding, self).__init__()\n        self.num_embeddings = num_embeddings\n        self.embedding_dim = embedding_dim\n        if padding_idx is not None:\n            if padding_idx > 0:\n                assert padding_idx < self.num_embeddings, 'Padding_idx must be within num_embeddings'\n            elif padding_idx < 0:\n                assert padding_idx >= -self.num_embeddings, 'Padding_idx must be within num_embeddings'\n                padding_idx = self.num_embeddings + padding_idx\n        self.padding_idx = padding_idx\n        self.max_norm = max_norm\n        self.norm_type = norm_type\n        self.scale_grad_by_freq = scale_grad_by_freq\n        self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))\n        self.sparse = sparse\n\n        self.reset_parameters()\n\n    def reset_parameters(self):\n        self.weight.data.normal_(0, 1)\n        if self.padding_idx is not None:\n            self.weight.data[self.padding_idx].fill_(0)\n\n    def forward(self, input):\n        return F.embedding(\n            input, self.weight, self.padding_idx, self.max_norm,\n            self.norm_type, self.scale_grad_by_freq, self.sparse)\n\n    def __repr__(self):\n        s = '{name}({num_embeddings}, {embedding_dim}'\n        if self.padding_idx is not None:\n            s += ', padding_idx={padding_idx}'\n        if self.max_norm is not None:\n            s += ', max_norm={max_norm}'\n        if self.norm_type != 2:\n            s += ', norm_type={norm_type}'\n        if self.scale_grad_by_freq is not False:\n            s += ', scale_grad_by_freq={scale_grad_by_freq}'\n        if self.sparse is not False:\n            s += ', sparse=True'\n        s += ')'\n        return s.format(name=self.__class__.__name__, **self.__dict__)\n\n\nclass EmbeddingBag(Module):\n    r\"\"\"Computes sums or means of 'bags' of embeddings, without instantiating the\n    intermediate embeddings.\n\n    For bags of constant length,\n        * nn.EmbeddingBag with `mode=sum` is equivalent to nn.Embedding followed by `torch.sum(dim=1)`\n        * with `mode=mean` is equivalent to nn.Embedding followed by `torch.mean(dim=1)`\n\n    However, nn.EmbeddingBag is much more time and memory efficient than using a chain of these\n    operations.\n\n    Args:\n        num_embeddings (int): size of the dictionary of embeddings\n        embedding_dim (int): the size of each embedding vector\n        max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this\n        norm_type (float, optional): The p of the p-norm to compute for the max_norm option\n        scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of\n                                                the words in the dictionary.\n        mode (string, optional): 'sum' | 'mean'. Specifies the way to reduce the bag. Default: 'mean'\n\n    Attributes:\n        weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)\n\n    Inputs: input, offsets\n        - **input** (N or BxN): LongTensor containing the indices of the embeddings\n                                to extract. When `input` is 1D Tensor of shape `N`,\n                                an `offsets` Tensor is given, that contains the\n                                starting position of each new sequence in the\n                                mini-batch.\n        - **offsets** (B or None): LongTensor containing the starting positions of\n                                   each sample in a mini-batch of variable length\n                                   sequences. If `input` is 2D (BxN), then offsets\n                                   does not need to be given, as the `input` is\n                                   treated as a mini-batch of fixed length sequences\n                                   of length `N` each.\n\n\n    Shape:\n        - Input: LongTensor `N`, N = number of embeddings to extract\n                 (or) LongTensor `BxN`, B = number of sequences in mini-batch,\n                                        N = number of embeddings per sequence\n        - Offsets: LongTensor `B`, B = number of bags. The values are the\n                   offsets in `input` for each bag, i.e. the cumsum of lengths.\n                   Offsets is not given if Input is 2D `BxN` Tensor,\n                   the input is considered to be of fixed-length sequences\n        - Output: `(B, embedding_dim)`\n\n    Examples::\n\n        >>> # an Embedding module containing 10 tensors of size 3\n        >>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')\n        >>> # a batch of 2 samples of 4 indices each\n        >>> input = Variable(torch.LongTensor([1,2,4,5,4,3,2,9]))\n        >>> offsets = Variable(torch.LongTensor([0,4]))\n        >>> embedding_sum(input, offsets)\n\n        Variable containing:\n        -0.7296 -4.6926  0.3295\n        -0.5186 -0.5631 -0.2792\n        [torch.FloatTensor of size 2x3]\n\n    \"\"\"\n\n    def __init__(self, num_embeddings, embedding_dim,\n                 max_norm=None, norm_type=2, scale_grad_by_freq=False,\n                 mode='mean'):\n        super(EmbeddingBag, self).__init__()\n        self.num_embeddings = num_embeddings\n        self.embedding_dim = embedding_dim\n        self.max_norm = max_norm\n        self.norm_type = norm_type\n        self.scale_grad_by_freq = scale_grad_by_freq\n        self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))\n        self.mode = mode\n\n        self.reset_parameters()\n\n    def reset_parameters(self):\n        self.weight.data.normal_(0, 1)\n\n    def forward(self, input, offsets=None):\n        return F.embedding_bag(self.weight, input, offsets,\n                               self.max_norm, self.norm_type,\n                               self.scale_grad_by_freq, self.mode)\n\n    def __repr__(self):\n        s = '{name}({num_embeddings}, {embedding_dim}'\n        if self.max_norm is not None:\n            s += ', max_norm={max_norm}'\n        if self.norm_type != 2:\n            s += ', norm_type={norm_type}'\n        if self.scale_grad_by_freq is not False:\n            s += ', scale_grad_by_freq={scale_grad_by_freq}'\n        s += ', mode={mode}'\n        s += ')'\n        return s.format(name=self.__class__.__name__, **self.__dict__)\n\n# TODO: SparseLinear\n", "path": "torch/nn/modules/sparse.py"}], "after_files": [{"content": "import torch\nfrom torch.autograd import Variable\nfrom torch.nn.parameter import Parameter\n\nfrom .module import Module\nfrom .. import functional as F\n\n\nclass Embedding(Module):\n    r\"\"\"A simple lookup table that stores embeddings of a fixed dictionary and size.\n\n    This module is often used to store word embeddings and retrieve them using indices.\n    The input to the module is a list of indices, and the output is the corresponding\n    word embeddings.\n\n    Args:\n        num_embeddings (int): size of the dictionary of embeddings\n        embedding_dim (int): the size of each embedding vector\n        padding_idx (int, optional): If given, pads the output with zeros whenever it encounters the index.\n        max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this\n        norm_type (float, optional): The p of the p-norm to compute for the max_norm option\n        scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of\n                                                the words in the mini-batch.\n        sparse (boolean, optional): if ``True``, gradient w.r.t. weight matrix will be a sparse tensor. See Notes for\n                                    more details regarding sparse gradients.\n\n    Attributes:\n        weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)\n\n    Shape:\n        - Input: LongTensor `(N, W)`, N = mini-batch, W = number of indices to extract per mini-batch\n        - Output: `(N, W, embedding_dim)`\n\n    Notes:\n        Keep in mind that only a limited number of optimizers support\n        sparse gradients: currently it's `optim.SGD` (`cuda` and `cpu`),\n        `optim.SparseAdam` (`cuda` and `cpu`) and `optim.Adagrad` (`cpu`)\n\n    Examples::\n\n        >>> # an Embedding module containing 10 tensors of size 3\n        >>> embedding = nn.Embedding(10, 3)\n        >>> # a batch of 2 samples of 4 indices each\n        >>> input = Variable(torch.LongTensor([[1,2,4,5],[4,3,2,9]]))\n        >>> embedding(input)\n\n        Variable containing:\n        (0 ,.,.) =\n         -1.0822  1.2522  0.2434\n          0.8393 -0.6062 -0.3348\n          0.6597  0.0350  0.0837\n          0.5521  0.9447  0.0498\n\n        (1 ,.,.) =\n          0.6597  0.0350  0.0837\n         -0.1527  0.0877  0.4260\n          0.8393 -0.6062 -0.3348\n         -0.8738 -0.9054  0.4281\n        [torch.FloatTensor of size 2x4x3]\n\n        >>> # example with padding_idx\n        >>> embedding = nn.Embedding(10, 3, padding_idx=0)\n        >>> input = Variable(torch.LongTensor([[0,2,0,5]]))\n        >>> embedding(input)\n\n        Variable containing:\n        (0 ,.,.) =\n          0.0000  0.0000  0.0000\n          0.3452  0.4937 -0.9361\n          0.0000  0.0000  0.0000\n          0.0706 -2.1962 -0.6276\n        [torch.FloatTensor of size 1x4x3]\n\n    \"\"\"\n\n    def __init__(self, num_embeddings, embedding_dim, padding_idx=None,\n                 max_norm=None, norm_type=2, scale_grad_by_freq=False,\n                 sparse=False):\n        super(Embedding, self).__init__()\n        self.num_embeddings = num_embeddings\n        self.embedding_dim = embedding_dim\n        if padding_idx is not None:\n            if padding_idx > 0:\n                assert padding_idx < self.num_embeddings, 'Padding_idx must be within num_embeddings'\n            elif padding_idx < 0:\n                assert padding_idx >= -self.num_embeddings, 'Padding_idx must be within num_embeddings'\n                padding_idx = self.num_embeddings + padding_idx\n        self.padding_idx = padding_idx\n        self.max_norm = max_norm\n        self.norm_type = norm_type\n        self.scale_grad_by_freq = scale_grad_by_freq\n        self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))\n        self.sparse = sparse\n\n        self.reset_parameters()\n\n    def reset_parameters(self):\n        self.weight.data.normal_(0, 1)\n        if self.padding_idx is not None:\n            self.weight.data[self.padding_idx].fill_(0)\n\n    def forward(self, input):\n        return F.embedding(\n            input, self.weight, self.padding_idx, self.max_norm,\n            self.norm_type, self.scale_grad_by_freq, self.sparse)\n\n    def __repr__(self):\n        s = '{name}({num_embeddings}, {embedding_dim}'\n        if self.padding_idx is not None:\n            s += ', padding_idx={padding_idx}'\n        if self.max_norm is not None:\n            s += ', max_norm={max_norm}'\n        if self.norm_type != 2:\n            s += ', norm_type={norm_type}'\n        if self.scale_grad_by_freq is not False:\n            s += ', scale_grad_by_freq={scale_grad_by_freq}'\n        if self.sparse is not False:\n            s += ', sparse=True'\n        s += ')'\n        return s.format(name=self.__class__.__name__, **self.__dict__)\n\n\nclass EmbeddingBag(Module):\n    r\"\"\"Computes sums or means of 'bags' of embeddings, without instantiating the\n    intermediate embeddings.\n\n    For bags of constant length,\n        * nn.EmbeddingBag with `mode=sum` is equivalent to nn.Embedding followed by `torch.sum(dim=1)`\n        * with `mode=mean` is equivalent to nn.Embedding followed by `torch.mean(dim=1)`\n\n    However, nn.EmbeddingBag is much more time and memory efficient than using a chain of these\n    operations.\n\n    Args:\n        num_embeddings (int): size of the dictionary of embeddings\n        embedding_dim (int): the size of each embedding vector\n        max_norm (float, optional): If given, will renormalize the embeddings to always have a norm lesser than this\n        norm_type (float, optional): The p of the p-norm to compute for the max_norm option\n        scale_grad_by_freq (boolean, optional): if given, this will scale gradients by the frequency of\n                                                the words in the dictionary.\n        mode (string, optional): 'sum' | 'mean'. Specifies the way to reduce the bag. Default: 'mean'\n\n    Attributes:\n        weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim)\n\n    Inputs: input, offsets\n        - **input** (N or BxN): LongTensor containing the indices of the embeddings\n                                to extract. When `input` is 1D Tensor of shape `N`,\n                                an `offsets` Tensor is given, that contains the\n                                starting position of each new sequence in the\n                                mini-batch.\n        - **offsets** (B or None): LongTensor containing the starting positions of\n                                   each sample in a mini-batch of variable length\n                                   sequences. If `input` is 2D (BxN), then offsets\n                                   does not need to be given, as the `input` is\n                                   treated as a mini-batch of fixed length sequences\n                                   of length `N` each.\n\n\n    Shape:\n        - Input: LongTensor `N`, N = number of embeddings to extract\n                 (or) LongTensor `BxN`, B = number of sequences in mini-batch,\n                                        N = number of embeddings per sequence\n        - Offsets: LongTensor `B`, B = number of bags. The values are the\n                   offsets in `input` for each bag, i.e. the cumsum of lengths.\n                   Offsets is not given if Input is 2D `BxN` Tensor,\n                   the input is considered to be of fixed-length sequences\n        - Output: `(B, embedding_dim)`\n\n    Examples::\n\n        >>> # an Embedding module containing 10 tensors of size 3\n        >>> embedding_sum = nn.EmbeddingBag(10, 3, mode='sum')\n        >>> # a batch of 2 samples of 4 indices each\n        >>> input = Variable(torch.LongTensor([1,2,4,5,4,3,2,9]))\n        >>> offsets = Variable(torch.LongTensor([0,4]))\n        >>> embedding_sum(input, offsets)\n\n        Variable containing:\n        -0.7296 -4.6926  0.3295\n        -0.5186 -0.5631 -0.2792\n        [torch.FloatTensor of size 2x3]\n\n    \"\"\"\n\n    def __init__(self, num_embeddings, embedding_dim,\n                 max_norm=None, norm_type=2, scale_grad_by_freq=False,\n                 mode='mean'):\n        super(EmbeddingBag, self).__init__()\n        self.num_embeddings = num_embeddings\n        self.embedding_dim = embedding_dim\n        self.max_norm = max_norm\n        self.norm_type = norm_type\n        self.scale_grad_by_freq = scale_grad_by_freq\n        self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))\n        self.mode = mode\n\n        self.reset_parameters()\n\n    def reset_parameters(self):\n        self.weight.data.normal_(0, 1)\n\n    def forward(self, input, offsets=None):\n        return F.embedding_bag(self.weight, input, offsets,\n                               self.max_norm, self.norm_type,\n                               self.scale_grad_by_freq, self.mode)\n\n    def __repr__(self):\n        s = '{name}({num_embeddings}, {embedding_dim}'\n        if self.max_norm is not None:\n            s += ', max_norm={max_norm}'\n        if self.norm_type != 2:\n            s += ', norm_type={norm_type}'\n        if self.scale_grad_by_freq is not False:\n            s += ', scale_grad_by_freq={scale_grad_by_freq}'\n        s += ', mode={mode}'\n        s += ')'\n        return s.format(name=self.__class__.__name__, **self.__dict__)\n\n# TODO: SparseLinear\n", "path": "torch/nn/modules/sparse.py"}]}