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

寻技术 Python编程 / 人工智能 2023年12月29日 89

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

人工神经网络

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

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

人工神经网络算法示例

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

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))

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

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]

关闭

用微信“扫一扫”