BP神经网络实现mnist
时间: 2023-06-11 16:07:50 浏览: 112
mnist BP神经网络
4星 · 用户满意度95%
MNIST是一个手写数字的图像数据集,BP神经网络可以用于分类这些图像。下面是一个简单的Python代码实现MNIST数据集的分类任务,使用了一个三层的BP神经网络。
首先,我们需要导入必要的库和MNIST数据集:
```python
import numpy as np
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
X = mnist.data.astype('float32')
y = mnist.target.astype('int32')
```
然后,我们将数据集划分为训练集和测试集:
```python
num_train = 60000
X_train, X_test = np.split(X, [num_train])
y_train, y_test = np.split(y, [num_train])
```
接下来,我们定义BP神经网络的结构和超参数:
```python
input_size = 784
hidden_size = 256
output_size = 10
learning_rate = 0.1
num_epochs = 20
batch_size = 100
```
然后,我们定义激活函数和BP神经网络的前向和反向传播函数:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def forward(x, W1, b1, W2, b2):
z1 = np.dot(x, W1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, W2) + b2
a2 = np.exp(z2) / np.sum(np.exp(z2), axis=1, keepdims=True)
return a1, a2
def backward(x, y, a1, a2, W1, b1, W2, b2):
m = x.shape[0]
dz2 = a2 - y
dW2 = np.dot(a1.T, dz2) / m
db2 = np.sum(dz2, axis=0, keepdims=True) / m
dz1 = np.dot(dz2, W2.T) * a1 * (1 - a1)
dW1 = np.dot(x.T, dz1) / m
db1 = np.sum(dz1, axis=0, keepdims=True) / m
return dW1, db1, dW2, db2
```
接下来,我们初始化BP神经网络的参数:
```python
W1 = np.random.randn(input_size, hidden_size) * 0.01
b1 = np.zeros((1, hidden_size))
W2 = np.random.randn(hidden_size, output_size) * 0.01
b2 = np.zeros((1, output_size))
```
然后,我们开始训练BP神经网络:
```python
num_batches = num_train // batch_size
for epoch in range(num_epochs):
for batch in range(num_batches):
start = batch * batch_size
end = start + batch_size
x_batch = X_train[start:end]
y_batch = np.zeros((batch_size, output_size))
y_batch[np.arange(batch_size), y_train[start:end]] = 1
a1, a2 = forward(x_batch, W1, b1, W2, b2)
dW1, db1, dW2, db2 = backward(x_batch, y_batch, a1, a2, W1, b1, W2, b2)
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
W2 -= learning_rate * dW2
b2 -= learning_rate * db2
_, a2 = forward(X_train, W1, b1, W2, b2)
train_loss = -np.mean(np.sum(np.log(a2) * (y_train == np.arange(output_size)), axis=1))
_, a2 = forward(X_test, W1, b1, W2, b2)
test_loss = -np.mean(np.sum(np.log(a2) * (y_test == np.arange(output_size)), axis=1))
print('Epoch %d, train loss: %.4f, test loss: %.4f' % (epoch+1, train_loss, test_loss))
```
最后,我们可以使用训练好的BP神经网络对测试集进行分类:
```python
_, a2 = forward(X_test, W1, b1, W2, b2)
y_pred = np.argmax(a2, axis=1)
accuracy = np.mean(y_pred == y_test)
print('Accuracy: %.4f' % accuracy)
```
这个简单的BP神经网络可以得到约96%的准确率。需要注意的是,这只是一个简单的示例,实际上,为了得到更好的性能,需要进行更多的优化和改进。
阅读全文