Artificial Android Life - Artificial Neurons

Ok so I am back at it today and I am going to share with you my simple Neuron implementation. hopefully this will be easy to fallow. So like I said in my last post the Neuron is just a bunch of functions that rely on external input. So I think anyone who has been interested in neural networks has seen this little image before right?

The image looks simple but the notation threw me off the first time I looked at it.

Inputs

This is as easy as it looks it is just a list of values I use an array of Floats from my input.

Weights

This is another list of values again I use floats for these, but if you notice the notation within the weight node there is actually a function executed for each node. I could just create a class for Weight that could simplify my explanation. except for fun I am going to use Greek letters for all variable names.

/**
 * Neural network Weight representation.
 */
public class Gamma {
 
 public int eta;  // index
 public float gamma; // weight
 
 /**
  * Simple input scaling function.
  * 
  * @param phi array of network inputs
  * @return result of multiplying input at index by weight
  */
 public float delta(float[] phi) {
  return phi[eta] * value;
 }
 
}

Sigma Transfer Function

In this case and most cases sigma stands for summation or the total of a value set. Using our weight class the function would look like this.

/**
 * List of weight for scaling input values.
 */
public List gammas;

/**
 * Simple summation function each value will be scaled by weights before
 * being added to total.
 * 
 * @param phi array of network inputs
 * @return result of summation and scaling
 */
public float sigma(float[] phi) {
 int sigma = 0;
 for(Gamma gamma : gammas) {
  sigma += gamma.delta(phi);
 }
 return sigma;
}

Sigmoid Transfer Function

This is usually the deciding function that will relay the output of the neuron back to the network.

/**
 * Simple binary step function.
 * 
 * @param sigma result of summation and scaling
 * @return 1 if sigma is greater or equal to zero otherwise 0
 */
public float mu(float sigma) {
 return sigma < 0 ? 0 : 1;
}

Putting It All Together

After all that lets just put it all together in a nice neat class. And yes I am still going to use the Greek letter names...LOL

/**
 * Representation of Artificial Neuron.
 */
public class Neuron {
 
 /**
  * Neural network Weight representation.
  */
 public static class Gamma {
  
  public int eta;  // index
  public float gamma; // weight
  
  /**
   * Simple input scaling function.
   * 
   * @param phi array of network inputs
   * @return result of multiplying input at index by weight
   */
  public float delta(float[] phi) {
   return phi[eta] * value;
  }
  
 }

 /**
  * List of weight for scaling input values.
  */
 public List gammas;
 
 /**
  * Primary neuron function for processing inputs.
  * 
  * @param phi array of network inputs
  * @return result
  */
 public float lambda(float[] phi) {
  return mu(sigma(phi));
 }
 
 /**
  * Simple summation function each value will be scaled by weights before
  * being added to total.
  * 
  * @param phi array of network inputs
  * @return result of summation and scaling
  */
 public float sigma(float[] phi) {
  int sigma = 0;
  for(Gamma gamma : gammas) {
   sigma += gamma.delta(phi);
  }
  return sigma;
 }
 
 /**
  * Simple binary step function.
  * 
  * @param sigma result of summation and scaling
  * @return 1 if sigma is greater or equal to zero otherwise 0
  */
 public float mu(float sigma) {
  return sigma < 0 ? 0 : 1;
 }
 
}

Now i am expecting to see about 500,000 implementations of SkyNet out on the inter webs tomorrow or at least after my next post.

Comments

Popular Posts