简体   繁体   English

神经网络最小化 Function

[英]Neural Network Minimize Function

I created a simple neural.network;我创建了一个简单的神经网络; in order to actually train it, I would need to know in which direction the weights and biases need to be tweaked.为了实际训练它,我需要知道需要在哪个方向调整权重和偏差。 I've read some articles on the topic, but I'm not exactly great at math and the only thing I understood was that the cost functions (which I managed to get working) need to be minimized.我读过一些关于这个主题的文章,但我并不擅长数学,我唯一理解的是成本函数(我设法开始工作)需要最小化。 It would be great if someone could at least tell me in theory how this works.如果有人至少可以从理论上告诉我这是如何工作的,那就太好了。 If required, I could also post more of the code.如果需要,我还可以发布更多代码。 The minimize function should in the end replace evolve() :最小化 function 最终应该替换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))));
  }
}

Cost function is basically a function of the difference between the real datapoints and your predictions (ie it's your penalty).成本 function 基本上是实际数据点与您的预测之间的差异的 function(即,这是您的惩罚)。 Say for argument's sake, your neural.network is f(x) = 2x + 1 .为了争论起见,你的 neural.network 是f(x) = 2x + 1 Now, say your observed real datapoint is x = 1 , y = 4 .现在,假设您观察到的真实数据点是x = 1y = 4 Therefore your prediction ( f(1) ) is 3 .因此,您的预测 ( f(1) ) 是3

If your cost function is the absolute difference between actual observed value and prediction ie |f(x) - y|如果你的成本 function 是实际观测值和预测值之间的绝对差即|f(x) - y| the value of your cost function is 1 (for x = 1 ) and you would need to minimize this cost function. However, if your cost function is 100 - |f(x) - y|您的成本 function 的值为1 (对于x = 1 ),您需要最小化此成本 function。但是,如果您的成本 function 为100 - |f(x) - y| you would want to maximize it.你会想要最大化它。 In this cost function your maximum reward is 100 .在此成本 function 中,您的最大奖励是100

So your weights and bias need to move in the direction that would get you closer to minimizing your penalty and maximizing your reward.因此,您的权重和偏差需要朝着使您更接近最小化惩罚和最大化奖励的方向移动。 The closer your prediction is to the observed dataset value, the higher the reward and smaller the penalty.您的预测越接近观察到的数据集值,奖励越高,惩罚越小。

Notes :注意事项

  1. This is a gross oversimplification of the math involved but it should help you get started.这是对所涉及数学的严重过度简化,但它应该可以帮助您入门。 Also read about overfitting in machine learning.另请阅读机器学习中的过度拟合

  2. For understanding machine learning theory Cross Validated would be better forum.对于理解机器学习理论, Cross Validated将是更好的论坛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM