栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

机器学习-14-BP神经网络-单层实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

机器学习-14-BP神经网络-单层实现

学习来源:日撸 Java 三百行(71-80天,BP 神经网络)_闵帆的博客-CSDN博客

单层实现

也就是在神经网络中的其中一层进行实现,其中该代码使用的是sigmoid函数作为激活函数。

代码理解

numInput:输入的维度

numOutput:输出的维度

learningRate:学习率

mobp:动量

weights:权值矩阵大小为(输入维度+1)*输出维度。其中加一是偏置(在当前的代码中全零,虽然全零不影响,但是加上是为了严谨)

activator:激活函数类的对象(其中设置为sigmoid函数)

errors:输出误差

input:输入的一维双精度数据

output:输出的一维双精度数据

activatedOutput:激活后的输出的一维双精度数据

代码:

package 日撸Java300行_71_80;


import java.util.Arrays;
import java.util.Random;


public class AnnLayer {

	
	int numInput;

	
	int numOutput;

	
	double learningRate;

	
	double mobp;

	
	double[][] weights;

	
	double[][] deltaWeights;

	
	double[] errors;

	
	double[] input;

	
	double[] output;

	
	double[] activatedOutput;

	
	Activator activator;

	
	Random random = new Random();

	
	public AnnLayer(int paraNumInput, int paraNumOutput, char paraActivator,
			double paraLearningRate, double paraMobp) {
		numInput = paraNumInput;
		numOutput = paraNumOutput;
		learningRate = paraLearningRate;
		mobp = paraMobp;

		weights = new double[numInput + 1][numOutput];
		deltaWeights = new double[numInput + 1][numOutput];
		for (int i = 0; i < numInput + 1; i++) {
			for (int j = 0; j < numOutput; j++) {
				weights[i][j] = random.nextDouble();
			} // Of for j
		} // Of for i

		errors = new double[numInput];

		input = new double[numInput];
		output = new double[numOutput];
		activatedOutput = new double[numOutput];

		activator = new Activator(paraActivator);
	}// Of the first constructor

	
	public void setParameters(double paraAlpha, double paraBeta, double paraGamma) {
		activator.setAlpha(paraAlpha);
		activator.setBeta(paraBeta);
		activator.setGamma(paraGamma);
	}// Of setParameters

	
	public double[] forward(double[] paraInput) {
		//System.out.println("Ann layer forward " + Arrays.toString(paraInput));
		// Copy data.
		for (int i = 0; i < numInput; i++) {
			input[i] = paraInput[i];
		} // Of for i

		// Calculate the weighted sum for each output.
		for (int i = 0; i < numOutput; i++) {
			output[i] = weights[numInput][i];
			for (int j = 0; j < numInput; j++) {
				output[i] += input[j] * weights[j][i];
			} // Of for j

			activatedOutput[i] = activator.activate(output[i]);
		} // Of for i

		return activatedOutput;
	}// Of forward

	
	public double[] backPropagation(double[] paraErrors) {
		//Step 1. Adjust the errors.
		for (int i = 0; i < paraErrors.length; i++) {
			paraErrors[i] = activator.derive(output[i], activatedOutput[i]) * paraErrors[i];
		}//Of for i
		
		//Step 2. Compute current errors.
		for (int i = 0; i < numInput; i++) {
			errors[i] = 0;
			for (int j = 0; j < numOutput; j++) {
				errors[i] += paraErrors[j] * weights[i][j];
				deltaWeights[i][j] = mobp * deltaWeights[i][j]
						+ learningRate * paraErrors[j] * input[i];
				weights[i][j] += deltaWeights[i][j];
			} // Of for j
		} // Of for i

		for (int j = 0; j < numOutput; j++) {
			deltaWeights[numInput][j] = mobp * deltaWeights[numInput][j] + learningRate * paraErrors[j];
			weights[numInput][j] += deltaWeights[numInput][j];
		} // Of for j

		return errors;
	}// Of backPropagation

	
	public double[] getLastLayerErrors(double[] paraTarget) {
		double[] resultErrors = new double[numOutput];
		for (int i = 0; i < numOutput; i++) {
			resultErrors[i] = (paraTarget[i] - activatedOutput[i]);
		} // Of for i

		return resultErrors;
	}// Of getLastLayerErrors

	
	public String toString() {
		String resultString = "";
		resultString += "Activator: " + activator;
		resultString += "rn weights = " + Arrays.deepToString(weights);
		return resultString;
	}// Of toString

	
	public static void unitTest() {
		AnnLayer tempLayer = new AnnLayer(2, 3, 's', 0.01, 0.1);
		double[] tempInput = { 1, 4 };

		System.out.println(tempLayer);

		double[] tempOutput = tempLayer.forward(tempInput);
		System.out.println("Forward, the output is: " + Arrays.toString(tempOutput));

		double[] tempError = tempLayer.backPropagation(tempOutput);
		System.out.println("Back propagation, the error is: " + Arrays.toString(tempError));
	}// Of unitTest

	
	public static void main(String[] args) {
		unitTest();
	}// Of main
}// Of class AnnLayer

截图:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946391.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号