83296

XOR Java Neural Network

Attempting a XOR neural network in java, but the network always predicts the final output it trains in.

Here is my code

for( int i = 0; i < 4; i++ ) { //Forward pass diff = 1; while( diff > 0.01 ) { SumError = 0; Y1 = ( InputOne[i] * Weight[0] ) + ( InputTwo[i] * Weight[1] ) + Weight[6]; Y1 = 1 / ( 1 + Math.exp( -Y1 ) ); Node1[i] = Y1; Y2 = ( InputOne[i] * Weight[2] ) + ( InputTwo[i] * Weight[3] ) + Weight[7]; Y2 = 1 / ( 1 + Math.exp( -Y2 ) ); Node2[i] = Y2; Y3 = ( Y1 * Weight[4] ) + ( Y2 * Weight[5] ) + Weight[8]; Y3 = 1 / ( 1 + Math.exp( -Y3 ) ); Node3[i] = Y3; diff = Math.abs( Result[i] - Y3 ); System.out.println( i + " " + Result[i] + " " + Y3 + " " + diff ); //Error Signals Delta3[i] = Y3 * ( 1 - Y3 ) * ( Result[i] - Y3 ); Delta2[i] = Node2[i] * ( 1 - Node2[i] ) * ( Delta3[i] * Weight[5] ); Delta1[i] = Node1[i] * ( 1 - Node1[i] ) * ( Delta3[i] * Weight[4] ); //Update Weights Weight[0] = Weight[0] + ( ( WeightChange[0] * alpha ) + ( eta * Delta2[i] * InputOne[i] ) ); Weight[2] = Weight[2] + ( ( WeightChange[2] * alpha ) + ( eta * Delta2[i] * InputTwo[i] ) ); Weight[1] = Weight[1] + ( ( WeightChange[1] * alpha ) + ( eta * Delta1[i] * InputOne[i] ) ); Weight[3] = Weight[3] + ( ( WeightChange[3] * alpha ) + ( eta * Delta1[i] * InputTwo[i] ) ); Weight[4] = Weight[4] + ( ( WeightChange[4] * alpha ) + ( eta * Delta3[i] * Y1 ) ); Weight[5] = Weight[5] + ( ( WeightChange[5] * alpha ) + ( eta * Delta3[i] * Y2 ) ); Weight[6] = Weight[6] + ( ( WeightChange[6] * alpha ) + ( eta * Delta1[i] ) ); Weight[7] = Weight[7] + ( ( WeightChange[7] * alpha ) + ( eta * Delta2[i] ) ); Weight[8] = Weight[8] + ( ( WeightChange[8] * alpha ) + ( eta * Delta3[i] ) ); for( int k = 0; k < 9; k++ ) { WeightChange[k] = OldWeight[k] - Weight[k]; OldWeight[k] = Weight[k]; } //Global Error for( int j = 0; j < 4; j++ ) { Y1 = ( InputOne[j] * Weight[0] ) + ( InputTwo[j] * Weight[1] ) + Weight[6]; Y1 = 1 / ( 1 + Math.exp( -Y1 ) ); Y2 = ( InputOne[j] * Weight[2] ) + ( InputTwo[j] * Weight[3] ) + Weight[7]; Y2 = 1 / ( 1 + Math.exp( -Y2 ) ); Y3 = ( Y1 * Weight[4] ) + ( Y2 * Weight[5] ) + Weight[8]; Y3 = 1 / ( 1 + Math.exp( -Y3 ) ); //System.out.println( Y3 + " " + Math.abs( Result[j] - Y3 ) ); SumError = SumError + Math.pow( ( Result[j] - Y3 ) , 2 ); } SumError = SumError * 0.5; } Count = Count + 1; }

Where InputOne, InputTwo and Result are the truth table entries for XOR, the weights are randomly assigned and WeightChange is the momentum.

I then feed in the truth table again and every output is more or less the same as the last input it trained on.

Does anybody have any ideas?

Answer1:

You should train for case1(once),case2(once),case3(once),case4(once) --> repeat again until it learns all four. Not a single case. Make single iteration per case. You need to make it learn plastic. For your teaching scheme, when you teach case2, it forgets case1. You need to feed all cases repeatedly in a common while loop until total error diminishes to a lower limit.

When you make it learn only single case, it learns it good but forgets others. So you feed one after another and take a group of cases' total error (maybe sum of squared errors) to be smaller than a tolerance.

Recommend

  • How to control node labels in Sankey diagram
  • XSL - Copy tree, copying some nodes twice and changing attribute in second copy
  • How to build several left join query in Entity Framework Core for several many to one related entiti
  • How to check XML has a node or it is an empty file using XSLT?
  • jQuery 1.6.1 add a new node in XML
  • Algorithm for solving simultaneous equations [closed]
  • How do I call the Pie function?
  • Java: Parse XML and bind all data content to a Map
  • Google Street View container inside Shiny Application
  • Label Areas in Python Matplotlib stackplot
  • Apply kurtosis to a distribution in python
  • How to create a Json object in Asp.net mvc 3 controller?
  • Verbose log abbriviations meaning in SVC, scikit-learn
  • How to get (-8)^0.333333 = -2 in MATLAB?
  • How to move object along the polygons
  • Java Circle-Circle Collision Detection
  • double precision error when converting to scientific notation
  • Color a heatmap in Python/Matplotlib according to requirement
  • Qt - Cannot get lambda to work [duplicate]
  • How to make a matlab legend recognize multiple scatter plots?
  • sql left join returns
  • Batching in py2neo
  • Adding/Removing Lines from a JPanel
  • Creating a layer of gradient within an SVG path dynamically
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • Jetty Server not starting: Unable to establish loopback connection
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • php design question - will a Helper help here?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • AngularJs get employee from factory
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass