Python实例题:使用Python实现深度神经网络
目录
Python实例题
题目
代码实现
实现原理
模块化设计:
反向传播算法:
损失函数:
激活函数:
关键代码解析
1. 全连接层实现
2. ReLU 激活函数
3. 神经网络训练
使用说明
构建网络:
设置损失函数和学习率:
训练网络:
进行预测:
扩展建议
增强功能:
性能优化:
增加功能:
应用扩展:
Python实例题
题目
使用Python实现深度神经网络
代码实现
import numpy as npclass Layer:"""神经网络层的基类"""def __init__(self):self.input = Noneself.output = Nonedef forward(self, input_data):"""前向传播计算输出"""raise NotImplementedErrordef backward(self, output_gradient, learning_rate):"""反向传播计算梯度并更新参数"""raise NotImplementedErrorclass Dense(Layer):"""全连接层"""def __init__(self, input_size, output_size):super().__init__()# 随机初始化权重和偏置self.weights = np.random.randn(output_size, input_size) * 0.01self.bias = np.zeros((output_size, 1))def forward(self, input_data):self.input = input_datareturn np.dot(self.weights, self.input) + self.biasdef backward(self, output_gradient, learning_rate):# 计算权重、偏置和输入的梯度weights_gradient = np.dot(output_gradient, self.input.T)input_gradient = np.dot(self.weights.T, output_gradient)bias_gradient = output_gradient# 更新参数self.weights -= learning_rate * weights_gradientself.bias -= learning_rate * bias_gradientreturn input_gradientclass Activation(Layer):"""激活函数层的基类"""def __init__(self, activation, activation_prime):super().__init__()self.activation = activationself.activation_prime = activation_primedef forward(self, input_data):self.input = input_datareturn self.activation(self.input)def backward(self, output_gradient, learning_rate):# 应用激活函数导数return np.multiply(output_gradient, self.activation_prime(self.input))class ReLU(Activation):"""ReLU激活函数"""def __init__(self):relu = lambda x: np.maximum(0, x)relu_prime = lambda x: np.where(x > 0, 1, 0)super().__init__(relu, relu_prime)class Sigmoid(Activation):"""Sigmoid激活函数"""def __init__(self):sigmoid = lambda x: 1 / (1 + np.exp(-x))sigmoid_prime = lambda x: sigmoid(x) * (1 - sigmoid(x))super().__init__(sigmoid, sigmoid_prime)class Softmax(Layer):"""Softmax激活函数,用于多分类问题"""def forward(self, input_data):tmp = np.exp(input_data)self.output = tmp / np.sum(tmp, axis=0)return self.outputdef backward(self, output_gradient, learning_rate):# Softmax的梯度计算比较复杂,这里简化实现n = np.size(self.output)return np.dot((np.identity(n) - self.output.T) * self.output, output_gradient)class Loss:"""损失函数的基类"""def loss(self, y_true, y_pred):raise NotImplementedErrordef gradient(self, y_true, y_pred):raise NotImplementedErrorclass MSE(Loss):"""均方误差损失函数"""def loss(self, y_true, y_pred):return np.mean(np.power(y_true - y_pred, 2))def gradient(self, y_true, y_pred):return 2 * (y_pred - y_true) / np.size(y_true)class CrossEntropy(Loss):"""交叉熵损失函数"""def loss(self, y_true, y_pred):# 避免对数计算中的数值不稳定epsilon = 1e-15y_pred = np.clip(y_pred, epsilon, 1 - epsilon)return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))def gradient(self, y_true, y_pred):# 避免除以零epsilon = 1e-15y_pred = np.clip(y_pred, epsilon, 1 - epsilon)return - (y_true / y_pred) + ((1 - y_true) / (1 - y_pred))class NeuralNetwork:"""神经网络类"""def __init__(self):self.layers = []self.loss = Noneself.learning_rate = Nonedef add(self, layer):"""添加层到网络"""self.layers.append(layer)def set_loss(self, loss, learning_rate):"""设置损失函数和学习率"""self.loss = lossself.learning_rate = learning_ratedef predict(self, input_data):"""使用网络进行预测"""result = []# 对每个样本进行前向传播for sample in input_data:output = sample.reshape(-1, 1)for layer in self.layers:output = layer.forward(output)result.append(output.flatten())return np.array(result)def train(self, X_train, y_train, epochs):"""训练神经网络"""samples = len(X_train)for epoch in range(epochs):loss = 0for i in range(samples):# 前向传播output = X_train[i].reshape(-1, 1)for layer in self.layers:output = layer.forward(output)# 计算损失loss += self.loss.loss(y_train[i], output)# 反向传播gradient = self.loss.gradient(y_train[i], output)for layer in reversed(self.layers):gradient = layer.backward(gradient, self.learning_rate)# 打印每个epoch的损失loss /= samplesprint(f'Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}')# 使用示例:训练XOR问题
if __name__ == "__main__":# 准备数据X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])y = np.array([[0], [1], [1], [0]])# 构建神经网络nn = NeuralNetwork()nn.add(Dense(2, 3)) # 输入层到隐藏层nn.add(ReLU()) # 隐藏层激活函数nn.add(Dense(3, 1)) # 隐藏层到输出层nn.add(Sigmoid()) # 输出层激活函数# 设置损失函数和学习率nn.set_loss(MSE(), learning_rate=0.1)# 训练网络nn.train(X, y, epochs=1000)# 测试预测predictions = nn.predict(X)print("\n预测结果:")for i in range(len(X)):print(f"输入: {X[i]}, 预测: {predictions[i][0]:.4f}, 实际: {y[i][0]}")
实现原理
这个深度神经网络框架基于以下核心概念实现:
-
模块化设计:
- 层(Layer)作为基本构建块
- 分离前向传播和反向传播逻辑
- 支持不同类型的层(全连接、激活函数)
-
反向传播算法:
- 通过链式法则计算梯度
- 逐层传递误差信号
- 实现参数更新的梯度下降
-
损失函数:
- 均方误差(MSE)用于回归问题
- 交叉熵(CrossEntropy)用于分类问题
-
激活函数:
- ReLU:提供非线性变换
- Sigmoid:用于二分类输出层
- Softmax:用于多分类输出层
关键代码解析
1. 全连接层实现
class Dense(Layer):def __init__(self, input_size, output_size):# 随机初始化权重和偏置self.weights = np.random.randn(output_size, input_size) * 0.01self.bias = np.zeros((output_size, 1))def forward(self, input_data):return np.dot(self.weights, input_data) + self.biasdef backward(self, output_gradient, learning_rate):# 计算梯度weights_gradient = np.dot(output_gradient, self.input.T)input_gradient = np.dot(self.weights.T, output_gradient)# 更新参数self.weights -= learning_rate * weights_gradientself.bias -= learning_rate * output_gradientreturn input_gradient
2. ReLU 激活函数
class ReLU(Activation):def __init__(self):relu = lambda x: np.maximum(0, x)relu_prime = lambda x: np.where(x > 0, 1, 0)super().__init__(relu, relu_prime)
3. 神经网络训练
class NeuralNetwork:def train(self, X_train, y_train, epochs):for epoch in range(epochs):loss = 0for i in range(len(X_train)):# 前向传播output = X_train[i].reshape(-1, 1)for layer in self.layers:output = layer.forward(output)# 计算损失loss += self.loss.loss(y_train[i], output)# 反向传播gradient = self.loss.gradient(y_train[i], output)for layer in reversed(self.layers):gradient = layer.backward(gradient, self.learning_rate)print(f'Epoch {epoch+1}, Loss: {loss/len(X_train):.4f}')
使用说明
-
构建网络:
nn = NeuralNetwork()
nn.add(Dense(2, 3)) # 输入层(2)到隐藏层(3)
nn.add(ReLU()) # ReLU激活函数
nn.add(Dense(3, 1)) # 隐藏层(3)到输出层(1)
nn.add(Sigmoid()) # Sigmoid激活函数
-
设置损失函数和学习率:
nn.set_loss(MSE(), learning_rate=0.1)
-
训练网络:
nn.train(X_train, y_train, epochs=1000)
-
进行预测:
predictions = nn.predict(X_test)
扩展建议
-
增强功能:
- 添加更多层类型(卷积层、池化层、Dropout)
- 实现批量归一化(Batch Normalization)
- 添加优化器(Adam、RMSProp、Adagrad)
-
性能优化:
- 向量化操作提高计算效率
- 实现 mini-batch 训练
- 添加 GPU 支持(使用 Numba 或 PyTorch)
-
增加功能:
- 实现早停(Early Stopping)
- 添加学习率调度
- 支持模型保存和加载
-
应用扩展:
- 实现图像分类应用
- 开发自然语言处理模型
- 构建强化学习代理