Skip to content

Instantly share code, notes, and snippets.

@cabloo
Last active December 11, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabloo/4520591 to your computer and use it in GitHub Desktop.
Save cabloo/4520591 to your computer and use it in GitHub Desktop.
Training neural network
//XOR logic
var xor = new NeuralNetwork( Array( 2, 3, 1 ) )
xor.train( 10000, Map( Array(0.0,1.0)->Array(1.0),Array(1.0,0.0)->Array(1.0),Array(1.0,1.0)->Array(0.0),Array(0.0,0.0)->Array(0.0)))
xor.truth_table()
//Rudimentary OCR
val zero = Array[Double](0,1,1,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,1,1,0)
val two = Array[Double](0,1,1,1,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0,0,0,1,1,1,0)
val three = Array[Double](0,1,1,1,0,0,0,0,1,0,0,1,1,1,0,0,0,0,1,0,0,1,1,1,0)
val four = Array[Double](0,1,0,1,0,0,1,0,1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,1,0)
var is_zero = new NeuralNetwork( Array( 25, 5, 5, 1 ) )
is_zero.train( 1000, Map( zero -> Array( 1.0 ), two -> Array( 0.0 ), three -> Array( 0.0 ), four -> Array( 0.0 ) ) )
is_zero.get_output( zero )(0).round //1
is_zero.get_output( two )(0).round //0
is_zero.get_output( three )(0).round //0
is_zero.get_output( four )(0).round //0
var is_two = new NeuralNetwork( Array( 25, 5, 5, 1 ) )
is_two.train( 1000, Map( zero -> Array( 0.0 ), two -> Array( 1.0 ), three -> Array( 0.0 ), four -> Array( 0.0 ) ) )
is_two.get_output( zero )(0).round //0
is_two.get_output( two )(0).round //1
is_two.get_output( three )(0).round //0
is_two.get_output( four )(0).round //0
//Getting one or two binary values off, still very accurate:
is_zero.get_output( Array[Double](0,1,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,1,1,1,0) )(0) //0.9700567363011835
is_zero.get_output( Array[Double](0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,1,0,0) )(0) //0.9705474911060848
is_zero.get_output( Array[Double](0,0,1,0,0,0,1,0,1,0,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0) )(0) //0.9696340771691928
//Checking to make sure it doesn't just answer close to 1 to everything other than two, three, and four:
is_zero.get_output( Array[Double](0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,0,1) )(0) //0.03297121454513127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment