day81

本文介绍了数据管理类`Dataset`,用于处理数据集,支持读取文件并构建实例。同时,定义了卷积核尺寸类`Size`,包含基本的除法和减法操作。此外,还枚举了神经网络层类型`LayerTypeEnum`,包括输入、卷积、采样和输出层。
摘要由CSDN通过智能技术生成
package machinelearning.cnn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ******************************************
 * Manage the dataset.
 *
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-08-13
 * ******************************************
 */
public class Dataset {
    /**
     * All instances organized by a list.
     */
    private List<Instance> instances;

    /**
     * The label index.
     */
    private int labelIndex;

    /**
     * The max label (label start from 0).
     */
    private double maxLabel = -1;

    /**
     ***********************
     * The first constructor.
     ***********************
     */
    public Dataset() {
        labelIndex = -1;
        instances = new ArrayList<Instance>();
    }//of the first constructor

    /**
     ***********************
     * The second constructor.
     *
     * @param paraFilename
     *            The filename.
     * @param paraSplitSign
     *            Often comma.
     * @param paraLabelIndex
     *            Often the last column.
     ***********************
     */
    public Dataset(String paraFilename, String paraSplitSign, int paraLabelIndex) {
        instances = new ArrayList<Instance>();
        labelIndex = paraLabelIndex;

        File tempFile = new File(paraFilename);
        try {
            BufferedReader tempReader = new BufferedReader(new FileReader(tempFile));
            String tempLine;
            while ((tempLine = tempReader.readLine()) != null) {
                String[] tempDatum = tempLine.split(paraSplitSign);
                if (tempDatum.length == 0) {
                    continue;
                }//of if

                double[] tempData = new double[tempDatum.length];
                for (int i = 0; i < tempDatum.length; i++)
                    tempData[i] = Double.parseDouble(tempDatum[i]);
                Instance tempInstance = new Instance(tempData);
                append(tempInstance);
            }//of while

            tempReader.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Unable to load " + paraFilename);
            System.exit(0);
        }//of try
    }//of the second constructor

    /**
     ***********************
     * Append an instance.
     *
     * @param paraInstance
     *            The given Instance.
     ***********************
     */
    public void append(Instance paraInstance) {
        instances.add(paraInstance);
    }//of append

    /**
     ********************
     * Append an instance  specified by double values.
     ********************
     */
    public void append(double[] paraAttributes, Double paraLabel) {
        instances.add(new Instance(paraAttributes, paraLabel));
    }//of append

    /**
     ********************
     * Getter.
     ********************
     */
    public Instance getInstance(int paraIndex) {
        return instances.get(paraIndex);
    }//of getInstance

    /**
     ********************
     * Getter.
     ********************
     */
    public int size() {
        return instances.size();
    }//of size

    /**
     ***********************
     * Getter.
     ***********************
     */
    public double[] getAttributes(int paraIndex) {
        return instances.get(paraIndex).getAttributes();
    }//of getAttrs

    /**
     ********************
     * Getter.
     ********************
     */
    public Double getLabel(int paraIndex) {
        return instances.get(paraIndex).getLabel();
    }//of getLabel

    /**
     ********************
     * Unit test.
     ********************
     */
    public static void main(String[] args) {
        Dataset tempData = new Dataset("d:/c/cann/data/mnist/train.format", ",", 784);
        Instance tempInstance = tempData.getInstance(0);
        System.out.println("The first instance is: " + tempInstance);
    }//of main

    /**
     ********************
     * An instance.
     ********************
     */
    public class Instance {
        /**
         * Conditional attributes.
         */
        private double[] attributes;

        /**
         * Label.
         */
        private Double label;

        /**
         ********************
         * The first constructor.
         ********************
         */
        private Instance(double[] paraAttrs, Double paraLabel) {
            attributes = paraAttrs;
            label = paraLabel;
        }//of the first constructor

