1、import java.io.*;public class NeuralNetwork public static final double learningRate = 0.001; private int numInputs; private int numOutputs; private double inputs; private double outputs; private double weights; public NeuralNetwork(int numInputs, int numOutputs) this.numInputs = numInputs + 1; this.
2、numInputs = numInputs; this.numOutputs = numOutputs; initialize(); private void initialize() inputs = new doublenumInputs; outputs = new doublenumOutputs; weights = new doublenumOutputsnumInputs; for (int i = 0; i numInputs; i+) inputsi = 0.0; for (int i = 0; i numOutputs; i+) outputsi = 0.0; for (i
3、nt i = 0; i numOutputs; i+) for (int j = 0; j numInputs; j+) weightsij = 0; /初始化输入,输出,权重都为0,权重的行为输出数组的个数,列位输入数组的个数 / public void activate(double values) activateInputs(values); activateOutputs(); /我理解为动态更改一个数组。不太明白做什么用的/这个动态改数组方法有两个函数,一个赋值给inputs数组(用传进来的values数组) /另一个函数就是将weights的一行与inputs加,赋值给outpu
4、ts数组的对应单元 /行号,与outputs单元号对应,inputs则是整个想加到weights对应的行 public void activateInputs(double values) inputsnumInputs - 1 = 0.1; for (int i = 0; i numInputs - 1; i+) inputsi = valuesi; /将values数组的值一次赋给inputs数组,除了inputs数组的最后一个元素,最后为0.1 public void activateOutputs() for (int i = 0; i numOutputs; i+) outputsi
5、 = summation(weightsi, inputs); /将输出数组outputs的每个值赋值为summation看下边的代码/这里是将权重weights二维数组的i行与inputs数组的值都加都sum上然后返回 private double summation(double weights, double inputs) double sum = 0.0; for (int i = 0; i numInputs; i+) sum += weightsi + inputsi; return sum; /求和,将两个参数数组各值求和 public double getOutput(int
6、 outputIndex) return outputsoutputIndex; /输出outputs数组的下标为outputIndex的值 public double getMaximumOutput() double maximum = Double.NEGATIVE_INFINITY; double output; for (int i = 0; i maximum) maximum = output; return maximum; /应该是得到outputs数组中的最倒置 public int getMaximumOutputIndex() double maximum = Doub
7、le.NEGATIVE_INFINITY; double output; int outputIndex = 0; for (int i = 0; i maximum) maximum = output; outputIndex = i; return outputIndex; /得到outputs数组中最大元素的下标 public void update(int outputIndex, double inputs, double target) activate(inputs);/用这个inputs赋值给inputs,并把outputs也更新,具体看上边的activate updateWe
8、ights(outputIndex, target);/更新权重数组 private void updateWeights(int outputIndex, double target) double error = target - outputsoutputIndex; System.out.println(Error: + error); for (int i = 0; i numInputs; i+) weightsoutputIndexi += learningRate * inputsi * error; /error为误差值,是目标值减去下表为outputIndex的output
9、s数组元素的值 / 把权重数组对应行更新学习率乘以输入乘以误差 public void loadData(File file) BufferedReader r = null; try r = new BufferedReader(new FileReader(file); for (int i = 0; i numOutputs; i+) for (int j = 0; j numInputs; j+) weightsij = Double.parseDouble(r.readLine(); /你妹的好像是重一个文件中读取数据到weights数组中,应该就是权重数组载入 catch (IOE
10、xception e) System.out.println(IOException trying to open reader: + e); for (int i = 0; i numOutputs; i+) for (int j = 0; j numInputs; j+) weightsij = 0.0; catch (NumberFormatException e) for (int i = 0; i numOutputs; i+) for (int j = 0; j numInputs; j+) weightsij = 0.0; finally try if (r != null) r
11、.close(); catch (IOException e) System.out.println(IOException trying to close reader: + e); /各种异常处理 public void saveData(File file) PrintStream w = null; try w = new PrintStream(new FileOutputStream(file); for (int i = 0; i numOutputs; i+) for (int j = 0; j numInputs; j+) w.println(weightsij); if (
12、w.checkError() System.out.println(I could not write the count!); w.close(); catch (IOException e) System.out.println(IOException trying to write: + e); finally try if (w != null) w.close(); catch (Exception e) System.out.println(Exception trying to close witer: + e); /应该是把权重数组保存。没仔细看 public int getN
13、umOutputs() return numOutputs; /得到输出数组的元素个数 public int getNumInputs() return numInputs; /输入数组元素个数 public void setWeight(int outputIndex, int inputIndex, double value) weightsoutputIndexinputIndex = value; /设置权重数组某个单元的值 public static void main(String args) NeuralNetwork neuralNet = new NeuralNetwork(
14、2, 1);/输入为2,输出为1 /创建一个nn类, for (int i = 0; i 1000 ; i+) neuralNet.update(0, new doublei, i, i + i + 100); /inputIndex=0, 输入数组为i,i,目标值为i+i+100 System.out.println(i + + + i + = + neuralNet.getOutput(0); /你妹的这是循环1000,每次调用nNet类调用update neuralNet.activate(new double50.0, 50.0);/用50,50这个数组去 /更新inputs,并且用输入和权重更新output System.out.println(50 + 50 = + neuralNet.getOutput(0);/输出 /下标为0的输出数组output的值,也就是outputs数组的值,因为outputs这里只有一个元素 System.out.println(Error越来越小说明两个输入值经过神经网络之后相加越来越接近实际值! + 你妹的看了一下午,基本把你丫的看明白了。);