Compute the layer output on a single minibatch. Notes ----- Equations: Y = W[x] Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, n_in)` or list of length `n_ex` Layer input, representing
(self, X, retain_derived=True)
| 1910 | return self.parameters["W"][ids] |
| 1911 | |
| 1912 | def forward(self, X, retain_derived=True): |
| 1913 | """ |
| 1914 | Compute the layer output on a single minibatch. |
| 1915 | |
| 1916 | Notes |
| 1917 | ----- |
| 1918 | Equations: |
| 1919 | Y = W[x] |
| 1920 | |
| 1921 | Parameters |
| 1922 | ---------- |
| 1923 | X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)` or list of length `n_ex` |
| 1924 | Layer input, representing a minibatch of `n_ex` examples. If |
| 1925 | ``self.pool`` is None, each example must consist of exactly `n_in` |
| 1926 | integer token IDs. Otherwise, `X` can be a ragged array, with each |
| 1927 | example consisting of a variable number of token IDs. |
| 1928 | retain_derived : bool |
| 1929 | Whether to retain the variables calculated during the forward pass |
| 1930 | for use later during backprop. If False, this suggests the layer |
| 1931 | will not be expected to backprop through with regard to this input. |
| 1932 | Default is True. |
| 1933 | |
| 1934 | Returns |
| 1935 | ------- |
| 1936 | Y : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in, n_out)` |
| 1937 | Embeddings for each coordinate of each of the `n_ex` examples |
| 1938 | """ # noqa: E501 |
| 1939 | # if X is a ragged array |
| 1940 | if isinstance(X, list) and not issubclass(X[0].dtype.type, np.integer): |
| 1941 | fstr = "Input to Embedding layer must be an array of integers, got '{}'" |
| 1942 | raise TypeError(fstr.format(X[0].dtype.type)) |
| 1943 | |
| 1944 | # otherwise |
| 1945 | if isinstance(X, np.ndarray) and not issubclass(X.dtype.type, np.integer): |
| 1946 | fstr = "Input to Embedding layer must be an array of integers, got '{}'" |
| 1947 | raise TypeError(fstr.format(X.dtype.type)) |
| 1948 | |
| 1949 | Y = self._fwd(X) |
| 1950 | if retain_derived: |
| 1951 | self.X.append(X) |
| 1952 | return Y |
| 1953 | |
| 1954 | def _fwd(self, X): |
| 1955 | """Actual computation of forward pass""" |