MNIST 数据集入门
MNIST 数据集简介
数字手写体识别数据集,常用来作为Deep Learning入门的基础数据集。它有60000
个训练样本集和10000
个测试样本集,每个样本图像的宽高为 28 * 28
。此数据集是以二进制存储的,不能直接以图像格式查看。
数据集大小:~12MB
下载地址:http://yann.lecun.com/exdb/mnist/index.html
1 | import tensorflow as tf |
1.6.0
tensorflow加载MNIST数据集
1 | # Import MNIST |
train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)
Extracting ../../data/train-images-idx3-ubyte.gz
Extracting ../../data/train-labels-idx1-ubyte.gz
Extracting ../../data/t10k-images-idx3-ubyte.gz
Extracting ../../data/t10k-labels-idx1-ubyte.gz
查看并可视化MNIST数据集
1 | import matplotlib.pyplot as plt |
<built-in method astype of numpy.ndarray object at 0x0000016EEF769080> (64, 784)
<built-in method astype of numpy.ndarray object at 0x0000016EEF772620> (64, 10)
[ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
<matplotlib.image.AxesImage at 0x16eef82a630>
tensorflow入门(hello world)
1 | import tensorflow as tf |
1 | # TensorFlow 实现简单的 hello world |
1 | # 启动 tf session |
1 | # 运行图 |
b'Hello, TensorFlow!'
tensorflow入门(基本操作)
常量操作
1 | # 常量基本操作 |
1 | # 启动默认的图 |
a: 2 b: 3
Addition with constants: 5
Multiplication with constants: 6
变量操作
1 | # 作为图输入变量的基本操作。 |
1 | # tf 中定义的操作 |
1 | # 启动默认的图 |
Addition with variables: 5
Multiplication with variables: 6
矩阵操作
1 | # 创建一个生成1x2矩阵的常数op。 |
1 | # tf 中定义的操作 |
1 | with tf.Session() as sess: |
[[ 12.]]
tensorflow入门(Eager API)
1 | from __future__ import absolute_import, division, print_function |
1 | # 设置 Eager API |
Setting Eager mode...
Eager API 常量操作
1 | # 定义常量 tensors |
Define constant tensors
a = 2
b = 3
1 | # 执行操作不需要 tf.Session |
Running operations, without tf.Session
a + b = 5
a * b = 6
Eager API 张量操作
1 | # 与 Numpy完全兼容 |
Mixing operations with Tensors and Numpy Arrays
Tensor:
a = tf.Tensor(
[[2. 1.]
[1. 0.]], shape=(2, 2), dtype=float32)
NumpyArray:
b = [[3. 0.]
[5. 1.]]
1 | # 在不需要 tf.Session 的情况下运行该操作 |
Running operations, without tf.Session
a + b = tf.Tensor(
[[5. 1.]
[6. 1.]], shape=(2, 2), dtype=float32)
a * b = tf.Tensor(
[[11. 1.]
[ 3. 0.]], shape=(2, 2), dtype=float32)
1 | # 遍历张量 |
Iterate through Tensor 'a':
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0.0, shape=(), dtype=float32)