| 1809 | |
| 1810 | |
| 1811 | class Embedding(LayerBase): |
| 1812 | def __init__( |
| 1813 | self, n_out, vocab_size, pool=None, init="glorot_uniform", optimizer=None, |
| 1814 | ): |
| 1815 | """ |
| 1816 | An embedding layer. |
| 1817 | |
| 1818 | Notes |
| 1819 | ----- |
| 1820 | Equations:: |
| 1821 | |
| 1822 | Y = W[x] |
| 1823 | |
| 1824 | NB. This layer must be the first in a neural network as the gradients |
| 1825 | do not get passed back through to the inputs. |
| 1826 | |
| 1827 | Parameters |
| 1828 | ---------- |
| 1829 | n_out : int |
| 1830 | The dimensionality of the embeddings |
| 1831 | vocab_size : int |
| 1832 | The total number of items in the vocabulary. All integer indices |
| 1833 | are expected to range between 0 and `vocab_size - 1`. |
| 1834 | pool : {'sum', 'mean', None} |
| 1835 | If not None, apply this function to the collection of `n_in` |
| 1836 | encodings in each example to produce a single, pooled embedding. |
| 1837 | Default is None. |
| 1838 | init : {'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform'} |
| 1839 | The weight initialization strategy. Default is `'glorot_uniform'`. |
| 1840 | optimizer : str, :doc:`Optimizer <numpy_ml.neural_nets.optimizers>` object, or None |
| 1841 | The optimization strategy to use when performing gradient updates |
| 1842 | within the :meth:`update` method. If None, use the :class:`SGD |
| 1843 | <numpy_ml.neural_nets.optimizers.SGD>` optimizer with |
| 1844 | default parameters. Default is None. |
| 1845 | |
| 1846 | Attributes |
| 1847 | ---------- |
| 1848 | X : list |
| 1849 | Running list of inputs to the :meth:`forward <numpy_ml.neural_nets.LayerBase.forward>` method since the last call to :meth:`update <numpy_ml.neural_nets.LayerBase.update>`. Only updated if the `retain_derived` argument was set to True. |
| 1850 | gradients : dict |
| 1851 | Dictionary of loss gradients with regard to the layer parameters |
| 1852 | parameters : dict |
| 1853 | Dictionary of layer parameters |
| 1854 | hyperparameters : dict |
| 1855 | Dictionary of layer hyperparameters |
| 1856 | derived_variables : dict |
| 1857 | Dictionary of any intermediate values computed during |
| 1858 | forward/backward propagation. |
| 1859 | """ # noqa: E501 |
| 1860 | super().__init__(optimizer) |
| 1861 | fstr = "'pool' must be either 'sum', 'mean', or None but got '{}'" |
| 1862 | assert pool in ["sum", "mean", None], fstr.format(pool) |
| 1863 | |
| 1864 | self.init = init |
| 1865 | self.pool = pool |
| 1866 | self.n_out = n_out |
| 1867 | self.vocab_size = vocab_size |
| 1868 | self.parameters = {"W": None} |
no outgoing calls