BP神经网络基础类 (数据读取与基本结构)

一、BP神经网络是什么?

BP(back propagation)神经网络是一种按照误差逆向传播算法训练的多层前馈神经网络,是应用最广泛的神经网络模型之一。
从结构上讲,BP网络具有输入层、隐藏层和输出层;从本质上讲,BP算法就是以网络误差平方为目标函数、采用梯度下降法来计算目标函数的最小值。

二、神经网络的基础机制

BP神经网络的计算过程由正向计算过程和反向计算过程组成。
在这里插入图片描述
图 1. BP神经网络模型图

1. 正向传播

输入模式从输入层经隐含层逐层处理,并转向输出层,每一层神经元的状态只影响下一层神经元的状态。

1.1 神经元

神经网络的基本组成单元是神经元。神经元的通用模型如图2所示,其中常用的激活函数有阈值函数、sigmoid函数和双曲正切函数。
在这里插入图片描述
图 2. 神经元模型

神经元的输出为:
y = f ( ∑ i = 1 m w i x i ) (1) y = f(\sum_{i=1}^{m}w_ix_i)\tag{1} y=f(i=1mwixi)(1)

1.2 激活函数

BP神经网络采用的传递函数是非线性变换函数——Sigmoid函数(又称S函数)。其特点是函数本身及其导数都是连续的,因而在处理上十分方便。
Sigmoid函数公式为:
f ( x ) = 1 1 + e − x (2) f(x) = \frac{1}{1+e^{-x}}\tag{2} f(x)=1+ex1(2)
Sigmoid导函数公式为:
f ( x ) = 1 1 + e − x ( 1 − 1 1 + e − x ) = f ( x ) ( 1 − f ( x ) ) (3) f(x) = \frac{1}{1+e^{-x}}(1 - \frac{1}{1+e^{-x}}) = f(x)(1-f(x)) \tag{3} f(x)=1+ex1(11+ex1)=f(x)(1f(x))(3)

2.反向传播

反向传播将误差信号沿原来的连接通路返回,通过修改各神经元的权值,使得误差信号最小。

2.1 梯度下降

沿着梯度向量的方向,是训练误差增加最快的地方; 而沿着梯度向量相反的方向,梯度减少最快。

梯度下降法的直观理解参见下图:
在这里插入图片描述

在山峰附件的某处,要一步一步走向山底,一个好的办法是求解当前位置的梯度,然后沿着梯度的负方向向下走一步,然后继续求解当前位置的梯度,继续沿着梯度的负方向走下去,这样一步一步直到山底,这其中用到的方向就是梯度下降法。

梯度下降法也有一个问题就是如果初始点的位置选择的不合适,就容易导致找到的一个局部最优解,而不是全局最优解。

三、数据读取与基本结构的实现

forward and backPropagation方法写为抽象类,在子类中实现。

package machinelearning.ann;

import java.io.FileReader;
import java.util.Arrays;
import java.util.Random;

import weka.core.Instances;

/**
 * 
 * @author Ling Lin E-mail:linling0.0@foxmail.com
 * 
 * @version 创建时间:2022年5月21日 下午7:30:46
 * 
 */
public abstract class GeneralAnn {

	/**
	 * The whole dataset.
	 */
	Instances dataset;

	/**
	 * Number of layers. It is counted according to nodes instead of edges.
	 */
	int numLayers;

	/**
	 * The number of nodes for each layer, e.g., [3, 4, 6, 2] means that there
	 * are 3 input nodes (conditional attributes), 2 hidden layers with 4 and 6
	 * nodes, respectively, and 2 class values (binary classification).
	 */
	int[] layerNumNodes;

	/**
	 * Momentum coefficient.
	 */
	public double mobp;

	/**
	 * Learning rate.
	 */
	public double learningRate;

	/**
	 * For random number generation.
	 */
	Random random = new Random();

	/**
	 ********************
	 * 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.
	 ********************
	 */
	public GeneralAnn(String paraFilename, int[] paraLayerNumNodes, double paraLearningRate, double paraMobp) {
		// Step 1. Read data.
		try {
			FileReader tempReader = new FileReader(paraFilename);
			dataset = new Instances(tempReader);
			// The last attribute is the decision class.
			dataset.setClassIndex(dataset.numAttributes() - 1);
			tempReader.close();
		} catch (Exception ee) {
			System.out.println(
					"Error occurred while trying to read \'" + paraFilename + "\' in GeneralAnn constructor.\r\n" + ee);
			System.exit(0);
		} // Of try

		// Step 2. Accept parameters.
		layerNumNodes = paraLayerNumNodes;
		numLayers = layerNumNodes.length;
		// Adjust if necessary.
		layerNumNodes[0] = dataset.numAttributes() - 1;
		layerNumNodes[numLayers - 1] = dataset.numClasses();
		learningRate = paraLearningRate;
		mobp = paraMobp;
	}// Of the first constructor

