tensorflow入门(6):认识MNIST数据集(读取数据,关于reshape,可视化image,认识label,独热编码,argmax,批量读取)

MNIST数据集的下载及读取,划分训练集、验证集、测试集。

关于reshape。

可视化image。

认识label,关于One Hot独热编码,argmax函数返回最大数的索引。

批量读取多条数据。

 

详细如下:

# -*- coding: utf-8 -*-
"""
Created on Mon May 11 09:11:59 2020

@author: DELL
"""

# MNIST 数据集可在 http://yann.lecun.com/exdb/mnist/ 获取
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNST_data/",one_hot=True)
# 该站点上有四个文件:
# train-images-idx3-ubyte.gz:训练集图像(9912422字节)
# train-labels-idx1-ubyte.gz:训练集标签(28881 字节)
# t10k-images-idx3-ubyte.gz:测试集图像(1648877字节)
# t10k-labels-idx1-ubyte.gz:测试集标签(4542字节)


# 了解MNIST手写数字识别数据集
print('训练集 train 数量:',mnist.train.num_examples,
      '验证集 validation 数量',mnist.validation.num_examples,
      '测试集 test 数量',mnist.test.num_examples)
# 训练集 train 数量: 55000 验证集 validation 数量 5000 测试集 test 数量 10000

# 查看train data
print('train images shape:', mnist.train.images.shape,
      'labels shape',mnist.train.labels.shape)
# train images shape: (55000, 784) labels shape (55000, 10)
# Remark: 28*28=784, 10分类 One Hot编码
len(mnist.train.images[0])          # 784
mnist.train.images[0].shape         # (784,)
mnist.train.images[0]               # array([ ,..., ],dtype=float32)
mnist.train.images[0].reshape(28,28)# array([[ ,..., ],...,[ ,..., ]],dtype=float32)

# 读取test data
print('test images shape:',mnist.test.images.shape,
      'labels shape:',mnist.test.labels.shape)
# test images shape: (10000, 784) labels shape: (10000, 10)


# 关于reshape()
import numpy as np
int_array = np.array([i for i in range(64)])
print(int_array)
"""
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
"""
int_array.reshape(8,8)
"""
 array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])
"""

# 可视化 image
import matplotlib.pyplot as plt
def plot_image(image):
    plt.imshow(image.reshape(28,28),cmap='binary')
    plt.show()
# 尝试作图
plt.imshow(mnist.train.images[20000].reshape(14,56),cmap='binary')
plt.show()

 

# 认识标签 label
mnist.train.labels[1]           # array([0., 0., 0., 1., 0., 0., 0., 0., 0., 0.])
mnist.train.labels[1].shape     # (10,)
# 独热编码取值
np.argmax(mnist.train.labels[1])# 3
# argmax返回的是最大数的索引

# 非one hot 编码的标签值
mnist_no_one_hot = input_data.read_data_sets("MNIST_data/",one_hot=False)
print(mnist_no_one_hot.train.labels[0:10])
# [7 3 4 6 1 8 1 0 9 8]


# argmax()
arr1 = np.array([1,3,2,5,7,0])
arr2 = np.array([[1.0,2,3],[3,2,1],[4,7,2],[8,3,2]])
print("arr1=",arr1)
# arr1= [1 3 2 5 7 0]
print("arr2=\n",arr2)
"""
arr2=
 [[1. 2. 3.]
 [3. 2. 1.]
 [4. 7. 2.]
 [8. 3. 2.]]
"""
argmax_1 = tf.argmax(arr1)
argmax_20 = tf.argmax(arr2,0)   # 取第一维元素值(行),即同列的每一行
argmax_21 = tf.argmax(arr2,1)   # 取第二维元素值(列),即同行的每一列
argmax_22 = tf.argmax(arr2,-1)  # 取最后维元素值

with tf.Session() as sess:
    print(argmax_1.eval())      # 4
    print(argmax_20.eval())     # [3 2 0]
    print(argmax_21.eval())     # [2 0 1 0]
    print(argmax_22.eval())     # [2 0 1 0]


# 一次批量读取多条数据
batch_images_xs, batch_labels_ys = mnist.train.next_batch(batch_size=10)
print(batch_images_xs.shape, batch_labels_ys.shape)
# (10, 784) (10, 10)
print(mnist.train.labels[0:10])
"""
[[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]]
"""
print(batch_labels_ys)
"""
[[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]]
"""
batch_images_xs, batch_labels_ys = mnist.train.next_batch(batch_size=10)
print(batch_labels_ys)
"""
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
 [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
"""

 

 

以上。参考Mooc课程吴明晖老师的《深度学习应用开发-TensorFlow实践》。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值