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

wufei123 发布于 2023-05-02 阅读(1418)

人工神经网络

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

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

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

人工神经网络算法示例

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

import numpy as np

class NeuralNetwork():

    def __init__(self, layers):

        """

        layers: 数组,包含每个层的神经元数量,例如 [2, 3, 1] 表示 3 层神经网络,第一层 2 个神经元,第二层 3 个神经元,第三层 1 个神经元。

        weights: 数组,包含每个连接的权重矩阵,默认值随机生成。

        biases: 数组,包含每个层的偏差值,默认值为 0。

        """

        self.layers = layers

        self.weights = [np.random.randn(a, b) for a, b in zip(layers[1:], layers[:-1])]

        self.biases = [np.zeros((a, 1)) for a in layers[1:]]

    def sigmoid(self, z):

        """Sigmoid 激活函数."""

        return 1 / (1 + np.exp(-z))

    def forward_propagation(self, a):

        """前向传播."""

        for w, b in zip(self.weights, self.biases):

            z = np.dot(w, a) + b

            a = self.sigmoid(z)

        return a

    def backward_propagation(self, x, y):

        """反向传播."""

        nabla_w = [np.zeros(w.shape) for w in self.weights]

        nabla_b = [np.zeros(b.shape) for b in self.biases]

        a = x

        activations = [x]

        zs = []

        for w, b in zip(self.weights, self.biases):

            z = np.dot(w, a) + b

            zs.append(z)

            a = self.sigmoid(z)

            activations.append(a)

        delta = self.cost_derivative(activations[-1], y) * self.sigmoid_prime(zs[-1])

        nabla_b[-1] = delta

        nabla_w[-1] = np.dot(delta, activations[-2].transpose())

        for l in range(2, len(self.layers)):

            z = zs[-l]

            sp = self.sigmoid_prime(z)

            delta = np.dot(self.weights[-l+1].transpose(), delta) * sp

            nabla_b[-l] = delta

            nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())

        return (nabla_w, nabla_b)

    def train(self, x_train, y_train, epochs, learning_rate):

        """训练网络."""

        for epoch in range(epochs):

            nabla_w = [np.zeros(w.shape) for w in self.weights]

            nabla_b = [np.zeros(b.shape) for b in self.biases]

            for x, y in zip(x_train, y_train):

                delta_nabla_w, delta_nabla_b = self.backward_propagation(np.array([x]).transpose(), np.array([y]).transpose())

                nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]

                nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]

            self.weights = [w-(learning_rate/len(x_train))*nw for w, nw in zip(self.weights, nabla_w)]

            self.biases = [b-(learning_rate/len(x_train))*nb for b, nb in zip(self.biases, nabla_b)]

    def predict(self, x_test):

        """预测."""

        y_predictions = []

        for x in x_test:

            y_predictions.append(self.forward_propagation(np.array([x]).transpose())[0][0])

        return y_predictions

    def cost_derivative(self, output_activations, y):

        """损失函数的导数."""

        return output_activations - y

    def sigmoid_prime(self, z):

        """Sigmoid 函数的导数."""

        return self.sigmoid(z) * (1 - self.sigmoid(z))

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

1

2

3

4

5

6

7

8

9

10

11

12

x_train = [[0, 0], [1, 0], [0, 1], [1, 1]]

y_train = [0, 1, 1, 0]

# 创建神经网络

nn = NeuralNetwork([2, 3, 1])

# 训练神经网络

nn.train(x_train, y_train, 10000, 0.1)

# 测试神经网络

x_test = [[0, 0], [1, 0], [0, 1], [1, 1]]

y_test = [0, 1, 1, 0]

y_predictions = nn.predict(x_test)

print("Predictions:", y_predictions)

print("Actual:", y_test)

输出结果:

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

以上就是python人工智能算法之人工神经网络怎么使用的详细内容


标签:  python 免费教程 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

河南中青旅行社综合资讯 奇遇综合资讯 盛世蓟州综合资讯 综合资讯 游戏百科综合资讯 新闻67902