使用MNIST数据集

本文提供了一种简单、快速获取MNIST数据集的方法,包括从百度云链接下载和创建input_data.py文件来导入数据。通过新建MINIST_data文件夹存放解压后的数据,避免了网络限制导致的下载延迟问题。
摘要由CSDN通过智能技术生成

首先,必须向各位强调的是:该数据集名字叫MNIST,而非MINIST~ 我之前就一直弄错了! 哈哈~


网上有很多使用MNIST数据集的教程,要么太麻烦,要么需要翻墙下载,很慢。 在这里分享一下我找到的最方便的方法


1 下载数据集并解压。

法1 从这个百度云链接下载

没有网络限制会比较快: 链接: https://pan.baidu.com/s/1ydcCsPBlP6U9Hw52TMnhkw 提取码: 8zjp

法2 (需要翻墙) 从MNIST官网中下载下图四个文件,下载速度很慢!在这里插入图片描述

下载的是4个.gz格式的压缩包
解压后得到四个新文件
之前的4个.gz压缩包就可以删除了
在这里插入图片描述


2 新建一个.py文件,起名为 input_data.py

将下面的代码复制粘贴进input_data.py中

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Functions for downloading and reading MNIST data."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os
import tempfile

import numpy
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

3 建立你的python项目,将input_data.py放在你的项目中

在这里插入图片描述


4 在你的python文件中(我的就是main.py,需要和input_data.py在头一个目录下)使用input_data

import tensorflow as tf
import input_data

# 下载并载入MNIST手写数据库
# 下面这句话的功能是使用input_data.py的内容自动下载数据集,如果数据集已经存在,则不用下载直接调用
mnist = input_data.read_data_sets(
MNIST是一个手写数字的数据集,其中包含了60000张训练图片和10000张测试图片。在MATLAB中,可以使用以下步骤来使用MNIST数据集。 1. 下载MNIST数据集 可以在Yann Lecun的网站上下载MNIST数据集,网址为:http://yann.lecun.com/exdb/mnist/ 下载数据集后,需要将其解压缩。 2. 读取MNIST数据集 MATLAB中可以使用`loadMNISTImages`和`loadMNISTLabels`函数来读取MNIST数据集。其中,`loadMNISTImages`函数用于读取MNIST图片数据,`loadMNISTLabels`函数用于读取MNIST标签数据。 ```matlab % 读取训练数据 train_images = loadMNISTImages('train-images.idx3-ubyte'); train_labels = loadMNISTLabels('train-labels.idx1-ubyte'); % 读取测试数据 test_images = loadMNISTImages('t10k-images.idx3-ubyte'); test_labels = loadMNISTLabels('t10k-labels.idx1-ubyte'); ``` 3. 可视化MNIST数据集 可以使用MATLAB中的`imshow`函数来可视化MNIST数据集中的图片。 ```matlab % 显示第一张训练图片 imshow(reshape(train_images(:, 1), [28, 28])); % 显示第一张测试图片 imshow(reshape(test_images(:, 1), [28, 28])); ``` 4. 使用MNIST数据集进行训练和测试 可以使用MATLAB中的机器学习工具箱来使用MNIST数据集进行训练和测试。例如,可以使用支持向量机(SVM)来训练和测试MNIST数据集。 ```matlab % 训练SVM分类器 svm_model = fitcecoc(train_images', train_labels); % 预测测试数据集 test_predictions = predict(svm_model, test_images'); % 计算准确率 accuracy = sum(test_predictions == test_labels) / length(test_labels); disp(['Accuracy: ', num2str(accuracy)]); ``` 以上是MATLAB中使用MNIST数据集的基本步骤,您可以根据需要进行调整和修改。
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值