gpt4 book ai didi

java - 我的项目中有 NaN 错误已编辑

转载 作者:太空宇宙 更新时间:2023-11-04 02:33:20 26 4
gpt4 key购买 nike

  1. 完整代码

      the full code of the neuron. am trying to code a radial basis                    neuron network but i can figure out where and getting an NaN error

    导入java.util.ArrayList; 导入java.util.List; 导入 java.util.Random;

    public class Neurons {

    static Random rand =new Random();
    instance variables


    private float m_neuronOutput;
    private float alpha=0.4f;
    private float m_gradient;
    private int neuronIndex;
    public float eta;
    public float sigma=22.5f;
    public List<Connection> neuronWeights= new ArrayList<Connection>();
    //public List<Center> neuronCenter= new ArrayList<Center>();
    NetWork net=new NetWork();
    public Neurons(){
    // sigma=22.5f;
    eta=0.5f;

    }
    //neuron constructor
    public Neurons(int outputNum, int m_neuronIndex){
    neuronIndex=m_neuronIndex;

    add connection weights
    for (int connect = 0; connect < outputNum; connect++) {
    neuronWeights.add(new Connection());


    }


    }

    public void SetOutputValue(float value){
    m_neuronOutput = value;

    }
    public float GetOutputValue(){
    return m_neuronOutput;
    }
    ## feed the layer with the input ##
  2. 给图层喂食

        public void FeedForward(Layer previousLayer, int n){
    float sum = 0;
    //weightsum the inputs of the last layer, including bias
    //System.out.println(n);
    m_neuronOutput= previousLayer.neuron.get(n).GetOutputValue () ;
    //System.out.println(m_neuronOutput);
    }
  3. 设置隐藏层的值

        public void FeedHidden(Layer previousLayer, int j ){

    // for(int i=0;i<center.length;i++)
    // System.out.println(center[i]);
    float distance= 0;
    //System.out.println(n+" "+hiddenLayer.neuron.get(n).neuronCenter.get(n).center);
    //weightsum the inputs of the last layer, including bias
    for (int n = 0; n <previousLayer.neuron.size(); n++) {
    distance += (float) Math.pow(previousLayer.neuron.get(n).GetOutputValue ()-previousLayer.neuronCenter.get(j).center,2);
    //System.out.println(previousLayer.neuronCenter.get(j).center);
    }

    ## where am calling phi(Gaussian function) ## m_neuronOutput = phi (distance);
    //System.out.println(m_neuronOutput);
    }
    private float phi( float distance){

    //System.out.println(sigma);
    return (float) Math.exp(- distance/(2*Math.pow(sigma, 2)));
    }
  4. 计算输出

        public void setOutput(Layer hidden){
    float output=0;
    for(int i=0;i<hidden.neuron.size();i++){
    output+=hidden.neuron.get(i).GetOutputValue()*hidden.neuron.get(i).neuronWeights.get(neuronIndex).weight;
    // System.out.println(hidden.neuron.get(i).GetOutputValue());
    // System.out.println(hidden.neuron.get(i).neuronWeights.get(neuronIndex).weight);
    }
    m_neuronOutput=output;
    //System.out.println(hidden.neuron.get(i).neuronWeights.get(neuronIndex).weight);

    }
  5. 输出层的梯度

        public void CalcOutputGradients( float targetValue,float output){
    // System.out.println(output);
    float delta = targetValue -output;


    m_gradient = delta;
    //System.out.println( output);

    }

    public void UpdateInputWeights(Layer previousLayer,float eta){
    //System.out.println(phi);
    the weights to be updated are in the connection container
    in the neurons in the preceding layer
    int numNeuronas = previousLayer.neuron.size();
    for (int n = 0; n < numNeuronas; n++) {//for every previous layer neuron
    Neurons neu = previousLayer.neuron.get(n);//getting the previous layer neuron
    //System.out.println(neu.neuronCenter.get(neuronIndex).center );
    float phi=previousLayer.neuron.get(n).GetOutputValue ();
    //double oldDeltaW = neu.neuronWeights.get(neuronIndex).deltaweight;
    double deltaWeight=
    //Individual input magnified by the gradient and trainrate
    eta *phi*m_gradient;
    also add momentum = a fraction of the previos delta weight
    +oldDeltaW;
    neu.neuronWeights.get(neuronIndex).deltaweight = deltaWeight;
    neu.neuronWeights.get(neuronIndex).weight+= deltaWeight;

    //System.out.println(m_gradient +" delta");

    }
    }
    ## my method to update the center but yet to call it in my network work class ##
    public void updateCenter( Layer previousLayer,float[] input, float eta){
    int numNeuronas = previousLayer.neuron.size();

    for (int n = 0; n < numNeuronas-1; n++) {//for every previous layer neuron
    Neurons neu = previousLayer.neuron.get(n);//getting the previous layer neuro
    Center neu1 = previousLayer.neuronCenter.get(n);
    float phi=previousLayer.neuron.get(n).GetOutputValue ();
    double oldcenter = neu1.center;

    double weight= neu.neuronWeights.get(neuronIndex).weight;
    double deltaC=
    //Individual input magnified by the gradient and trainrate
    eta *phi*((m_gradient*weight)/Math.pow(sigma, 2))*(input[n]-oldcenter);
    //also add momentum = a fraction of the previos delta weight
    //+oldcenter;


    neu1.center+= deltaC;

    //System.out.println(gredient+" delta");

    }



    }

    更新西格玛

        public void updateSigma(Layer previousLayer, float[] center, float[] input, float eta){
    float distance= 0;
    //weightsum the inputs of the last layer, including bias
    for (int n = 0; n < center.length; n++) {
    distance+= Math.pow(previousLayer.neuron.get(n).GetOutputValue () -center[n],2);
    //System.out.println(distance);
    }
    float phi=(float) phi(distance);
    float sigma1=0;
    for(int i=0;i<center.length;i++){
    double w =previousLayer.neuron.get(i).neuronWeights.get(neuronIndex).weight;
    sigma1=(float) (sigma+(eta*((m_gradient*w)/Math.pow(sigma, 3))*phi*Math.pow(input[i]-center[i],2)));
    }
    sigma=sigma1;
    }
    get the current learning rate
    public float updateLearningRate(float epoch){
    //search the converge algorithm

    return (float) (eta/((epoch)/500));

    }

    set the learning rate
    public void setLearningRate(float rate){

    eta=rate;

    }

    update the learining rate
    public float getLearningRate(){
    return eta;

    }




    }

    寻找对您有帮助的词。谢谢

最佳答案

return (float) Math.exp(- distance/(2*Math.pow(sigma, 2)));

在上一行中,当sigmadistance0时,执行Math.exp(-0/0) NaN

NaN 代表 Not a Number,例如当您除以 0 或取负数的平方根时。

NaNIEE 754 定义,代表一个 double 值,它实际上不是数值。

作为附带说明,我相信您会读到,请始终将堆栈跟踪添加到问题中。始终添加有关问题的所有重要信息。

关于java - 我的项目中有 NaN 错误已编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40587675/

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