利用Python语言实现神经网络模型
时间: 2024-02-16 22:55:11 浏览: 18
实现一个基本的神经网络模型可以分为以下几个步骤:
1. 导入必要的库
```python
import numpy as np
import matplotlib.pyplot as plt
```
2. 数据预处理
```python
# 加载数据
data = np.loadtxt('data.txt')
X = data[:, 0:2]
y = data[:, 2]
# 特征缩放
X = (X - np.mean(X, axis=0)) / np.std(X, axis=0)
# 将标签转换为列向量
y = y.reshape(-1, 1)
```
3. 定义神经网络模型
```python
class NeuralNetwork:
def __init__(self, input_dim, hidden_dim, output_dim):
# 初始化权重和偏置
self.W1 = np.random.randn(input_dim, hidden_dim)
self.b1 = np.zeros((1, hidden_dim))
self.W2 = np.random.randn(hidden_dim, output_dim)
self.b2 = np.zeros((1, output_dim))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def forward(self, X):
# 前向传播
z1 = np.dot(X, self.W1) + self.b1
a1 = self.sigmoid(z1)
z2 = np.dot(a1, self.W2) + self.b2
y_pred = self.sigmoid(z2)
return y_pred
def sigmoid_derivative(self, x):
return x * (1 - x)
def backward(self, X, y, y_pred, learning_rate):
# 反向传播
error = y - y_pred
d_output = error * self.sigmoid_derivative(y_pred)
error_hidden = d_output.dot(self.W2.T)
d_hidden = error_hidden * self.sigmoid_derivative(a1)
# 更新权重和偏置
self.W2 += a1.T.dot(d_output) * learning_rate
self.b2 += np.sum(d_output, axis=0, keepdims=True) * learning_rate
self.W1 += X.T.dot(d_hidden) * learning_rate
self.b1 += np.sum(d_hidden, axis=0) * learning_rate
def train(self, X, y, learning_rate, epochs):
# 训练模型
for i in range(epochs):
y_pred = self.forward(X)
self.backward(X, y, y_pred, learning_rate)
```
4. 训练模型
```python
input_dim = 2
hidden_dim = 4
output_dim = 1
learning_rate = 0.1
epochs = 10000
nn = NeuralNetwork(input_dim, hidden_dim, output_dim)
nn.train(X, y, learning_rate, epochs)
```
5. 测试模型
```python
# 预测新的数据点
x_new = np.array([-0.5, 0.5])
x_new = (x_new - np.mean(X, axis=0)) / np.std(X, axis=0)
y_new = nn.forward(x_new)
print('预测结果:', y_new)
# 绘制决策边界
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x1_min, x1_max, 0.1), np.arange(x2_min, x2_max, 0.1))
Z = nn.forward(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), alpha=0.8)
plt.show()
```