        /**
         ********************
         * The second constructor.
         ********************
         */
        public Instance(double[] paraData) {
            if (labelIndex == -1)
                // No label.
                attributes = paraData;
            else {
                label = paraData[labelIndex];
                if (label > maxLabel) {
                    // It is a new label.
                    maxLabel = label;
                }//of if

                if (labelIndex == 0) {
                    // The first column is the label.
                    attributes = Arrays.copyOfRange(paraData, 1, paraData.length);
                } else {
                    // The last column is the label.
                    attributes = Arrays.copyOfRange(paraData, 0, paraData.length - 1);
                }//of if
            }//of if
        }//of the second constructor

        /**
         ********************
         * Getter.
         ********************
         */
        public double[] getAttributes() {
            return attributes;
        }//of getAttributes

        /**
         ********************
         * Getter.
         ********************
         */
        public Double getLabel() {
            if (labelIndex == -1)
                return null;
            return label;
        }//of getLabel

        /**
         ********************
         * toString.
         ********************
         */
        public String toString(){
            return Arrays.toString(attributes) + ", " + label;
        }//of toString
    }//of class Instance
}//of class Dataset

package machinelearning.cnn;

/**
 * ******************************************
 * The size of a convolution core.
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-08-13
 * ******************************************
 */
public class Size {
    /**
     * Cannot be changed after initialization.
     */
    public final int width;

    /**
     * Cannot be changed after initialization.
     */
    public final int height;

    /**
     ***********************
     * The first constructor.
     *
     * @param paraWidth
     *            The given width.
     * @param paraHeight
     *            The given height.
     ***********************
     */
    public Size(int paraWidth, int paraHeight) {
        width = paraWidth;
        height = paraHeight;
    }//of the first constructor

    /**
     ***********************
     * Divide a scale with another one. For example (4, 12) / (2, 3) = (2, 4).
     *
     * @param paraScaleSize
     *            The given scale size.
     * @return The new size.
     ***********************
     */
    public Size divide(Size paraScaleSize) {
        int resultWidth = width / paraScaleSize.width;
        int resultHeight = height / paraScaleSize.height;
        if (resultWidth * paraScaleSize.width != width
                || resultHeight * paraScaleSize.height != height)
            throw new RuntimeException("Unable to divide " + this + " with " + paraScaleSize);
        return new Size(resultWidth, resultHeight);
    }//of divide

    /**
     ***********************
     * Subtract a scale with another one, and add a value. For example (4, 12) -
     * (2, 3) + 1 = (3, 10).
     *
     * @param paraScaleSize
     *            The given scale size.
     * @param paraAppend
     *            The appended size to both dimensions.
     * @return The new size.
     ***********************
     */
    public Size subtract(Size paraScaleSize, int paraAppend) {
        int resultWidth = width - paraScaleSize.width + paraAppend;
        int resultHeight = height - paraScaleSize.height + paraAppend;
        return new Size(resultWidth, resultHeight);
    }//of subtract

    /**
     ***********************
     * string showing itself.
     ***********************
     */
    public String toString() {
        String resultString = "(" + width + ", " + height + ")";
        return resultString;
    }//of toString

    /**
     ***********************
     * Unit test.
     ***********************
     */
    public static void main(String[] args) {
        Size tempSize1 = new Size(4, 6);
        Size tempSize2 = new Size(2, 2);
        System.out.println(
                "" + tempSize1 + " divide " + tempSize2 + " = " + tempSize1.divide(tempSize2));

        System.out.printf("a");

        try {
            System.out.println(
                    "" + tempSize2 + " divide " + tempSize1 + " = " + tempSize2.divide(tempSize1));
        } catch (Exception ee) {
            System.out.println(ee);
        }//of try

        System.out.println(
                "" + tempSize1 + " - " + tempSize2 + " + 1 = " + tempSize1.subtract(tempSize2, 1));
    }//of main
}//of class Size
package machinelearning.cnn;

/**
 * ******************************************
 * Enumerate all layer types.
 *
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-08-14
 * ******************************************
 */
public enum LayerTypeEnum {
    INPUT, CONVOLUTION, SAMPLING, OUTPUT;
}//of enum LayerTypeEnum

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值