Compute the network output for a single timestep. Parameters ---------- Xt : :py:class:`ndarray ` of shape `(n_ex, n_in)` Input at timestep `t` consisting of `n_ex` examples each of dimensionality `n_in`. Returns
(self, Xt)
| 3667 | } |
| 3668 | |
| 3669 | def forward(self, Xt): |
| 3670 | """ |
| 3671 | Compute the network output for a single timestep. |
| 3672 | |
| 3673 | Parameters |
| 3674 | ---------- |
| 3675 | Xt : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)` |
| 3676 | Input at timestep `t` consisting of `n_ex` examples each of |
| 3677 | dimensionality `n_in`. |
| 3678 | |
| 3679 | Returns |
| 3680 | ------- |
| 3681 | At: :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_out)` |
| 3682 | The value of the hidden state at timestep `t` for each of the |
| 3683 | `n_ex` examples. |
| 3684 | """ |
| 3685 | if not self.is_initialized: |
| 3686 | self.n_in = Xt.shape[1] |
| 3687 | self._init_params() |
| 3688 | |
| 3689 | # increment timestep |
| 3690 | self.derived_variables["n_timesteps"] += 1 |
| 3691 | self.derived_variables["current_step"] += 1 |
| 3692 | |
| 3693 | # Retrieve parameters |
| 3694 | ba = self.parameters["ba"] |
| 3695 | bx = self.parameters["bx"] |
| 3696 | Wax = self.parameters["Wax"] |
| 3697 | Waa = self.parameters["Waa"] |
| 3698 | |
| 3699 | # initialize the hidden state to zero |
| 3700 | As = self.derived_variables["A"] |
| 3701 | if len(As) == 0: |
| 3702 | n_ex, n_in = Xt.shape |
| 3703 | A0 = np.zeros((n_ex, self.n_out)) |
| 3704 | As.append(A0) |
| 3705 | |
| 3706 | # compute next hidden state |
| 3707 | Zt = As[-1] @ Waa + ba.T + Xt @ Wax + bx.T |
| 3708 | At = self.act_fn(Zt) |
| 3709 | |
| 3710 | self.derived_variables["Z"].append(Zt) |
| 3711 | self.derived_variables["A"].append(At) |
| 3712 | |
| 3713 | # store intermediate variables |
| 3714 | self.X.append(Xt) |
| 3715 | return At |
| 3716 | |
| 3717 | def backward(self, dLdAt): |
| 3718 | """ |