JavaDay28

学习来源:日撸 Java 三百行(81-90天,CNN 卷积神经网络)_闵帆的博客——CSDN博客

网络结构与参数

设定网络框架,提供基础的功能函数。

一、单层CNN结构

1. CNN单层类,主要涉及各层的初始化以及属性的getter和setter方法。

2. 属性type表示这层的类型,如输入、卷积、池化、输出层。

3. 创建单层对象时,构造函数有paraType、paraNum、paraSize三个参数,根据paraType的值来判断其余两个参数的意义。例如当paraType等于CONVOLUTION时,表示当前层为卷积层,那么paraNum表示卷积核的个数,也是卷积层输出的图片个数。paraSize则表示卷积核的大小。

代码如下:

package JavaDay28;

/**
 * One layer, support all four layer types. The code mainly initializes, gets,
 * and sets variables. Essentially no algorithm is implemented.
 *
 * @author Ke-Xiong Wang.
 */
public class CnnLayer {
	/**
	 * The type of the layer.
	 */
	LayerTypeEnum type;

	/**
	 * The number of out map.
	 */
	int outMapNum;

	/**
	 * The map size.
	 */
	Size mapSize;

	/**
	 * The kernel size.
	 */
	Size kernelSize;

	/**
	 * The scale size.
	 */
	Size scaleSize;

	/**
	 * The index of the class (label) attribute.
	 */
	int classNum = -1;

	/**
	 * Kernel. Dimensions: [front map][out map][width][height].
	 */
	private double[][][][] kernel;

	/**
	 * Bias. The length is outMapNum.
	 */
	private double[] bias;

	/**
	 * Out maps. Dimensions:
	 * [batchSize][outMapNum][mapSize.width][mapSize.height].
	 */
	private double[][][][] outMaps;

	/**
	 * Errors.
	 */
	private double[][][][] errors;

	/**
	 * For batch processing.
	 */
	private static int recordInBatch = 0;

	/**
	 *********************** 
	 * The first constructor.
	 * 
	 * @param paraNum
	 *            When the type is CONVOLUTION, it is the out map number. when
	 *            the type is OUTPUT, it is the class number.
	 * @param paraSize
	 *            When the type is INPUT, it is the map size; when the type is
	 *            CONVOLUTION, it is the kernel size; when the type is SAMPLING,
	 *            it is the scale size.
	 *********************** 
	 */
	public CnnLayer(LayerTypeEnum paraType, int paraNum, Size paraSize) {
		type = paraType;
		switch (type) {
		case INPUT:
			outMapNum = 1;
			mapSize = paraSize; // No deep copy.
			break;
		case CONVOLUTION:
			outMapNum = paraNum;
			kernelSize = paraSize;
			break;
		case SAMPLING:
			scaleSize = paraSize;
			break;
		case OUTPUT:
			classNum = paraNum;
			mapSize = new Size(1, 1);
			outMapNum = classNum;
			break;
		default:
			System.out.println("Internal error occurred in AbstractLayer.java constructor.");
		}// Of switch
	}// Of the first constructor

	/**
	 *********************** 
	 * Initialize the kernel.
	 * 
	 * @param paraNum
	 *            When the type is CONVOLUTION, it is the out map number. when
	 *********************** 
	 */
	public void initKernel(int paraFrontMapNum) {
		kernel = new double[paraFrontMapNum][outMapNum][][];
		for (int i = 0; i < paraFrontMapNum; i++) {
			for (int j = 0; j < outMapNum; j++) {
				kernel[i][j] = MathUtils.randomMatrix(kernelSize.width, kernelSize.height, true);
			} // Of for j
		} // Of for i
	}// Of initKernel

	/**
	 *********************** 
	 * Initialize the output kernel. The code is revised to invoke
	 * initKernel(int).
	 *********************** 
	 */
	public void initOutputKernel(int paraFrontMapNum, Size paraSize) {
		kernelSize = paraSize;
		initKernel(paraFrontMapNum);
	}// Of initOutputKernel

	/**
	 *********************** 
	 * Initialize the bias. No parameter. "int frontMapNum" is claimed however
	 * not used.
	 *********************** 
	 */
	public void initBias() {
		bias = MathUtils.randomArray(outMapNum);
	}// Of initBias

	/**
	 *********************** 
	 * Initialize the errors.
	 * 
	 * @param paraBatchSize
	 *            The batch size.
	 *********************** 
	 */
	public void initErrors(int paraBatchSize) {
		errors = new double[paraBatchSize][outMapNum][mapSize.width][mapSize.height];
	}// Of initErrors

	/**
	 *********************** 
	 * Initialize out maps.
	 * 
	 * @param paraBatchSize
	 *            The batch size.
	 *********************** 
	 */
	public void initOutMaps(int paraBatchSize) {
		outMaps = new double[paraBatchSize][outMapNum][mapSize.width][mapSize.height];
	}// Of initOutMaps

	/**
	 *********************** 
	 * Prepare for a new batch.
	 *********************** 
	 */
	public static void prepareForNewBatch() {
		recordInBatch = 0;
	}// Of prepareForNewBatch

	/**
	 *********************** 
	 * Prepare for a new record.
	 *********************** 
	 */
	public static void prepareForNewRecord() {
		recordInBatch++;
	}// Of prepareForNewRecord

	/**
	 *********************** 
	 * Set one value of outMaps.
	 *********************** 
	 */
	public void setMapValue(int paraMapNo, int paraX, int paraY, double paraValue) {
		outMaps[recordInBatch][paraMapNo][paraX][paraY] = paraValue;
	}// Of setMapValue

