Search code examples
scalaneural-networklinear-algebrascala-breeze

Simple neural network using linear algebra and scala breeze


Below is my implementation of a neural network with 1 input layer, two hidden layers and 1 output layer :

import breeze.linalg._
import breeze.math._
import breeze.numerics._

object NN extends App {

  //Forward propogation
  val x1 = DenseVector(1.0, 0.0, 1.0)
  val y1 = DenseVector(1.0, 1.0, 1.0)

  val theta1 = DenseMatrix((1.0, 1.0, 1.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0));
  val theta2 = DenseMatrix((1.0, 1.0, 1.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0));
  val theta3 = DenseMatrix((1.0, 1.0, 1.0), (1.0, 1.0, 0.0), (1.0, 0.0, 0.0));

  val a1 = x1;

  val z2 = theta1 * a1;
  val a2 = (z2.map { x => 1 + sigmoid(x) })

  val z3 = theta2 * a2;
  val a3 = (z3.map { x => 1 + sigmoid(x) })

  val z4 = theta3 * a3;
  val a4 = (z4.map { x => 1 + sigmoid(x) })

  //Back propagation
  val errorLayer4 = a4 - DenseVector(1.0, 1.0, 1.0)
  val errorLayer3 = (theta3.t * errorLayer4) :* (a3 :* (DenseVector(1.0, 1.0, 1.0) - a3))
  val errorLayer2 = (theta2.t * errorLayer3) :* (a2 :* (DenseVector(1.0, 1.0, 1.0) - a2))

  //Compute delta values
  val delta1 = errorLayer2 * a2.t
  val delta2 = errorLayer3 * a3.t
  val delta3 = errorLayer4 * a4.t


  //Gradient descent
  val m = 1
  val alpha = .0001
  val x = DenseVector(1.0, 0.0, 1.0)
  val y = DenseVector(1.0, 1.0, 1.0)

  val pz1 = delta1 - (alpha / m) * (x.t * (delta1 * x - y))
  val p1z1 = (sigmoid(delta1 * x)) + 1.0 
  println(p1z1);

  val pz2 = delta2 - (alpha / m) * (x.t * (delta2 * x - y))
  val p1z2 = (sigmoid(delta2 * p1z1)) + 1.0
  println(p1z2);

  val pz3 = delta3 - (alpha / m) * (x.t * (delta3 * x - y))
  val p1z3 = (sigmoid(delta3 * p1z2)) + 1.0
  println(p1z3);


}

The output of this network is :

Jun 03, 2016 7:47:50 PM com.github.fommil.netlib.BLAS <clinit>
WARNING: Failed to load implementation from: com.github.fommil.netlib.NativeSystemBLAS
Jun 03, 2016 7:47:50 PM com.github.fommil.jni.JniLoader liberalLoad
INFO: successfully loaded C:\Users\Local\Temp\jniloader3606930058943197684netlib-native_ref-win-x86_64.dll
DenseVector(2.0, 2.0, 1.9999999999946196)
DenseVector(1.0, 1.0, 1.0000000064265646)
DenseVector(1.9971047766732295, 1.9968279599465841, 1.9942769808711798)

I'm using a single training example 101 and output value of 111. The predicted value given 1,0,1 is 1.9,1.9,1.9 when the predicted value should be 1,1,1 .

I think how I'm computing the sigmoid with bias is incorrect, should the bias +1 value be added after sigmoid calculation for layer , in other words use { x => sigmoid(x+1) } instead of { x => 1 + sigmoid(x) } ?


Solution

  • A perceptron-style neuron's output is sigmoid(sum(xi * wi)) where the bias input x0 is 1, but the weight is not necessarily 1. You definitely don't sum the 1 outside the sigmoid, but you also don't sum it inside. You need a weight. So it should be equivalent to

    sigmoid(w0 + w1*x1 + w2*x2 + ...)