人工神经网络(Artificial Neural Networks,简称ANNs)-python训练案例

人工神经网络(ANN)是一种受大脑启发的信息处理模式。就像人类一样,ANN通过示例来学习。通过学习过程,ANN被配置用于特定应用,例如模式识别或数据分类。学习过程主要涉及调整神经元之间存在的突触连接。

大脑由数千亿个称为神经元的细胞组成。这些神经元通过突触连接在一起,突触只不过是一个神经元可以向另一个神经元发送冲动的连接。当一个神经元向另一个神经元发送兴奋性信号时,该信号将被添加到该神经元的所有其他输入中。如果它超过给定的阈值,那么它将导致目标神经元向前发射动作信号——这就是思考过程内部的工作方式。
在计算机科学中,我们通过使用矩阵在计算机上创建 “网络” 来模拟这个过程。这些网络可以理解为神经元的抽象,而没有考虑所有生物复杂性。为简单起见,我们只对一个简单的 NN 进行建模,其中两层能够解决线性分类问题。

假设我们有一个问题,我们想在给定一组输入和输出作为训练示例的情况下预测输出,如下所示:

训练数据如下:

预测下面数据:

请注意,输出与第三列直接相关,即输入3的值是图2中每个训练示例中的输出。因此,对于测试示例,输出值应为1。

训练过程包括以下步骤:

  1. 前向传播:
    获取输入,乘以权重(仅使用随机数作为权重)
    设Y = WiIi = W1I1+W2I2+W3I3
  2. 通过 sigmoid 公式传递结果以计算神经元的输出。Sigmoid 函数用于在 0 和 1 之间标准化结果:
    1/1 + e-y
  3. 反向传播
    计算误差,即实际输出和预期输出之间的差值。根据误差,通过将误差乘以输入,然后再次乘以 S 形曲线的梯度来调整权重:
    权重 += 误差输入输出(1-输出),这里输出(1-输出)是S形曲线的导数。

注意:重复整个过程进行几千次迭代。

让我们用Python编写整个过程的代码。我们将使用Numpy库来帮助我们轻松完成矩阵上的所有计算。您需要在系统上安装numpy库才能运行代码

Command 来安装 numpy

sudo apt -get install python-numpy

# Python program to implement a 
# single neuron neural network

# import all necessary libraries
from numpy import exp, array, random, dot, tanh

# Class to create a neural 
# network with single neuron
class NeuralNetwork():
	
	def __init__(self):
		
		# Using seed to make sure it'll 
		# generate same weights in every run
		random.seed(1)
		
		# 3x1 Weight matrix
		self.weight_matrix = 2 * random.random((3, 1)) - 1

	# tanh as activation function
	def tanh(self, x):
		return tanh(x)

	# derivative of tanh function.
	# Needed to calculate the gradients.
	def tanh_derivative(self, x):
		return 1.0 - tanh(x) ** 2

	# forward propagation
	def forward_propagation(self, inputs):
		return self.tanh(dot(inputs, self.weight_matrix))
	
	# training the neural network.
	def train(self, train_inputs, train_outputs,
							num_train_iterations):
								
		# Number of iterations we want to
		# perform for this set of input.
		for iteration in range(num_train_iterations):
			output = self.forward_propagation(train_inputs)

			# Calculate the error in the output.
			error = train_outputs - output

			# multiply the error by input and then 
			# by gradient of tanh function to calculate
			# the adjustment needs to be made in weights
			adjustment = dot(train_inputs.T, error *
							self.tanh_derivative(output))
							
			# Adjust the weight matrix
			self.weight_matrix += adjustment

# Driver Code
if __name__ == "__main__":
	
	neural_network = NeuralNetwork()
	
	print ('Random weights at the start of training')
	print (neural_network.weight_matrix)

	train_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
	train_outputs = array([[0, 1, 1, 0]]).T

	neural_network.train(train_inputs, train_outputs, 10000)

	print ('New weights after training')
	print (neural_network.weight_matrix)

	# Test the neural network with a new situation.
	print ("Testing network on new examples ->")
	print (neural_network.forward_propagation(array([1, 0, 0])))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法资料吧!

我会继续分享编程资料,学习资料

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值