python手写识别mnist_详解python实现识别手写MNIST数字集的程序

我们需要做的第⼀件事情是获取 MNIST 数据。如果你是⼀个 git ⽤⼾,那么你能够通过克隆这本书的代码仓库获得数据,实现我们的⽹络来分类数字

git clone https://github.com/mnielsen/neural-networks-and-deep-learning.git

class Network(object):

def __init__(self, sizes):

self.num_layers = len(sizes)

self.sizes = sizes

self.biases = [np.random.randn(y, 1) for y in sizes[1:]]self.weights = [np.random.randn(y, x)

for x, y in zip(sizes[:-1], sizes[1:])]

在这段代码中,列表 sizes 包含各层神经元的数量。例如,如果我们想创建⼀个在第⼀层有2 个神经元,第⼆层有 3 个神经元,最后层有 1 个神经元的 Network 对象,我们应这样写代码:

net = Network([2, 3, 1])

Network 对象中的偏置和权重都是被随机初始化的,使⽤ Numpy 的 np.random.randn 函数来⽣成均值为 0,标准差为 1 的⾼斯分布。这样的随机初始化给了我们的随机梯度下降算法⼀个起点。在后⾯的章节中我们将会发现更好的初始化权重和偏置的⽅法,但是⽬前随机地将其初始化。注意 Network 初始化代码假设第⼀层神经元是⼀个输⼊层,并对这些神经元不设置任何偏置,因为偏置仅在后⾯的层中⽤于计算输出。有了这些,很容易写出从⼀个 Network 实例计算输出的代码。我们从定义 S 型函数开始

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了实现Python手写数字识别MNIST数据,可以使用神经网络算法。以下是实现步骤: 1. 导入必要的库和数据 ```python import numpy as np import matplotlib.pyplot as plt # 读取MNIST数据 data_file = open("mnist_train_100.csv") data_list = data_file.readlines() data_file.close() ``` 2. 数据预处理 ```python # 将数据中的每个数字图像转换为28x28的矩阵 all_values = data_list[0].split(',') image_array = np.asfarray(all_values[1:]).reshape((28,28)) # 将图像矩阵可视化 plt.imshow(image_array, cmap='Greys', interpolation='None') plt.show() # 将数据中的所有数字图像转换为28x28的矩阵,并将其存储在一个numpy数组中 scaled_input = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 ``` 3. 构建神经网络模型 ```python # 定义神经网络的输入、隐藏和输出层节点数 input_nodes = 784 hidden_nodes = 100 output_nodes = 10 # 初始化权重矩阵 weight_input_hidden = np.random.normal(0.0, pow(input_nodes, -0.5), (hidden_nodes, input_nodes)) weight_hidden_output = np.random.normal(0.0, pow(hidden_nodes, -0.5), (output_nodes, hidden_nodes)) # 定义激活函数 def sigmoid(x): return 1 / (1 + np.exp(-x)) # 计算神经网络的输出 hidden_inputs = np.dot(weight_input_hidden, scaled_input) hidden_outputs = sigmoid(hidden_inputs) final_inputs = np.dot(weight_hidden_output, hidden_outputs) final_outputs = sigmoid(final_inputs) ``` 4. 训练神经网络模型 ```python # 定义目标输出 target = np.zeros(output_nodes) + 0.01 target[int(all_values[0])] = 0.99 # 计算误差 output_errors = target - final_outputs hidden_errors = np.dot(weight_hidden_output.T, output_errors) # 更新权重矩阵 weight_hidden_output += learning_rate * np.dot((output_errors * final_outputs * (1.0 - final_outputs)), np.transpose(hidden_outputs)) weight_input_hidden += learning_rate * np.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), np.transpose(scaled_input)) ``` 5. 测试神经网络模型 ```python # 读取测试数据 test_data_file = open("mnist_test_10.csv") test_data_list = test_data_file.readlines() test_data_file.close() # 预处理测试数据 all_values = test_data_list[0].split(',') image_array = np.asfarray(all_values[1:]).reshape((28,28)) plt.imshow(image_array, cmap='Greys', interpolation='None') plt.show() scaled_input = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 # 计算神经网络的输出 hidden_inputs = np.dot(weight_input_hidden, scaled_input) hidden_outputs = sigmoid(hidden_inputs) final_inputs = np.dot(weight_hidden_output, hidden_outputs) final_outputs = sigmoid(final_inputs) # 输出神经网络的预测结果 print("预测结果:", np.argmax(final_outputs)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值