Guest User

Untitled

a guest
Aug 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. public class NeuralNode
  2. {
  3. private double value;
  4. private double[] weights; //weights going out of this node
  5. private double bias;
  6. private double delta;
  7.  
  8. public NeuralNode(double val)
  9. {
  10. value = val;
  11. }
  12.  
  13. public void setValue(double val)
  14. {
  15. value = val;
  16. }
  17.  
  18. public double getValue()
  19. {
  20. return value;
  21. }
  22.  
  23. public void setWeights(double[] weights)//sets weights from this node to the connecting nodes of the next layer
  24. {
  25. this.weights = weights;
  26. }
  27.  
  28. public double getWeightTo(int i)
  29. {
  30. return weights[i];
  31. }
  32.  
  33. public double getBias()
  34. {
  35. return bias;
  36. }
  37.  
  38. public void setBias(double bias)
  39. {
  40. this.bias = bias;
  41. }
  42.  
  43. public double getDelta()
  44. {
  45. return delta;
  46. }
  47.  
  48. public void setDelta(double delta)
  49. {
  50. this.delta = delta;
  51. }
  52. }
  53. =================================================
  54. public class NeuralLayer
  55. {
  56. NeuralNode[] nodes;
  57.  
  58. public NeuralLayer(int numNodes)
  59. {
  60. nodes = new NeuralNode[numNodes];
  61. for (int i=0; i<nodes.length; i++)
  62. {
  63. nodes[i] = new NeuralNode(0.0);
  64. }
  65. }
  66.  
  67. public void setNodeAt(int pos, NeuralNode node)
  68. {
  69. nodes[pos] = node;
  70. }
  71.  
  72. public NeuralNode getNodeAt(int i)
  73. {
  74. return nodes[i];
  75. }
  76.  
  77. public NeuralNode[] getNodes()
  78. {
  79. return nodes;
  80. }
  81.  
  82. public int getSize()
  83. {
  84. return nodes.length;
  85. }
  86.  
  87. public String toString()
  88. {
  89. String s = "";
  90. for (int i=0; i<nodes.length; i++)
  91. s += (nodes[i].getValue() + " ");
  92. return s;
  93. }
  94. }
  95. ==================================================
  96. import java.lang.*;
  97.  
  98. /** Neural Network class has an array of layers
  99. * each layer has an array of nodes
  100. * and each node has an array of weights coming from it
  101. * excluding those in the output layer.
  102. */
  103.  
  104. public class NeuralNetwork
  105. {
  106. private static NeuralLayer[] layers;
  107. private NeuralLayer inputs;
  108. private double[] targets;
  109. private double trainingRate;
  110. //numLayers would be an int array telling how many nodes per layer
  111. public NeuralNetwork(NeuralLayer inputLayer, double[] targetValues, int[] numLayers)
  112. {
  113. layers = new NeuralLayer[numLayers.length];
  114. for (int i=0; i<layers.length; i++)
  115. layers[i] = new NeuralLayer(numLayers[i]);
  116.  
  117. inputs = inputLayer;
  118. targets = targetValues;
  119.  
  120. layers[0] = inputs;
  121. }
  122.  
  123. public NeuralNetwork(double[] inputs, double[] targetValues, int[] numLayers)
  124. {
  125. layers = new NeuralLayer[numLayers.length];
  126. for (int i=0; i<layers.length; i++)
  127. layers[i] = new NeuralLayer(numLayers[i]);
  128.  
  129. NeuralNode[] inputNodes = new NeuralNode[inputs.length];
  130. for (int i=0; i<inputs.length; i++)
  131. {
  132. inputNodes[i] = new NeuralNode(inputs[i]);
  133. }
  134. this.inputs = new NeuralLayer(inputNodes.length);
  135. targets = targetValues;
  136.  
  137. layers[0] = this.inputs;
  138. }
  139.  
  140. public void initializeRandomWeights() //set random weights and bias
  141. {
  142. NeuralLayer nextLayer; //layer of nodes recieving the weights
  143. double[] rWeights; //random weights
  144. double rBias;
  145. for (int L=0; L<layers.length-1; L++)
  146. {
  147. for (int n=0; n<layers[L].getSize(); n++)
  148. { //I initially had this statement after "nextLayer = layers[l+1];" but I realized it
  149. rWeights = randomWeights(L); //has to be in this loop
  150. rBias = Math.random()*2; //or else it would send the same array to all the nodes
  151. layers[L].getNodeAt(n).setBias(rBias);
  152. layers[L].getNodeAt(n).setWeights(rWeights);
  153. }
  154. }
  155. }
  156.  
  157. public double[] randomWeights(int layer)
  158. {
  159. int size = layers[layer+1].getSize();
  160. double[] w = new double[size];
  161. for (int i=0; i<size; i++)
  162. w[i] = Math.random()*2;
  163. return w;
  164. }
  165.  
  166. public double getWeightFrom(int layer, int nodeFrom, int nodeTo) //layer,
  167. { // node in the layer,
  168. return layers[layer].getNodeAt(nodeFrom).getWeightTo(nodeTo); //which node in the next layer
  169. }
  170.  
  171. public double sigmoid(double x)
  172. {
  173. return 1.0/(1.0 + Math.exp(-x));
  174. }
  175.  
  176. public double sigmoidDerivative(double x)
  177. {
  178. return sigmoid(x)*(1 - sigmoid(x));
  179. }
  180.  
  181. public double calcNode(int layer, int node)
  182. {
  183. double sum = 0.0;
  184. for (int i=0; i<layers[layer-1].getSize(); i++)
  185. {
  186. sum += layers[layer-1].getNodeAt(i).getValue()*getWeightFrom(layer-1, i, node);
  187. }
  188. sum += layers[layer].getNodeAt(node).getBias();
  189. return sigmoid(sum);
  190. }
  191.  
  192. public void forward()
  193. {
  194. NeuralNode node;
  195. for(int L=1; L<layers.length; L++)
  196. {
  197. for (int n=0; n<layers[L].getSize(); n++)
  198. {
  199. node = layers[L].getNodeAt(n);
  200. node.setValue(calcNode(L, n));
  201. }
  202. }
  203. }
  204.  
  205. public void calcOutputDelta() //sets the deltas for each node in output layer
  206. {
  207. int t = 0;
  208. double val;
  209. NeuralNode[] outputs = layers[layers.length-1].getNodes();
  210. for(NeuralNode output: outputs)
  211. {
  212. val = output.getValue();
  213. output.setDelta(val*(1-val)*(targets[t]-val));
  214. t++;
  215. }
  216. }
  217.  
  218. public void updateWeights(int layer) //updates the weights for the layer sending out those weights
  219. {
  220. NeuralLayer nextLayer = layers[layer+1];
  221. double[] weights = new double[nextLayer.getSize()];
  222. double nextNodeDelta; //delta of next layer's node; output is of current layer
  223. double weightDiff; //change in weight
  224. NeuralLayer L = layers[layer];
  225. for (int n=0; n<L.getSize(); n++)
  226. {
  227. for (int w=0; w<nextLayer.getSize(); w++)
  228. {
  229. nextNodeDelta = nextLayer.getNodeAt(w).getDelta();
  230. weightDiff = (-1*trainingRate)*nextNodeDelta*L.getNodeAt(n).getValue();
  231. weights[w] = L.getNodeAt(n).getWeightTo(w) + weightDiff;
  232. }
  233. L.getNodeAt(n).setWeights(weights);
  234. }
  235. }
  236.  
  237. public void updateBias(int layer) //updates the bias's for any layer except the input layer
  238. {
  239. NeuralNode node;
  240. double newBias;
  241. for (int n=0; n<layers[layer].getSize(); n++)
  242. {
  243. node = layers[layer].getNodeAt(n);
  244. newBias = (-1*trainingRate)*node.getDelta();
  245. node.setBias(newBias);
  246. }
  247. }
  248.  
  249. public void calcLayerDelta(int L) //caluculates the delta value for nodes of a layer
  250. {
  251. double summation = 0.0;
  252. NeuralNode node;
  253. for(int n=0; n<layers[L].getSize(); n++)
  254. {
  255. node = layers[L].getNodeAt(n);
  256. for(int m=0; m<layers[L+1].getSize(); m++)
  257. summation += (layers[L+1].getNodeAt(m).getDelta()*getWeightFrom(L, n, m));
  258. node.setDelta(node.getValue()*(1-node.getValue())*summation);
  259. }
  260. }
  261.  
  262. public void runAll(int numTimes) //number of iterations for running the network
  263. {
  264. initializeRandomWeights();
  265. int count=0;
  266. trainingRate = .9;
  267.  
  268. while (count < numTimes)
  269. {
  270. forward();
  271. calcOutputDelta();
  272. System.out.println("outputs: " + layers[layers.length-1].toString());
  273. for (int L=layers.length-1; L>0; L--)
  274. {
  275. updateWeights(L-1);
  276. updateBias(L);
  277. calcLayerDelta(L-1);
  278. }
  279. count++;
  280. //trainingRate *= .9999;
  281. }
  282. }
  283.  
  284. public static void main(String[] args)
  285. {
  286. int[] design = new int[] {1, 3, 1}; //2 nodes in input layer, 3 in hidden, and 2 in output
  287.  
  288. NeuralLayer input = new NeuralLayer(1);
  289. input.setNodeAt(0, new NeuralNode(2.0));
  290. //input.setNodeAt(1, new NeuralNode(1.0));
  291. System.out.println("inputs: " + input.toString());
  292.  
  293. double[] target = new double[]{4.0};
  294. String t = "";
  295. for (int i=0; i<target.length; i++)
  296. t+=target[i] + " ";
  297. System.out.println("targets: " + t);
  298.  
  299. NeuralNetwork net = new NeuralNetwork(input, target, design);
  300. net.runAll(20);
  301.  
  302. //System.out.println("outputs: " + layers[layers.length-1].toString());
  303. }
  304. }
Add Comment
Please, Sign In to add comment