资源描述
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.numInputs = numInputs;
this.numOutputs = numOutputs;
initialize();
}
private void initialize()
{
inputs = new double[numInputs];
outputs = new double[numOutputs];
weights = new double[numOutputs][numInputs];
for (int i = 0; i < numInputs; i++)
inputs[i] = 0.0;
for (int i = 0; i < numOutputs; i++)
outputs[i] = 0.0;
for (int i = 0; i < numOutputs; i++)
for (int j = 0; j < numInputs; j++)
weights[i][j] = 0;
}//初始化输入,输出,权重都为0,权重的行为输出数组的个数,列位输入数组的个数
//
public void activate(double[] values)
{
activateInputs(values);
activateOutputs();
}//我理解为动态更改一个数组。。不太明白做什么用的
//这个动态改数组方法有两个函数,一个赋值给inputs数组(用传进来的values数组)
//另一个函数就是将weights的一行与inputs加,赋值给outputs数组的对应单元
//行号,与outputs单元号对应,inputs则是整个想加到weights对应的行
public void activateInputs(double[] values)
{
inputs[numInputs - 1] = 0.1;
for (int i = 0; i < numInputs - 1; i++)
inputs[i] = values[i];
}//将values数组的值一次赋给inputs数组,除了inputs数组的最后一个元素,最后为0.1
public void activateOutputs()
{
for (int i = 0; i < numOutputs; i++)
outputs[i] = summation(weights[i], 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 += weights[i] + inputs[i];
return sum;
}//求和,将两个参数数组各值求和
public double getOutput(int outputIndex)
{
return outputs[outputIndex];
}//输出outputs数组的下标为outputIndex的值
public double getMaximumOutput()
{
double maximum = Double.NEGATIVE_INFINITY;
double output;
for (int i = 0; i < numOutputs; i++)
{
output = outputs[i];
if (output > maximum)
maximum = output;
}
return maximum;
}//应该是得到outputs数组中的最倒置
public int getMaximumOutputIndex()
{
double maximum = Double.NEGATIVE_INFINITY;
double output;
int outputIndex = 0;
for (int i = 0; i < numOutputs; i++)
{
output = outputs[i];
if (output > maximum)
{
maximum = output;
outputIndex = i;
}
}
return outputIndex;
}//得到outputs数组中最大元素的下标
public void update(int outputIndex, double[] inputs, double target)
{
activate(inputs);//用这个inputs赋值给inputs,并把outputs也更新,具体看上边的activate
updateWeights(outputIndex, target);//更新权重数组
}
private void updateWeights(int outputIndex, double target)
{
double error = target - outputs[outputIndex];
System.out.println("Error: " + error);
for (int i = 0; i < numInputs; i++)
weights[outputIndex][i] += learningRate * inputs[i] * error;
}
//error为误差值,是目标值减去下表为outputIndex的outputs数组元素的值
// 把权重数组对应行更新学习率乘以输入乘以误差
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++)
weights[i][j] = Double.parseDouble(r.readLine());
}//你妹的好像是重一个文件中读取数据到weights数组中,应该就是权重数组载入
catch (IOException e)
{
System.out.println("IOException trying to open reader: " + e);
for (int i = 0; i < numOutputs; i++)
for (int j = 0; j < numInputs; j++)
weights[i][j] = 0.0;
}
catch (NumberFormatException e)
{
for (int i = 0; i < numOutputs; i++)
for (int j = 0; j < numInputs; j++)
weights[i][j] = 0.0;
}
finally
{
try
{
if (r != null)
r.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(weights[i][j]);
if (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 getNumOutputs()
{
return numOutputs;
}//得到输出数组的元素个数
public int getNumInputs()
{
return numInputs;
}//输入数组元素个数
public void setWeight(int outputIndex, int inputIndex, double value)
{
weights[outputIndex][inputIndex] = value;
}//设置权重数组某个单元的值
public static void main(String[] args)
{
NeuralNetwork neuralNet = new NeuralNetwork(2, 1);//输入为2,输出为1
//创建一个nn类,
for (int i = 0; i < 1000 ; i++)
{
neuralNet.update(0, new double[]{i, 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 double[]{50.0, 50.0});//用{50,50}这个数组去
//更新inputs,并且用输入和权重更新output
System.out.println("50 + 50 = " + neuralNet.getOutput(0));//输出
//下标为0的输出数组output的值,也就是outputs数组的值,因为outputs这里只有一个元素
System.out.println("Error越来越小说明两个输入值经过神经网络之后相加越来越接近实际值!" +
"你妹的看了一下午,基本把你丫的看明白了。。。");
}
}
展开阅读全文