Skip to content

Instantly share code, notes, and snippets.

@kidtronnix
Last active September 7, 2018 13:51
Show Gist options
  • Save kidtronnix/8d7c173b9a9b2d807ed54c3bc2e4990a to your computer and use it in GitHub Desktop.
Save kidtronnix/8d7c173b9a9b2d807ed54c3bc2e4990a to your computer and use it in GitHub Desktop.
package nn
import "gonum.org/v1/gonum/mat"
// forward takes input data and returns the 'activation' and 'z'
// from each layer - z = w.x +b and a = sigmoid(z)
func (n *MLP) forward(x mat.Matrix) (as, zs []mat.Matrix) {
as = append(as, x) // first activation is input
_x := x
for i := 0; i < len(n.weights); i++ {
w := n.weights[i]
b := n.biases[i]
dot := new(mat.Dense)
dot.Mul(_x, w)
z := new(mat.Dense)
addB := func(_, col int, v float64) float64 { return v + b.At(col, 0) }
z.Apply(addB, dot)
a := new(mat.Dense)
a.Apply(applySigmoid, z)
zs = append(zs, z)
as = append(as, a)
_x = a
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment