Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class Network {
  2.  
  3.   private activations: {[type: string]: (x: number) => number}
  4.   /**
  5.    * creates a new network.
  6.    * @param {Model} the network model.
  7.    * @returns {Network}
  8.   */
  9.   constructor(private model: neuron.Model) {
  10.     this.activations = {
  11.       "linear"  : (x: number) => (x),
  12.       "sigmoid" : (x: number) => (1.0 / (1.0 + Math.exp(-x))),
  13.       "tanh"    : (x: number) => (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x))
  14.     }
  15.     // randomly initialize weights.
  16.     this.model.layers().each(layer => {
  17.       layer.neurons().skip(1).each(neuron => {
  18.         neuron.outputs().each(synapse => {
  19.           //synapse.weight(Math.random())
  20.           synapse.weight(0.5)
  21.         })
  22.       })
  23.     })
  24.   }
  25.  
  26.   /**
  27.    * forward propagates the given input through the network.
  28.    * @param {Array<number>} the input to this network.
  29.    * @returns {Array<number>} the networks output layer.
  30.    */
  31.   public forward(input: Array<number>): Array<number> {
  32.     this.model.layers()
  33.               .first()
  34.               .neurons()
  35.               .skip(1)
  36.               .each((neuron, index) =>
  37.                 neuron.value(input[index]))
  38.     this.model.layers().skip(1).each(hidden => {
  39.       let activation = this.activations[hidden.activation()]
  40.       hidden.neurons().each(neuron => {
  41.         neuron.value(
  42.             activation(
  43.               neuron.inputs().aggregate((acc, synapse) => {
  44.                 return (acc + (synapse.weight() * synapse.target().value()))
  45.               }, 0)
  46.             )
  47.           )
  48.       })
  49.     })
  50.     return this.model.layers()
  51.       .last()
  52.       .neurons()
  53.       .skip(1)
  54.       .select(neuron => neuron.value())
  55.       .collect()
  56.   }
  57.  
  58.   /**
  59.    * trains this network via back propagation where the actual value
  60.    * obtained from a call to forward() on this network is compared against
  61.    * the ideal value given.
  62.    * @param {Array<number>} the actual value for this network.
  63.    * @param {Array<number>} the ideal / expected value for this actual.
  64.    * @returns {void}
  65.    */
  66.   public backward(actual: Array<number>, ideal: Array<number>): void {
  67.     // todo: mission
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement