day76

package machinelearning.ann;

import java.util.Arrays;

/**
 * ******************************************
 *
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-08-08
 * ******************************************
 */
public class FullAnn extends GeneralAnn {

    /**
     * The layers.
     */
    AnnLayer[] layers;

    /**
     ********************
     * The first constructor.
     *
     * @param paraFilename
     *            The arff filename.
     * @param paraLayerNumNodes
     *            The number of nodes for each layer (may be different).
     * @param paraLearningRate
     *            Learning rate.
     * @param paraMobp
     *            Momentum coefficient.
     * @param paraActivators The storing the activators of each layer.
     ********************
     */
    public FullAnn(String paraFilename, int[] paraLayerNumNodes, double paraLearningRate,
                   double paraMobp, String paraActivators) {
        super(paraFilename, paraLayerNumNodes, paraLearningRate, paraMobp);

        // Initialize layers.
        layers = new AnnLayer[numLayers - 1];
        for (int i = 0; i < layers.length; i++) {
            layers[i] = new AnnLayer(layerNumNodes[i], layerNumNodes[i + 1], paraActivators.charAt(i), paraLearningRate,
                    paraMobp);
        }//of for i
    }//of the first constructor

    /**
     ********************
     * Forward prediction. This is just a stub and should be overwritten in the subclass.
     *
     * @param paraInput
     *            The input data of one instance.
     * @return The data at the output end.
     ********************
     */
    public double[] forward(double[] paraInput) {
        double[] resultArray = paraInput;
        for(int i = 0; i < numLayers - 1; i ++) {
            resultArray = layers[i].forward(resultArray);
        }//of for i

        return resultArray;
    }//of forward

    /**
     ********************
     * Back propagation. This is just a stub and should be overwritten in the subclass.
     *
     * @param paraTarget
     *            For 3-class data, it is [0, 0, 1], [0, 1, 0] or [1, 0, 0].
     *
     ********************
     */
    public void backPropagation(double[] paraTarget) {
        double[] tempErrors = layers[numLayers - 2].getLastLayerErrors(paraTarget);
        for (int i = numLayers - 2; i >= 0; i--) {
            tempErrors = layers[i].backPropagation(tempErrors);
        }//of for i
    }//of backPropagation

    /**
     ********************
     * Show me.
     ********************
     */
    public String toString() {
        String resultString = "I am a full ANN with " + numLayers + " layers";
        return resultString;
    }//of toString

    /**
     ********************
     * Test the algorithm.
     ********************
     */
    public static void main(String[] args) {
        int[] tempLayerNodes = { 4, 8, 8, 3 };
        FullAnn tempNetwork = new FullAnn("D:/mitchelles/data/iris.arff", tempLayerNodes, 0.01,
                0.6, "sss");

        for (int round = 0; round < 5000; round++) {
            tempNetwork.train();
        }//of for n

        double tempAccuracy = tempNetwork.test();
        System.out.println("The accuracy is: " + tempAccuracy);
        System.out.println("FullAnn ends.");
    }//of main
}//of class FullAnn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值