MCPcopy
hub / github.com/ddbourgin/numpy-ml / forward

Method forward

numpy_ml/neural_nets/layers/layers.py:3937–3992  ·  view source on GitHub ↗

Compute the layer 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)

Source from the content-addressed store, hash-verified

3935 }
3936
3937 def forward(self, Xt):
3938 """
3939 Compute the layer output for a single timestep.
3940
3941 Parameters
3942 ----------
3943 Xt : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_in)`
3944 Input at timestep t consisting of `n_ex` examples each of
3945 dimensionality `n_in`.
3946
3947 Returns
3948 -------
3949 At: :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_out)`
3950 The value of the hidden state at timestep `t` for each of the `n_ex`
3951 examples.
3952 Ct: :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, n_out)`
3953 The value of the cell/memory state at timestep `t` for each of the
3954 `n_ex` examples.
3955 """
3956 if not self.is_initialized:
3957 self.n_in = Xt.shape[1]
3958 self._init_params()
3959
3960 Wf, Wu, Wc, Wo, bf, bu, bc, bo = self._get_params()
3961
3962 self.derived_variables["n_timesteps"] += 1
3963 self.derived_variables["current_step"] += 1
3964
3965 if len(self.derived_variables["A"]) == 0:
3966 n_ex, n_in = Xt.shape
3967 init = np.zeros((n_ex, self.n_out))
3968 self.derived_variables["A"].append(init)
3969 self.derived_variables["C"].append(init)
3970
3971 A_prev = self.derived_variables["A"][-1]
3972 C_prev = self.derived_variables["C"][-1]
3973
3974 # concatenate A_prev and Xt to create Zt
3975 Zt = np.hstack([A_prev, Xt])
3976
3977 Gft = self.gate_fn(Zt @ Wf + bf)
3978 Gut = self.gate_fn(Zt @ Wu + bu)
3979 Got = self.gate_fn(Zt @ Wo + bo)
3980 Cct = self.act_fn(Zt @ Wc + bc)
3981 Ct = Gft * C_prev + Gut * Cct
3982 At = Got * self.act_fn(Ct)
3983
3984 # bookkeeping
3985 self.X.append(Xt)
3986 self.derived_variables["A"].append(At)
3987 self.derived_variables["C"].append(Ct)
3988 self.derived_variables["Gf"].append(Gft)
3989 self.derived_variables["Gu"].append(Gut)
3990 self.derived_variables["Go"].append(Got)
3991 self.derived_variables["Cc"].append(Cct)
3992 return At, Ct
3993
3994 def backward(self, dLdAt):

Callers 1

test_LSTMCellFunction · 0.95

Calls 3

_init_paramsMethod · 0.95
_get_paramsMethod · 0.95
act_fnMethod · 0.80

Tested by 1

test_LSTMCellFunction · 0.76