从头写一个神经网络识别Mnist数据集

本文介绍了如何从头开始构建一个神经网络来识别Mnist数据集。通过参考多个技术文章,解决了损失函数的实现问题。首先,下载Mnist数据集,然后使用两个Python脚本:change.py用于转换数据格式,test.py用于训练和评估模型,最终得出识别准确率。整个过程在Pycharm环境中进行。
摘要由CSDN通过智能技术生成

1、参考文章
①https://zhuanlan.zhihu.com/p/58964140
②https://blog.csdn.net/zugexiaodui/article/details/77130862?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
③https://blog.csdn.net/ebzxw/article/details/81591437
④https://www.cnblogs.com/codehome/p/9718611.html
2、难点:损失函数在代码中的体现
步骤:①先下载Mnist数据集
②运行第一个py文件:change.py
③运行第二个py文件:test.py,得到识别准确率
2、Mnist数据集下载地址:
下载后得到的内容:
在这里插入图片描述
3、编译器:Pycharm
4、第一个py文件:change.py
目的:将Mnist数据集文件转换为.csv文件

def convert(imgf, labelf, outf, n):
    f = open(imgf, "rb")
    o = open(outf, "w")
    l = open(labelf, "rb")

    f.read(16)
    l.read(8)
    images = []

    for i in range(n):
        image = [ord(l.read(1))]
        for j in range(28 * 28):
            image.append(ord(f.read(1)))
        images.append(image)

    for image in images:
        o.write(",".join(str(pix) for pix in image) + "\n")
    f.close()
    o.close()
    l.close()


convert("/Users/Downloads/MNIST_dataset/train-images.idx3-ubyte", "/UsersDownloads/MNIST_dataset/train-labels.idx1-ubyte",
        "mnist_train.csv", 60000)
convert("/Users/Downloads/MNIST_dataset/t10k-images.idx3-ubyte", "/Users/Downloads/MNIST_dataset/t10k-labels.idx1-ubyte",
        "mnist_test.csv", 10000)

print("Convert Finished!")

5、第二个py文件:test.py

# Code for a 3-layer neural network, and code for learning the MNIST dataset
# Zhouxw@
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是使用R语言实现卷积神经网络识别MNIST数据集的步骤: 1. 导入必要的库 首先,我们需要导入必要的R库,包括keras、tensorflow和reshape2。 ```R library(keras) library(tensorflow) library(reshape2) ``` 2. 加载数据集 接下来,我们需要加载MNIST数据集MNIST数据集包括手数字的图片和对应的标签。 ```R mnist <- dataset_mnist() x_train <- mnist$train$x y_train <- mnist$train$y x_test <- mnist$test$x y_test <- mnist$test$y ``` 3. 数据预处理 在训练模型之前,我们需要对数据进行预处理。首先,我们将图像的维度从28x28调整为一个长度为784的向量。然后,我们将像素值标准化为0到1之间的范围。 ```R x_train <- array_reshape(x_train, c(nrow(x_train), 784)) x_test <- array_reshape(x_test, c(nrow(x_test), 784)) x_train <- x_train / 255 x_test <- x_test / 255 ``` 此外,我们还需要将标签进行独热编码,以便在训练模型时使用。 ```R y_train <- to_categorical(y_train, 10) y_test <- to_categorical(y_test, 10) ``` 4. 构建模型 接下来,我们可以构建卷积神经网络模型。我们将使用两个卷积层和两个全连接层。 ```R model <- keras_model_sequential() %>% layer_reshape(input_shape = c(28, 28, 1), target_shape = c(28, 28, 1)) %>% layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu") %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_flatten() %>% layer_dense(units = 128, activation = "relu") %>% layer_dropout(rate = 0.5) %>% layer_dense(units = 10, activation = "softmax") ``` 5. 编译模型 在训练模型之前,我们需要编译模型。我们将使用categorical_crossentropy作为损失函数,Adam优化器和accuracy指标。 ```R model %>% compile( loss = "categorical_crossentropy", optimizer = optimizer_adam(), metrics = c("accuracy") ) ``` 6. 训练模型 现在,我们可以开始训练模型。我们将使用32个样本的批处理大小,10个epochs和验证集占20%。 ```R model %>% fit( x_train, y_train, batch_size = 32, epochs = 10, validation_split = 0.2 ) ``` 7. 评估模型 最后,我们可以评估模型在测试集上的性能。 ```R model %>% evaluate(x_test, y_test) ``` 完整代码如下: ```R library(keras) library(tensorflow) library(reshape2) # load MNIST dataset mnist <- dataset_mnist() x_train <- mnist$train$x y_train <- mnist$train$y x_test <- mnist$test$x y_test <- mnist$test$y # reshape and normalize data x_train <- array_reshape(x_train, c(nrow(x_train), 784)) x_test <- array_reshape(x_test, c(nrow(x_test), 784)) x_train <- x_train / 255 x_test <- x_test / 255 # one-hot encode labels y_train <- to_categorical(y_train, 10) y_test <- to_categorical(y_test, 10) # build model model <- keras_model_sequential() %>% layer_reshape(input_shape = c(28, 28, 1), target_shape = c(28, 28, 1)) %>% layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu") %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_flatten() %>% layer_dense(units = 128, activation = "relu") %>% layer_dropout(rate = 0.5) %>% layer_dense(units = 10, activation = "softmax") # compile model model %>% compile( loss = "categorical_crossentropy", optimizer = optimizer_adam(), metrics = c("accuracy") ) # train model model %>% fit( x_train, y_train, batch_size = 32, epochs = 10, validation_split = 0.2 ) # evaluate model model %>% evaluate(x_test, y_test) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值