gpt4 book ai didi

java - 使用感知器 1 层进行错误学习

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:34 24 4
gpt4 key购买 nike

我需要有关感知器 1 层的帮助,我使用函数 sigmoide 来传递函数,并使用算法反向传播来进行学习。我想做一个简单的神经网络来计算 A 和 B(逻辑与)。我的问题是在学习之后我输入了 2 个值(例如 0 和 0),而我的 IA 总是给我 0.99。我看了代码 3 次,我不明白为什么我的程序在学习后返回错误的答案。请帮助我。

神经元.java:

public class Neuron {
public double value;
public double[] weights;
public double bias;
public double deltas;

public Neuron(int nb_entree){
weights = new double[nb_entree];

value = Math.random() / 10000000000000.0;
bias = Math.random() / 10000000000000.0;
deltas = Math.random() / 10000000000000.0;

for(int i = 0 ; i < weights.length ; i++){
weights[i] = Math.random() / 10000000000000.0;
}
}

/***
* Function to evaluate a neurone with a sigmoide function
* @param input : list to input value
* @return the result of sigmoide function
*/
public double evaluate(double[] input){
double x = 0.0;

for(int i = 0 ; i < input.length ; i++){
x += input[i] * weights[i];
}
x += bias;

value = 1 / (1 + Math.pow(Math.E, x));

return value;
}

//Function to delete value of neurons
protected void delete(){
value = 0.0;
}
}

神经网络.java:

public class NeuralNetwork {
public Neuron[] neurons_hidden;
public Neuron[] neurons_output;
public double rate_learning;
public int nb_hidden;
public int nb_output;

public NeuralNetwork(int nb_input, int nb_hid, int nb_out, double rate){
nb_hidden = nb_hid;
nb_output = nb_out;
rate_learning = rate;

neurons_hidden = new Neuron[nb_hidden];
neurons_output = new Neuron[nb_output];

//Create hidden neurons
for(int i = 0 ; i < nb_hidden ; i++){
neurons_hidden[i] = new Neuron(nb_input);
}

//Create output neurons
for(int i = 0 ; i < nb_output ; i++){
neurons_output[i] = new Neuron(nb_hidden);
}
}

public double[] evaluate(double[] input){
double[] output_hidden = new double[nb_hidden];
double[] outputs = new double[nb_output];

//we delete the value of hidden neurons
for(Neuron n : neurons_hidden){
n.delete();
}

//we delete the value of output neurons
for(Neuron n : neurons_output){
n.delete();
}

//Pour chaque neurone caches
for(int i = 0 ; i < nb_hidden ; i++){
output_hidden[i] = neurons_hidden[i].evaluate(input);
}

//Pour chaque neurone sortie
for(int i = 0 ; i < nb_output ; i++){
outputs[i] = neurons_output[i].evaluate(output_hidden);
}

return outputs;
}


public double backPropagate(double[] input, double[] output){

double[] output_o = evaluate(input);
double error;
int i;
int k;

//For all neurons output, we compute the deltas
for(i = 0 ; i < nb_output ; i++){
error = output[i] - output_o[i];
neurons_output[i].deltas = error * (output_o[i] - Math.pow(output_o[i], 2));
}

//For all neurons hidden, we compute the deltas
for(i = 0 ; i < nb_hidden ; i++){
error = 0.0;
for(k = 0 ; k < nb_output ; k++){
error += neurons_output[k].deltas * neurons_output[k].weights[i];
}
neurons_hidden[i].deltas = error * (neurons_hidden[i].value - Math.pow(neurons_hidden[i].value, 2));
}


//For all neurons output, we modify the weight
for(i = 0 ; i < nb_output ; i++){
for(k = 0 ; k < nb_hidden ; k++){
neurons_output[i].weights[k] += rate_learning *
neurons_output[i].deltas *
neurons_hidden[k].value;
}
neurons_output[i].bias += rate_learning * neurons_output[i].deltas;
}


//For all neurons hidden, we modify the weight
for(i = 0 ; i < nb_hidden ; i++){
for(k = 0 ; k < input.length ; k++){
neurons_hidden[i].weights[k] += rate_learning * neurons_hidden[i].deltas * input[k];
}
neurons_hidden[i].bias += rate_learning * neurons_hidden[i].deltas;
}

error = 0.0;
for(i = 0 ; i < output.length ; i++){
error += Math.abs(output_o[i] - output[i]);
}

error = error / output.length;

return error;
}
}

测试.java:

public class Test {

public static void main(String[] args) {

NeuralNetwork net = new NeuralNetwork(2, 2, 1, 0.6);

/* Learning */
for(int i = 0 ; i < 10000 ; i++)
{
double[] inputs = new double[]{Math.round(Math.random()), Math.round(Math.random())};
double[] output = new double[1];
double error;

if((inputs[0] == inputs[1]) && (inputs[0] == 1))
output[0] = 1.0;
else
output[0] = 0.0;

System.out.println(inputs[0]+" and "+inputs[1]+" = "+output[0]);

error = net.backPropagate(inputs, output);
System.out.println("Error at step "+i+" is "+error);
}

System.out.println("Learning finish!");

/* Test */
double[] inputs = new double[]{0.0, 0.0};
double[] output = net.evaluate(inputs);

System.out.println(inputs[0]+" and "+inputs[1]+" = "+output[0]+"");
}
}

谢谢你帮助我

最佳答案

您的 sigmoid 函数不正确。它需要一个负 t:

1 / (1 + Math.pow(Math.E, -x))

我不确定这是否是唯一的错误。

此外,对于连接词“and”,您只需要一层。

最后,您可以在反向传播方法中单独处理偏差。这可以通过添加一个输入节点来简化,其中输入节点为常数 1 作为输入,偏差作为权重。请参阅https://en.wikipedia.org/wiki/Perceptron#Definitions .

关于java - 使用感知器 1 层进行错误学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39811517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com