mlp神经网络预测加反向设计
时间: 2024-12-31 12:41:02 浏览: 5
### 使用MLP神经网络进行预测
多层感知器(Multilayer Perceptron, MLP)是一种典型的前馈型人工神经网络,由多个层次组成,每一层都完全连接到下一层。MLP通常用于监督学习任务,在给定输入特征的情况下预测连续值或分类标签。
为了使用MLP进行预测,需要经历以下几个方面的工作:
- **定义模型结构**:确定隐藏层数量及其大小、激活函数的选择等超参数设置。
- **初始化权重**:随机分配初始权值矩阵来启动训练过程。
- **正向传播计算**:依据当前设定好的参数执行一次完整的前向遍历操作,得到最终输出并据此计算误差[^1]。
```python
import numpy as np
from sklearn.neural_network import MLPRegressor
# 定义一个简单的回归问题的数据集
X = [[0., 0.], [1., 1.]]
y = [0, 1]
# 创建一个多层感知机回归器实例
mlp_regressor = MLPRegressor(hidden_layer_sizes=(5,), max_iter=200)
# 训练模型
mlp_regressor.fit(X, y)
# 预测新样本的结果
predictions = mlp_regressor.predict([[2., 2.]])
print(predictions)
```
### 反向传播算法的设计与实现
反向传播是调整神经网络内部各节点间连接强度的关键技术之一,其核心在于利用链式法则沿着错误梯度方向更新每一对相邻层之间的权重。具体来说,当完成一轮前向传递之后,会根据实际输出与期望输出间的差异构建损失函数,并以此为基础逆序逐层修正各个参数直至收敛于局部最优解处[^4]。
以下是简化版的手动实现反向传播的过程:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
s = sigmoid(x)
return s * (1 - s)
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.weights_input_hidden = np.random.randn(input_size, hidden_size)
self.bias_hidden = np.zeros((1, hidden_size))
self.weights_hidden_output = np.random.randn(hidden_size, output_size)
self.bias_output = np.zeros((1, output_size))
def forward(self, X):
z_hidden = np.dot(X, self.weights_input_hidden) + self.bias_hidden
a_hidden = sigmoid(z_hidden)
z_output = np.dot(a_hidden, self.weights_hidden_output) + self.bias_output
predicted_y = sigmoid(z_output)
return {
'z_hidden': z_hidden,
'a_hidden': a_hidden,
'z_output': z_output,
'predicted_y': predicted_y
}
def backward(self, X, Y_true, cache, learning_rate=0.01):
batch_size = X.shape[0]
delta_output = (cache['predicted_y'] - Y_true) * sigmoid_derivative(cache['z_output'])
d_weights_hidden_output = np.dot(cache['a_hidden'].T, delta_output) / batch_size
d_bias_output = np.sum(delta_output, axis=0, keepdims=True) / batch_size
delta_hidden = np.dot(delta_output, self.weights_hidden_output.T) * sigmoid_derivative(cache['z_hidden'])
d_weights_input_hidden = np.dot(X.T, delta_hidden) / batch_size
d_bias_hidden = np.sum(delta_hidden, axis=0, keepdims=True) / batch_size
# 更新权重和偏置项
self.weights_hidden_output -= learning_rate * d_weights_hidden_output
self.bias_output -= learning_rate * d_bias_output
self.weights_input_hidden -= learning_rate * d_weights_input_hidden
self.bias_hidden -= learning_rate * d_bias_hidden
def train(self, X_train, Y_train, epochs=10000):
for epoch in range(epochs):
cache = self.forward(X_train)
loss = ((Y_train - cache['predicted_y']) ** 2).mean()
if epoch % 1000 == 0:
print(f'Epoch {epoch}, Loss: {loss}')
self.backward(X_train, Y_train, cache)
nn = SimpleNeuralNetwork(2, 3, 1)
nn.train(np.array([[0, 0], [0, 1], [1, 0], [1, 1]]), np.array([0, 1, 1, 0]))
test_result = nn.forward(np.array([[0, 0], [0, 1], [1, 0], [1, 1]]))['predicted_y']
print(test_result)
```
阅读全文