	/**
	 *********************** 
	 * Set values of the whole map.
	 *********************** 
	 */
	public void setMapValue(int paraMapNo, double[][] paraOutMatrix) {
		outMaps[recordInBatch][paraMapNo] = paraOutMatrix;
	}// Of setMapValue

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public Size getMapSize() {
		return mapSize;
	}// Of getMapSize

	/**
	 *********************** 
	 * Setter.
	 *********************** 
	 */
	public void setMapSize(Size paraMapSize) {
		mapSize = paraMapSize;
	}// Of setMapSize

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public LayerTypeEnum getType() {
		return type;
	}// Of getType

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public int getOutMapNum() {
		return outMapNum;
	}// Of getOutMapNum

	/**
	 *********************** 
	 * Setter.
	 *********************** 
	 */
	public void setOutMapNum(int paraOutMapNum) {
		outMapNum = paraOutMapNum;
	}// Of setOutMapNum

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public Size getKernelSize() {
		return kernelSize;
	}// Of getKernelSize

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public Size getScaleSize() {
		return scaleSize;
	}// Of getScaleSize

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double[][] getMap(int paraIndex) {
		return outMaps[recordInBatch][paraIndex];
	}// Of getMap

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double[][] getKernel(int paraFrontMap, int paraOutMap) {
		return kernel[paraFrontMap][paraOutMap];
	}// Of getKernel

	/**
	 *********************** 
	 * Setter. Set one error.
	 *********************** 
	 */
	public void setError(int paraMapNo, int paraMapX, int paraMapY, double paraValue) {
		errors[recordInBatch][paraMapNo][paraMapX][paraMapY] = paraValue;
	}// Of setError

	/**
	 *********************** 
	 * Setter. Set one error matrix.
	 *********************** 
	 */
	public void setError(int paraMapNo, double[][] paraMatrix) {
		errors[recordInBatch][paraMapNo] = paraMatrix;
	}// Of setError

	/**
	 *********************** 
	 * Getter. Get one error matrix.
	 *********************** 
	 */
	public double[][] getError(int paraMapNo) {
		return errors[recordInBatch][paraMapNo];
	}// Of getError

	/**
	 *********************** 
	 * Getter. Get the whole error tensor.
	 *********************** 
	 */
	public double[][][][] getErrors() {
		return errors;
	}// Of getErrors

	/**
	 *********************** 
	 * Setter. Set one kernel.
	 *********************** 
	 */
	public void setKernel(int paraLastMapNo, int paraMapNo, double[][] paraKernel) {
		kernel[paraLastMapNo][paraMapNo] = paraKernel;
	}// Of setKernel

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double getBias(int paraMapNo) {
		return bias[paraMapNo];
	}// Of getBias

	/**
	 *********************** 
	 * Setter.
	 *********************** 
	 */
	public void setBias(int paraMapNo, double paraValue) {
		bias[paraMapNo] = paraValue;
	}// Of setBias

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double[][][][] getMaps() {
		return outMaps;
	}// Of getMaps

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double[][] getError(int paraRecordId, int paraMapNo) {
		return errors[paraRecordId][paraMapNo];
	}// Of getError

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public double[][] getMap(int paraRecordId, int paraMapNo) {
		return outMaps[paraRecordId][paraMapNo];
	}// Of getMap

	/**
	 *********************** 
	 * Getter.
	 *********************** 
	 */
	public int getClassNum() {
		return classNum;
	}// Of getClassNum

	/**
	 *********************** 
	 * Getter. Get the whole kernel tensor.
	 *********************** 
	 */
	public double[][][][] getKernel() {
		return kernel;
	} // Of getKernel
}// Of class CnnLayer

二、多层管理

1. 通过元素为CnnLayer的list来管理CNN的各层。

2. 通过list可以很方便地进行添加、访问各层。

代码如下:

package JavaDay28;

import java.util.ArrayList;
import java.util.List;

/**
 * CnnLayer builder. 
 * 
 * @author Ke-Xiong Wang.
 */
public class LayerBuilder {
	/**
	 * Layers.
	 */
	private List<CnnLayer> layers;

	/**
	 *********************** 
	 * The first constructor.
	 *********************** 
	 */
	public LayerBuilder() {
		layers = new ArrayList<CnnLayer>();
	}// Of the first constructor

	/**
	 *********************** 
	 * The second constructor.
	 *********************** 
	 */
	public LayerBuilder(CnnLayer paraLayer) {
		this();
		layers.add(paraLayer);
	}// Of the second constructor

	/**
	 *********************** 
	 * Add a layer.
	 * 
	 * @param paraLayer
	 *            The new layer.
	 *********************** 
	 */
	public void addLayer(CnnLayer paraLayer) {
		layers.add(paraLayer);
	}// Of addLayer
	
	/**
	 *********************** 
	 * Get the specified layer.
	 * 
	 * @param paraIndex
	 *            The index of the layer.
	 *********************** 
	 */
	public CnnLayer getLayer(int paraIndex) throws RuntimeException{
		if (paraIndex >= layers.size()) {
			throw new RuntimeException("CnnLayer " + paraIndex + " is out of range: "
					+ layers.size() + ".");
		}//Of if
		
		return layers.get(paraIndex);
	}//Of getLayer
	
	/**
	 *********************** 
	 * Get the output layer.
	 *********************** 
	 */
	public CnnLayer getOutputLayer() {
		return layers.get(layers.size() - 1);
	}//Of getOutputLayer

	/**
	 *********************** 
	 * Get the number of layers.
	 *********************** 
	 */
	public int getNumLayers() {
		return layers.size();
	}//Of getNumLayers
}// Of class LayerBuilder

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值