	/**
	 ********************
	 * Forward prediction.
	 * 
	 * @param paraInput
	 *            The input data of one instance.
	 * @return The data at the output end.
	 ********************
	 */
	public abstract double[] forward(double[] paraInput);

	/**
	 ********************
	 * Back propagation.
	 * 
	 * @param paraTarget
	 *            For 3-class data, it is [0, 0, 1], [0, 1, 0] or [1, 0, 0].
	 * 
	 ********************
	 */
	public abstract void backPropagation(double[] paraTarget);

	/**
	 ********************
	 * Train using the dataset.
	 ********************
	 */
	public void train() {
		double[] tempInput = new double[dataset.numAttributes() - 1];
		double[] tempTarget = new double[dataset.numClasses()];
		for (int i = 0; i < dataset.numInstances(); i++) {
			// Fill the data.
			for (int j = 0; j < tempInput.length; j++) {
				tempInput[j] = dataset.instance(i).value(j);
			} // Of for j

			// Fill the class label.
			Arrays.fill(tempTarget, 0);
			tempTarget[(int) dataset.instance(i).classValue()] = 1;

			// Train with this instance.
			forward(tempInput);
			backPropagation(tempTarget);
		} // Of for i
	}// Of train

	/**
	 ********************
	 * Get the index corresponding to the max value of the array.
	 * 
	 * @return the index.
	 ********************
	 */
	public static int argmax(double[] paraArray) {
		int resultIndex = -1;
		double tempMax = -1e10;
		for (int i = 0; i < paraArray.length; i++) {
			if (tempMax < paraArray[i]) {
				tempMax = paraArray[i];
				resultIndex = i;
			} // Of if
		} // Of for i

		return resultIndex;
	}// Of argmax

	/**
	 ********************
	 * Test using the dataset.
	 * 
	 * @return The precision.
	 ********************
	 */
	public double test() {
		double[] tempInput = new double[dataset.numAttributes() - 1];

		double tempNumCorrect = 0;
		double[] tempPrediction;
		int tempPredictedClass = -1;

		for (int i = 0; i < dataset.numInstances(); i++) {
			// Fill the data.
			for (int j = 0; j < tempInput.length; j++) {
				tempInput[j] = dataset.instance(i).value(j);
			} // Of for j

			// Train with this instance.
			tempPrediction = forward(tempInput);
			// System.out.println("prediction: " +
			// Arrays.toString(tempPrediction));
			tempPredictedClass = argmax(tempPrediction);
			if (tempPredictedClass == (int) dataset.instance(i).classValue()) {
				tempNumCorrect++;
			} // Of if
		} // Of for i

		System.out.println("Correct: " + tempNumCorrect + " out of " + dataset.numInstances());

		return tempNumCorrect / dataset.numInstances();
	}// Of test
}// Of class GeneralAnn

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bp神经网络(Back Propagation Neural Network)是一种基本的人工神经网络模型,它采用反向传播算法来训练网络。该算法通过计算损失函数对权重的梯度进行调整,以减小训练误差。通过反复迭代训练,网络逐渐调整权重,使得输出结果更加接近期望值。 深度神经网络(Deep Neural Network)是一种由多个隐含层构成的人工神经网络。它的隐含层可以有很多层,有时甚至可达到几十层或更多。深度神经网络通过多层的非线性变换和特征抽取,可以实现更强大的模式识别能力。 与传统的浅层神经网络相比,深度神经网络具有以下优点: 1. 更好的特征表示能力:通过多层的非线性变换,深度神经网络可以逐步将原始数据进行抽象和转换,获取更加丰富和高级的特征表示,从而提升了模型的表达能力。 2. 更强的非线性拟合能力:深度神经网络通过引入多层非线性激活函数,可以灵活地拟合各种复杂的非线性关系。 3. 更好的泛化能力:深度神经网络可以通过正则化等方法抑制过拟合现象,从而具有更好的泛化能力。 然而,深度神经网络也存在一些挑战和困难: 1. 训练难度增加:随着网络层数的增加,深度神经网络的训练复杂度也增加。深层网络更容易出现梯度消失或梯度爆炸等问题,导致训练困难。 2. 需要大量数据和计算资源:深度神经网络通常需要大量的训练数据才能取得好的性能。此外,深层网络的计算复杂度也较高,需要大量的计算资源和时间。 3. 参数调整和设置困难:深度神经网络的网络结构和参数设置较为复杂,需要进行大量的实验和调整。 总的来说,bp神经网络是深度神经网络中最基础的模型,而深度神经网络则是在bp神经网络基础上引入更多隐含层的模型。深度神经网络通过多层的非线性变换和特征抽取,具备更强大的学习和表达能力,但也面临训练难度增加、大量数据和计算资源需求以及参数调整困难等问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值