Layers

Just like before, we need to consider our input and output. We are using MNIST again, since encoding digits is a useful feature. As such, we know that our input is 784 pixels, and we know that our output must also have 784 pixels.

Since we already have helper functions to decode our input and output into tensors, we can just leave that work aside and go straight to our neural network. Our network is as follows:

We can reuse most of our code from the last example and just change up our layers:

func newNN(g *gorgonia.ExprGraph) *nn {
// Create node for w/weight
w0 := gorgonia.NewMatrix(g, dt, gorgonia.WithShape(784, 128), gorgonia.WithName("w0"), gorgonia.WithInit(gorgonia.GlorotU(1.0)))
w1 := gorgonia.NewMatrix(g, dt, gorgonia.WithShape(128, 64), gorgonia.WithName("w1"), gorgonia.WithInit(gorgonia.GlorotU(1.0)))
w2 := gorgonia.NewMatrix(g, dt, gorgonia.WithShape(64, 128), gorgonia.WithName("w2"), gorgonia.WithInit(gorgonia.GlorotU(1.0)))
w3 := gorgonia.NewMatrix(g, dt, gorgonia.WithShape(128, 784), gorgonia.WithName("w3"), gorgonia.WithInit(gorgonia.GlorotU(1.0)))

return &nn{
g: g,
w0: w0,
w1: w1,
w2: w2,
w3: w3,
}
}

However, this time, we won't use ReLU activation functions, as we know our output has to be zeros and ones. We are using the Sigmoid activation function, as this gives us a convenient output. As you can see in the following code block, while we are using it for every layer, you could also just use ReLU activation functions everywhere except the last layer, since the output layer should ideally be constrained to values between 0 and 1:

func (m *nn) fwd(x *gorgonia.Node) (err error) {
var l0, l1, l2, l3, l4 *gorgonia.Node
var l0dot, l1dot, l2dot, l3dot *gorgonia.Node

// Set first layer to be copy of input
l0 = x

// Dot product of l0 and w0, use as input for Sigmoid
if l0dot, err = gorgonia.Mul(l0, m.w0); err != nil {
return errors.Wrap(err, "Unable to multiple l0 and w0")
}
l1 = gorgonia.Must(gorgonia.Sigmoid(l0dot))

if l1dot, err = gorgonia.Mul(l1, m.w1); err != nil {
return errors.Wrap(err, "Unable to multiple l1 and w1")
}
l2 = gorgonia.Must(gorgonia.Sigmoid(l1dot))

if l2dot, err = gorgonia.Mul(l2, m.w2); err != nil {
return errors.Wrap(err, "Unable to multiple l2 and w2")
}
l3 = gorgonia.Must(gorgonia.Sigmoid(l2dot))

if l3dot, err = gorgonia.Mul(l3, m.w3); err != nil {
return errors.Wrap(err, "Unable to multiple l3 and w3")
}
l4 = gorgonia.Must(gorgonia.Sigmoid(l3dot))

// m.pred = l3dot
// gorgonia.Read(m.pred, &m.predVal)
// return nil

m.out = l4
gorgonia.Read(l4, &m.predVal)
return

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.254.192