以下是关于“Python利用三层神经网络实现手写数字分类详解”的完整攻略:
简介
神经网络是一种模拟人脑神经元工作方式的计算模型,它可以用于分类、回归、聚类等任务。在本教程中,我们将介绍如何使用Python实现一个三层神经网络,并使用MNIST数据集进行手写数字分类。
神经网络基本概念
神经网络由多个神经元组成,每个神经元接收多个输入,经过加权和和激活函数处理后,输出一个结果。神经网络的基本结构包括输入层、隐藏层和输出层。输入层接收输入数据,隐藏层对输入数据进行处理,输出层输出结果。神经网络的训练过程是通过反向传播算法来实现的,它可以调整神经元之间的权重和偏置,使得神经网络的输出结果更加准确。
神经网络 Python实现
以下是使用Python实现神经网络的代码:
import numpy as np
# Define the sigmoid function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Define the derivative of the sigmoid function
def sigmoid_derivative(x):
return x * (1 - x)
# Define the neural network class
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.weights1 = np.random.randn(self.input_size, self.hidden_size)
self.weights2 = np.random.randn(self.hidden_size, self.output_size)
def forward(self, X):
self.z = np.dot(X, self.weights1)
self.z2 = sigmoid(self.z)
self.z3 = np.dot(self.z2, self.weights2)
output = sigmoid(self.z3)
return output
def backward(self, X, y, output):
self.output_error = y - output
self.output_delta = self.output_error * sigmoid_derivative(output)
self.z2_error = self.output_delta.dot(self.weights2.T)
self.z2_delta = self.z2_error * sigmoid_derivative(self.z2)
self.weights1 += X.T.dot(self.z2_delta)
self.weights2 += self.z2.T.dot(self.output_delta)
def train(self, X, y):
output = self.forward(X)
self.backward(X, y, output)
# Define the input, hidden, and output sizes
input_size = 784
hidden_size = 16
output_size = 10
# Load the MNIST dataset
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshape the input data
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
# Normalize the input data
X_train = X_train / 255
X_test = X_test / 255
# Convert the output data to one-hot encoding
y_train_onehot = np.zeros((60000, 10))
y_train_onehot[np.arange(60000), y_train] = 1
# Create the neural network object
nn = NeuralNetwork(input_size, hidden_size, output_size)
# Train the neural network
for i in range(1000):
nn.train(X_train, y_train_onehot)
# Test the neural network
correct = 0
for i in range(len(X_test)):
output = nn.forward(X_test[i])
if np.argmax(output) == y_test[i]:
correct += 1
accuracy = correct / len(X_test)
print('Accuracy:', accuracy)
在这个示例中,我们定义了sigmoid函数和sigmoid_derivative函数,用于计算神经元的输出和导数。我们定义了NeuralNetwork类,包括初始化函数、前向传播函数、反向传播函数和训练函数。我们使用keras库中的mnist模块加载MNIST数据集,并使用reshape函数将输入数据转换为一维向量。我们使用normalize函数将输入数据归一化,并使用one-hot编码将输出数据转换为向量。我们创建一个NeuralNetwork对象,并使用train函数训练神经网络。我们使用forward函数测试神经网络,并使用argmax函数计算预测结果的最大值。
示例说明
以下是两个示例说明,展示了如何使用Python实现神经网络。
示例1
假设我们要使用神经网络对Iris数据集进行分类:
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Define the input, hidden, and output sizes
input_size = 4
hidden_size = 8
output_size = 3
# Create the neural network object
nn = NeuralNetwork(input_size, hidden_size, output_size)
# Train the neural network
for i in range(1000):
nn.train(X_train, y_train)
# Test the neural network
y_pred = []
for i in range(len(X_test)):
output = nn.forward(X_test[i])
y_pred.append(np.argmax(output))
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
在这个示例中,我们使用sklearn库中的datasets模块加载Iris数据集,并使用train_test_split函数将数据集分为训练集和测试集。我们定义了输入、隐藏和输出大小,并创建一个NeuralNetwork对象。我们使用train函数训练神经网络,并使用forward函数测试神经网络。我们使用argmax函数计算预测结果的最大值,并使用accuracy_score函数计算模型的准确率。
示例2
假设我们要使用神经网络对digits数据集进行分类:
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load digits dataset
digits = datasets.load_digits()
X = digits.data
y = digits.target
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Define the input, hidden, and output sizes
input_size = 64
hidden_size = 16
output_size = 10
# Create the neural network object
nn = NeuralNetwork(input_size, hidden_size, output_size)
# Train the neural network
for i in range(1000):
nn.train(X_train, y_train)
# Test the neural network
y_pred = []
for i in range(len(X_test)):
output = nn.forward(X_test[i])
y_pred.append(np.argmax(output))
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
在这个示例中,我们使用sklearn库中的datasets模块加载digits数据集,并使用train_test_split函数将数据集分为训练集和测试集。我们定义了输入、隐藏和输出大小,并创建一个NeuralNetwork对象。我们使用train函数训练神经网络,并使用forward函数测试神经网络。我们使用argmax函数计算预测结果的最大值,并使用accuracy_score函数计算模型的准确率。
结论
本教程介绍了如何使用Python实现一个三层神经网络,并使用MNIST数据集进行手写数字分类。我们定义了sigmoid函数和sigmoid_derivative函数,用于计算神经元的输出和导数。我们定义了NeuralNetwork类,包括初始化函数、前向传播函数、反向传播函数和训练函数。我们使用keras库中的mnist模块加载MNIST数据集,并使用reshape函数将输入数据转换为一维向量。我们使用normalize函数将输入数据归一化,并使用one-hot编码将输出数据转换为向量。我们创建一个NeuralNetwork对象,并使用train函数训练神经网络。我们使用forward函数测试神经网络,并使用argmax函数计算预测结果的最大值。我们还使用两个示例说明展示了如何使用Python实现神经网络。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python利用三层神经网络实现手写数字分类详解 - Python技术站