| 631 | |
| 632 | |
| 633 | class Add(LayerBase): |
| 634 | def __init__(self, act_fn=None, optimizer=None): |
| 635 | """ |
| 636 | An "addition" layer that returns the sum of its inputs, passed through |
| 637 | an optional nonlinearity. |
| 638 | |
| 639 | Parameters |
| 640 | ---------- |
| 641 | act_fn : str, :doc:`Activation <numpy_ml.neural_nets.activations>` object, or None |
| 642 | The element-wise output nonlinearity used in computing the final |
| 643 | output. If None, use the identity function :math:`f(x) = x`. |
| 644 | Default is None. |
| 645 | optimizer : str, :doc:`Optimizer <numpy_ml.neural_nets.optimizers>` object, or None |
| 646 | The optimization strategy to use when performing gradient updates |
| 647 | within the :meth:`update` method. If None, use the :class:`SGD |
| 648 | <numpy_ml.neural_nets.optimizers.SGD>` optimizer with |
| 649 | default parameters. Default is None. |
| 650 | |
| 651 | Attributes |
| 652 | ---------- |
| 653 | X : list |
| 654 | 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. |
| 655 | gradients : dict |
| 656 | Unused |
| 657 | parameters : dict |
| 658 | Unused |
| 659 | hyperparameters : dict |
| 660 | Dictionary of layer hyperparameters |
| 661 | derived_variables : dict |
| 662 | Dictionary of any intermediate values computed during |
| 663 | forward/backward propagation. |
| 664 | """ # noqa: E501 |
| 665 | super().__init__(optimizer) |
| 666 | self.act_fn = ActivationInitializer(act_fn)() |
| 667 | self._init_params() |
| 668 | |
| 669 | def _init_params(self): |
| 670 | self.gradients = {} |
| 671 | self.parameters = {} |
| 672 | self.derived_variables = {"sum": []} |
| 673 | |
| 674 | @property |
| 675 | def hyperparameters(self): |
| 676 | """Return a dictionary containing the layer hyperparameters.""" |
| 677 | return { |
| 678 | "layer": "Sum", |
| 679 | "act_fn": str(self.act_fn), |
| 680 | "optimizer": { |
| 681 | "cache": self.optimizer.cache, |
| 682 | "hyperparameters": self.optimizer.hyperparameters, |
| 683 | }, |
| 684 | } |
| 685 | |
| 686 | def forward(self, X, retain_derived=True): |
| 687 | r""" |
| 688 | Compute the layer output on a single minibatch. |
| 689 | |
| 690 | Parameters |
no outgoing calls