文章目录
前言
创作开始时间:2021年4月3日16:41:32
如题。讲述MNIST定义,基础信息,相关代码,以及作图展示。
1、Tensorflow版本
- Tensorflow 2.4.0
- 还要pip install tensorflow_datasets (参考:https://*.com/questions/56920995/not-able-to-import-tensorflow-datasets-module-in-jupyter-notebook)
2、MNIST释义
MINST定义(全称):
The MNIST database (Modified National Institute of Standards and Technology database) is a large database of handwritten digits that is commonly used for training various image processing systems. The database is also widely used for training and testing in the field of machine learning.
MINST数据:
Most examples are using MNIST dataset of handwritten digits. The dataset contains 60,000 examples for training and 10,000 examples for testing. The digits have been size-normalized and centered in a fixed-size image (28x28 pixels) with values from 0 to 1. For simplicity, each image has been flatten and converted to a 1-D numpy array of 784 features (28*28).
网址:http://yann.lecun.com/exdb/mnist/
3、MNIST相关代码
建议直接看方法4,前面的都是一些简单的尝试
方法1:
import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
# 从tf.keras.datasets.mnist 来下载mnist,下载后的mnist.npz放在~/.keras 下面。
方法2:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
# 这里有个不好的地方是:tensorflow2没有这个模块:tensorflow.examples.tutorials.mnist,尴尬
方法3:(略坑)
import tensorflow as tf
import tensorflow_datasets
mnist = tensorflow_datasets.load('mnist')
#mnist = tensorflow_datasets.load('mnist',data_dir = "/home/apr/test/MNIST", download=False)
batch_X, batch_Y = mnist.train.next_batch(64)
# 这里是真的下载不下来,貌似需要改proxy,太麻烦。
推荐代码
方法4:(推荐)
import tensorflow as tf
import matplotlib.pyplot as plt
# load data
print("load data.")
(train_images,test_images),(train_labels,test_labels) = tf.keras.datasets.mnist.load_data()
# path = "/home/xxx/.keras/datasets/mnist.npz"
print("plot now.")
# init fig
n_row = 10
n_col = 10
# 子图初始化
fig, axes = plt.subplots(n_row, n_col, num = "show mnist", figsize = (13, 13))
# plt.figure(figsize=(n_col*1.4,n_row*1.6))
cnt = 0
for i in range(1, n_row + 1):
for j in range(1, n_col + 1):
# 绘制子图
plt.subplot(n_row, n_col, cnt + 1)
# 绘制
plt.imshow(train_images[cnt], cmap='binary')
cnt += 1
# plt.axis("off")
plt.show()
4、遇到的问题:tensorflow_dataset.load(‘mnist’)无法下载数据集
真的很坑。一直下载不下来。
网络问题很麻烦的,所以直接跳过了。
另外还有其他的问题:
- 有时候相同的代码,会得到不同的结果(比如方法4,运行几次都得不到结果,真是“运气游戏”?)。也可能是vscode的问题。代码应该没什么问题。
其他释义
- uint8
8位无符号整型 - Numpy arrays
Numpy 数组操作 https://www.runoob.com/numpy/numpy-array-manipulation.html
小结
渐渐明白,在赶时间的时候,真的还是要“不求甚解”。一个人不可能搞懂所有的问题,在有限的时间内,需要学会做出正确的选择。
创作结束时间:2021年4月4日14:15:05
参考文献
- https://*.com/questions/60296149/load-custom-dataset-like-mnist-tensorflow-python
- https://*.com/questions/64883267/tensorflow-load-vs-download-url
- https://*.com/questions/55475671/accessing-already-downloaded-dataset-with-tensorflow-datasets-api
- https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html
- https://github.com/tensorflow/datasets/blob/dd51a2d510bdcbf4498e9dcd2ee1ef33d44a13f3/tensorflow_datasets/core/download/downloader.py#L147
- https://*.com/questions/40368697/where-does-next-batch-in-the-tensorflow-tutorial-batch-xs-batch-ys-mnist-trai
- https://github.com/tensorflow/tensorflow/issues/32790
- https://blog.csdn.net/weixin_43364556/article/details/102892890
- 主要参考:博客园文章: TensorFlow加载mnist数据集并显示
- https://blog.csdn.net/itluojie/article/details/104898872
- https://blog.csdn.net/politeboy/article/details/107297271
- https://blog.csdn.net/weixin_44305115/article/details/110001384
- https://www.zhihu.com/question/362309074
- https://www.pythonf.cn/read/131633
- https://tensorflow.google.cn/install/pip#system-install