Python实现的简单神经网络及其在手写数字识别中的实验分析
发布时间:2023-12-17 00:41:05
神经网络是一种模仿人脑神经元工作原理的机器学习算法。它通过训练一系列的神经元连接权重来学习数据的模式和特征,从而实现各种任务,如分类、回归和聚类。
在Python中,使用第三方库可以轻松地实现一个简单的神经网络。下面我们以手写数字识别为例,进行实验分析。
首先,我们需要导入所需的库:
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_digits from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score
接下来,我们加载由sklearn库提供的手写数字数据集:
digits = load_digits() X = digits.data y = digits.target
然后,我们将数据集分为训练集和测试集:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
接着,我们定义一个简单的神经网络类:
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights_input_hidden = np.random.randn(self.input_size, self.hidden_size)
self.bias_input_hidden = np.random.randn(self.hidden_size)
self.weights_hidden_output = np.random.randn(self.hidden_size, self.output_size)
self.bias_hidden_output = np.random.randn(self.output_size)
def forward(self, X):
self.hidden_layer = self.sigmoid(np.dot(X, self.weights_input_hidden) + self.bias_input_hidden)
self.output_layer = self.sigmoid(np.dot(self.hidden_layer, self.weights_hidden_output) + self.bias_hidden_output)
return self.output_layer
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def backward(self, X, y, output, learning_rate):
self.output_error = y - output
self.output_delta = self.output_error * self.sigmoid_derivative(output)
self.hidden_error = self.output_delta.dot(self.weights_hidden_output.T)
self.hidden_delta = self.hidden_error * self.sigmoid_derivative(self.hidden_layer)
self.weights_hidden_output += self.hidden_layer.T.dot(self.output_delta) * learning_rate
self.bias_hidden_output += np.sum(self.output_delta) * learning_rate
self.weights_input_hidden += X.T.dot(self.hidden_delta) * learning_rate
self.bias_input_hidden += np.sum(self.hidden_delta) * learning_rate
def train(self, X, y, epochs, learning_rate):
self.loss = []
for epoch in range(epochs):
output = self.forward(X)
self.backward(X, y, output, learning_rate)
loss = np.mean(np.square(y - output))
self.loss.append(loss)
if epoch % 100 == 0:
print(f"Epoch {epoch}, loss: {loss}")
def predict(self, X):
output = self.forward(X)
predictions = np.argmax(output, axis=1)
return predictions
在定义完神经网络类后,我们可以创建一个神经网络对象,并使用训练集进行训练:
input_size = X_train.shape[1] hidden_size = 32 output_size = len(np.unique(y_train)) neural_network = NeuralNetwork(input_size, hidden_size, output_size) neural_network.train(X_train, y_train, epochs=1000, learning_rate=0.01)
训练完成后,我们可以使用测试集进行预测,并计算模型的准确率:
predictions = neural_network.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy}")
最后,我们可以绘制损失函数随训练轮次的变化情况,以及部分测试集样本的预测结果:
plt.plot(range(len(neural_network.loss)), neural_network.loss)
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.show()
# 随机选择10个样本进行展示
plt.figure(figsize=(10, 4))
for i, index in enumerate(np.random.randint(len(X_test), size=10)):
plt.subplot(2, 5, i+1)
plt.imshow(X_test[index].reshape(8, 8), cmap='gray')
plt.title(f"Prediction: {predictions[index]}")
plt.axis('off')
plt.show()
通过以上步骤,我们成功实现了一个简单的神经网络,并使用手写数字数据集进行了实验分析。通过调整神经网络的参数和超参数,可以进一步提高模型的准确率和性能。
