我的项目编辑中有NaN错误

时间:2016-11-14 11:27:59

标签: java c

  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
    

    import java.util.ArrayList;     import java.util.List;     import 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");
    
            }
    
    
    
        }
    

    更新sigma

        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;
    
        }
    
    
    
    
    }
    

    寻求帮助。感谢

1 个答案:

答案 0 :(得分:2)

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

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

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

NaN IEE 754 定义,代表双倍,实际上并非数值。

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