很高兴为您提供一个基于 Python 的神经网络示例,具有 10 个输入参数,5 个隐藏层和 2 个输出层。

首先,我们需要导入所需的库:

import numpy as np
import matplotlib.pyplot as plt

接下来,我们将定义神经网络的各个层,包括输入层,隐藏层和输出层:

# 定义神经网络的各个层
class NeuralNetwork:
    def __init__(self, input_layer_size, hidden_layer_size, output_layer_size, num_hidden_layers):
        # 初始化权重和偏置
        self.weights = []
        self.biases = []
        # 输入层到第一个隐藏层
        self.weights.append(np.random.randn(input_layer_size, hidden_layer_size))
        self.biases.append(np.random.randn(hidden_layer_size))
        # 隐藏层之间
        for i in range(num_hidden_layers - 1):
            self.weights.append(np.random.randn(hidden_layer_size, hidden_layer_size))
            self.biases.append(np.random.randn(hidden_layer_size))
        # 最后一个隐藏层到输出层
        self.weights.append(np.random.randn(hidden_layer_size, output_layer_size))
        self.biases.append(np.random.randn(output_layer_size))

接下来,我们将定义神经网络的前向传播函数,该函数将输入数据传递到隐藏层和输出层:

# 定义前向传播函数
    def forward(self, X):
        # 输入层到第一个隐藏层
        Z = np.dot(X, self.weights[0]) + self.biases[0]
        A = np.tanh(Z)
        # 隐藏层之间
        for i in range(1, len(self.weights) - 1):
            Z = np.dot(A, self.weights[i]) + self.biases[i]
            A = np.tanh(Z)
        # 最后一个隐藏层到输出层
        Z = np.dot(A, self.weights[-1]) + self.biases[-1]
        A = np.exp(Z) / np.sum(np.exp(Z), axis=1, keepdims=True)
        return A

接下来,我们将定义神经网络的反向传播函数,该函数将计算损失并更新权重和偏置:

# 定义反向传播函数
    def backward(self, X, y, learning_rate):
        # 计算梯度
        dZ2 = self.forward(X) - y
        dW2 = np.dot(self.layer1.T, dZ2)
        db2 = np.sum(dZ2, axis=0)
        dZ1 = np.dot(dZ2, self.weights[1].T) * (1 - np.power(self.layer1, 2))
        dW1 = np.dot(X.T, dZ1)
        db1 = np.sum(dZ1, axis=0)
        # 更新权重和偏置
        self.weights[0] -= learning_rate * dW1
        self.biases[0] -= learning_rate * db1
        for i in range(1, len(self.weights) - 1):
            self.weights[i] -= learning_rate * dW2
            self.biases[i] -= learning_rate * db2
        self.weights[-1] -= learning_rate * dW2
        self.biases[-1] -= learning_rate * db2

最后,我们将定义训练函数来训练神经网络,并使用测试数据集对其进行评估:

# 定义训练函数
def train(X, y, input_layer_size, hidden_layer_size, output_layer_size, num_hidden_layers, num_iterations, learning_rate):
    network = NeuralNetwork(input_layer_size, hidden_layer_size, output_layer_size, num_hidden_layers)
    loss_history = []
    for i in range(num_iterations):
        # 前向传播
        A2 = network.forward(X)
        # 计算损失
        loss = -np.mean(np.sum(y * np.log(A2), axis=1))
        loss_history.append(loss)
        # 反向传播
        network.backward(X, y, learning_rate)
        # 打印损失
        if i % 1000 == 0:
            print('Iteration: %d - Loss: %.5f' % (i, loss))
    # 绘制损失历史
    plt.plot(loss_history)
    plt.xlabel('Iterations')
    plt.ylabel('Loss')
    plt.show()
    # 使用测试数据集评估模型
    test_X = np.random.randn(100, input_layer_size)
    test_y = np.random.randint(0, output_layer_size, size=(100, output_layer_size))
    test_A2 = network.forward(test_X)
    accuracy = np.mean(np.argmax(test_y, axis=1) == np.argmax(test_A2, axis=1))
    print('Test Accuracy: %.2f%%' % (accuracy * 100))

现在我们可以使用此训练函数来训练我们的神经网络,并使用测试数据集对其进行评估:

# 定义训练数据
X = np.random.randn(1000, 10)
y = np.random.randint(0, 2, size=(1000, 2))
# 训练神经网络
train(X, y, input_layer_size=10, hidden_layer_size=100, output_layer_size=2, num_hidden_layers=5, num_iterations=10000, learning_rate=0.01)

这将输出训练过程中的损失历史,并输出使用测试数据集评估模型的准确度。

希望这个示例对您有所帮助!

Python 神经网络实现:10个输入参数,5个隐藏层,2个输出层

原文地址: https://www.cveoy.top/t/topic/m4eq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录