python人工智能算法之人工神经网络怎么使用

其他教程   发布日期:2023年09月21日   浏览次数:416

本篇内容介绍了“python人工智能算法之人工神经网络怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

人工神经网络

(Artificial Neural Network,ANN)是一种模仿生物神经网络的结构和功能的数学模型,其目的是通过学习和训练,在处理未知的输入数据时能够进行复杂的非线性映射关系,实现自适应的智能决策。可以说,ANN是人工智能算法中最基础、最核心的一种算法。

ANN模型的基本结构包含输入层、隐藏层和输出层。输入层接收输入数据,隐藏层负责对数据进行多层次、高维度的变换和处理,输出层对处理后的数据进行输出。ANN的训练过程是通过多次迭代,不断调整神经网络中各层的权重,从而使得神经网络能够对输入数据进行正确的预测和分类。

人工神经网络算法示例

接下来看看一个简单的人工神经网络算法示例:

  1. import numpy as np
  2. class NeuralNetwork():
  3. def __init__(self, layers):
  4. """
  5. layers: 数组,包含每个层的神经元数量,例如 [2, 3, 1] 表示 3 层神经网络,第一层 2 个神经元,第二层 3 个神经元,第三层 1 个神经元。
  6. weights: 数组,包含每个连接的权重矩阵,默认值随机生成。
  7. biases: 数组,包含每个层的偏差值,默认值为 0。
  8. """
  9. self.layers = layers
  10. self.weights = [np.random.randn(a, b) for a, b in zip(layers[1:], layers[:-1])]
  11. self.biases = [np.zeros((a, 1)) for a in layers[1:]]
  12. def sigmoid(self, z):
  13. """Sigmoid 激活函数."""
  14. return 1 / (1 + np.exp(-z))
  15. def forward_propagation(self, a):
  16. """前向传播."""
  17. for w, b in zip(self.weights, self.biases):
  18. z = np.dot(w, a) + b
  19. a = self.sigmoid(z)
  20. return a
  21. def backward_propagation(self, x, y):
  22. """反向传播."""
  23. nabla_w = [np.zeros(w.shape) for w in self.weights]
  24. nabla_b = [np.zeros(b.shape) for b in self.biases]
  25. a = x
  26. activations = [x]
  27. zs = []
  28. for w, b in zip(self.weights, self.biases):
  29. z = np.dot(w, a) + b
  30. zs.append(z)
  31. a = self.sigmoid(z)
  32. activations.append(a)
  33. delta = self.cost_derivative(activations[-1], y) * self.sigmoid_prime(zs[-1])
  34. nabla_b[-1] = delta
  35. nabla_w[-1] = np.dot(delta, activations[-2].transpose())
  36. for l in range(2, len(self.layers)):
  37. z = zs[-l]
  38. sp = self.sigmoid_prime(z)
  39. delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
  40. nabla_b[-l] = delta
  41. nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
  42. return (nabla_w, nabla_b)
  43. def train(self, x_train, y_train, epochs, learning_rate):
  44. """训练网络."""
  45. for epoch in range(epochs):
  46. nabla_w = [np.zeros(w.shape) for w in self.weights]
  47. nabla_b = [np.zeros(b.shape) for b in self.biases]
  48. for x, y in zip(x_train, y_train):
  49. delta_nabla_w, delta_nabla_b = self.backward_propagation(np.array([x]).transpose(), np.array([y]).transpose())
  50. nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
  51. nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
  52. self.weights = [w-(learning_rate/len(x_train))*nw for w, nw in zip(self.weights, nabla_w)]
  53. self.biases = [b-(learning_rate/len(x_train))*nb for b, nb in zip(self.biases, nabla_b)]
  54. def predict(self, x_test):
  55. """预测."""
  56. y_predictions = []
  57. for x in x_test:
  58. y_predictions.append(self.forward_propagation(np.array([x]).transpose())[0][0])
  59. return y_predictions
  60. def cost_derivative(self, output_activations, y):
  61. """损失函数的导数."""
  62. return output_activations - y
  63. def sigmoid_prime(self, z):
  64. """Sigmoid 函数的导数."""
  65. return self.sigmoid(z) * (1 - self.sigmoid(z))

使用以下代码示例来实例化和使用这个简单的神经网络类:

  1. x_train = [[0, 0], [1, 0], [0, 1], [1, 1]]
  2. y_train = [0, 1, 1, 0]
  3. # 创建神经网络
  4. nn = NeuralNetwork([2, 3, 1])
  5. # 训练神经网络
  6. nn.train(x_train, y_train, 10000, 0.1)
  7. # 测试神经网络
  8. x_test = [[0, 0], [1, 0], [0, 1], [1, 1]]
  9. y_test = [0, 1, 1, 0]
  10. y_predictions = nn.predict(x_test)
  11. print("Predictions:", y_predictions)
  12. print("Actual:", y_test)

输出结果:

Predictions: [0.011602156431658403, 0.9852717774725432, 0.9839448924887225, 0.020026540429992387]
Actual: [0, 1, 1, 0]

以上就是python人工智能算法之人工神经网络怎么使用的详细内容,更多关于python人工智能算法之人工神经网络怎么使用的资料请关注九品源码其它相关文章!