Slide 7
Slide 7 text
Matteo Grella 2021
What is a Neural Model?
1. // Model for the LSTM
2. type Model struct {
3. WIn nn.Param `spago:"type:weights"`
4. WInRec nn.Param `spago:"type:weights"`
5. BIn nn.Param `spago:"type:biases"`
6. WOut nn.Param `spago:"type:weights"`
7. WOutRec nn.Param `spago:"type:weights"`
8. BOut nn.Param `spago:"type:biases"`
9. WFor nn.Param `spago:"type:weights"`
10. WForRec nn.Param `spago:"type:weights"`
11. BFor nn.Param `spago:"type:biases"`
12. WCand nn.Param `spago:"type:weights"`
13. WCandRec nn.Param `spago:"type:weights"`
14. BCand nn.Param `spago:"type:biases"`
15. }
1. // Forward performs the forward step.
2. func (m *Model) Forward(x, yPrev, cellPrev ag.Node) (cell, y ag.Node) {
3. g := m.Graph()
4. inG := g.Sigmoid(nn.Affine(g, m.BIn, m.WIn, x, m.WInRec, yPrev))
5. outG := g.Sigmoid(nn.Affine(g, m.BOut, m.WOut, x, m.WOutRec, yPrev))
6. forG := g.Sigmoid(nn.Affine(g, m.BFor, m.WFor, x, m.WForRec, yPrev))
7. cand := g.Tanh(nn.Affine(g, m.BCand, m.WCand, x, m.WCandRec, yPrev))
8. cell = g.Add(g.Prod(inG, cand), g.Prod(forG, cellPrev))
9. y = g.Prod(s.OutG, g.Tanh(s.Cell))
10. return
11. }
Trained Parameters
(weights and biases)
Mathematical expressions
(Forward() method)
7/22