如何在java的neuron类中实现可修改的激活函数?

如何在java的neuron类中实现可修改的激活函数?,java,neural-network,activation-function,Java,Neural Network,Activation Function,我正在学习神经网络的概念。我决定试着自己制作神经元课程。在我的代码中实现不同激活函数的最佳方法是什么?现在它只使用二进制步长函数。 这是我第一次尝试编码神经网络,所以如果你对我的代码有任何建议,或者它是完全愚蠢的,请让我知道 这是我的密码: public class Neuron { // properties private ArrayList<Neuron> input; private ArrayList<Float> weight; pr

我正在学习神经网络的概念。我决定试着自己制作神经元课程。在我的代码中实现不同激活函数的最佳方法是什么?现在它只使用二进制步长函数。 这是我第一次尝试编码神经网络,所以如果你对我的代码有任何建议,或者它是完全愚蠢的,请让我知道

这是我的密码:

public class Neuron {

// properties
    private ArrayList<Neuron> input;
    private ArrayList<Float> weight;
    private float pot, bias, sense, out;
    private boolean checked;

// methods
    public float fire(){
        pot = 0f;
        if (input != null) {
            for (Neuron n : input){
                if (!n.getChecked()){
                    pot += n.fire()*weight.get(input.indexOf(n));
                } else {
                        pot += n.getOut()*weight.get(input.indexOf(n));
                } // end of condition (checked)
            } // end of loop (for input)
        } // end of condition (input exists)
        checked = true;
        pot -= bias;
        pot += sense;
        out = actFunc(pot);
        return out;
    } // end of fire()

    // getting properties
    public float getPot(){return pot;}
    public boolean getChecked(){return checked;}
    public float getOut(){return out;}

    // setting properties
    public void stimulate(float f){sense = f;}
    public void setBias(float b){bias = b;}
    public void setChecked(boolean c){checked = c;}
    public void setOut(float o){out = o;}

    // connection
    public void connect(Neuron n, float w){
        input.add(n);
        weight.add(w);
        }
    public void deconnect(Neuron n){
        weight.remove(input.indexOf(n));
        input.remove(n);
    }

    // activation function
        private float actFunc(float x){
            if (x < 0) {
                return 0f;
            } else {
                return 1f;
            }
        }

// constructor
    public Neuron(Neuron[] ns, float[] ws, float b, float o){
        if (ns != null){
            input = new ArrayList<Neuron>();
            weight = new ArrayList<Float>();
            for (Neuron n : ns) input.add(n);
            for (int i = 0; i < ws.length; i++) weight.add(ws[i]);
        } else {
            input = null;
            weight = null;
        }
        bias = b;
        out = o;
    }

    public Neuron(Neuron[] ns){
        if (ns != null){
            input = new ArrayList<Neuron>();
            weight = new ArrayList<Float>();
            for (Neuron n : ns) input.add(n);
            for (int i = 0; i < input.size(); i++) weight.add((float)Math.random()*2f-1f);
        } else {
            input = null;
            weight = null;
        }
        bias = (float)Math.random();
        out = (float)Math.random();
    }
公共类神经元{
//性质
私有数组列表输入;
私有数组列表权重;
私浮壶,偏颇,理智,出局;
私有布尔检查;
//方法
公众浮火{
pot=0f;
如果(输入!=null){
用于(神经元n:输入){
如果(!n.getChecked()){
pot+=n.fire()*weight.get(input.indexOf(n));
}否则{
pot+=n.getOut()*weight.get(input.indexOf(n));
}//条件结束(已选中)
}//循环结束(用于输入)
}//条件结束(输入存在)
选中=正确;
pot-=偏差;
锅+=感觉;
out=actFunc(pot);
返回;
}//火灾结束()
//获取属性
公共浮点getPot(){return pot;}
公共布尔getChecked(){return checked;}
public float getOut(){return out;}
//设置属性
公共空间(float f){sense=f;}
公共无效设置(浮动b){bias=b;}
公共void setChecked(布尔c){checked=c;}
公共无效放样(浮点数o){out=o;}
//联系
公共空连接(神经元n,浮子w){
输入。添加(n);
增加重量(w);
}
公共无效解除连接(n){
权重。移除(输入索引(n));
输入。删除(n);
}
//激活函数
专用浮点actFunc(浮点x){
if(x<0){
返回0f;
}否则{
返回1f;
}
}
//建造师
公共神经元(神经元[]ns,浮点[]ws,浮点b,浮点o){
如果(ns!=null){
输入=新的ArrayList();
权重=新的ArrayList();
对于(神经元n:ns)输入,添加(n);
对于(inti=0;i

}首先,定义任何激活功能的接口:

public interface ActivationFunction {
    float get(float f);
}
然后编写一些实现:

public class StepFunction implements ActivationFunction {
    @Override
    public float get() {return (x < 0) ? 0f : 1f;}
}

public class SigmoidFunction implements ActivationFunction {
    @Override
    public float get() {return StrictMath.tanh(h);}
}
详情如下:

Neuron n = new Neuron(new SigmoidFunction());
注意,neural netoworks使用通过神经元的信号传播,在神经元中产生权重。权重的计算还取决于激活函数的一阶导数。因此,我将通过方法扩展
ActivationFunction
,该方法将在指定点x返回一阶导数:

public interface ActivationFunction {
    float get(float f);
    float firstDerivative(float x);
}
因此,实现将如下所示:

public class StepFunction implements ActivationFunction {
    @Override
    public float get(float x) {return (x < 0) ? 0f : 1f;}

    @Override
    public float firstDerivative(float x) {return 1;}
}

public class SigmoidFunction implements ActivationFunction {
    @Override
    public float get(float x) {return StrictMath.tanh(x);}

    // derivative_of tanh(x) = (4*e^(2x))/(e^(2x) + 1)^2 == 1-tanh(x)^2 
    @Override
    public float firstDerivative(float x) {return 1 - Math.pow(StrictMath.tanh(x), 2);}
}
public类StepFunction实现ActivationFunction{
@凌驾
公共浮点get(float x){return(x<0)?0f:1f;}
@凌驾
公共浮点一阶导数(浮点x){return 1;}
}
公共类SigmoidFunction实现ActivationFunction{
@凌驾
公共浮点get(float x){return StrictMath.tanh(x);}
//tanh(x)=(4*e^(2x))/(e^(2x)+1的导数^2==1-tanh(x)^2
@凌驾
公共浮点一阶导数(float x){return 1-Math.pow(StrictMath.tanh(x),2);}
}

然后,使用
actFunction.一阶导数(x)
fire()
方法中计算重量。

谢谢!我想到了类似的方法,但我想知道是否有更简单的解决方案。@Turtletston请参见编辑,也许它会对您有所帮助。
public class StepFunction implements ActivationFunction {
    @Override
    public float get(float x) {return (x < 0) ? 0f : 1f;}

    @Override
    public float firstDerivative(float x) {return 1;}
}

public class SigmoidFunction implements ActivationFunction {
    @Override
    public float get(float x) {return StrictMath.tanh(x);}

    // derivative_of tanh(x) = (4*e^(2x))/(e^(2x) + 1)^2 == 1-tanh(x)^2 
    @Override
    public float firstDerivative(float x) {return 1 - Math.pow(StrictMath.tanh(x), 2);}
}