【发布时间】:2021-05-20 17:56:09
【问题描述】:
我创建了一个简单的神经网络;为了实际训练它,我需要知道需要在哪个方向调整权重和偏差。我已经阅读了一些关于该主题的文章,但我对数学并不十分擅长,我唯一理解的是成本函数(我设法使其工作)需要最小化。如果有人至少能在理论上告诉我这是如何工作的,那就太好了。如果需要,我还可以发布更多代码。最小化函数最终应该替换evolve():
import java.util.Random;
public class Neuron {
Neuron[] input;
float[] weight;
float bias;
Float value = null;
public Neuron(Neuron[] input) {
this.input = input;
weight = new float[input.length];
setRandom();
}
public void setValue(float val) {
this.value = val;
}
public float getValue() {
if(this.value == null) {
return calculate();
}
else {
return this.value;
}
}
private float calculate() {
float res = 0;
for(int i = 0; i < input.length; i++) {
res += input[i].getValue() * weight[i];
}
res -= bias;
return sigmoid(res);
}
private void setRandom() {
Random rand = new Random();
float max = 0;
for(int i = 0; i < weight.length; i++) {
weight[i] = rand.nextFloat();
max += weight[i];
}
this.bias = max * 0.8f - rand.nextFloat();
}
public void evolve() {
Random rand = new Random();
for(int i = 0; i < weight.length; i++) {
weight[i] += rand.nextFloat() - 0.5f;
}
this.bias += rand.nextFloat() - 0.5f;
}
public static float sigmoid(float x) {
return (float)(1/( 1 + Math.pow(Math.E,(-1*(double)x))));
}
}
【问题讨论】:
标签: java math neural-network minimize