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
    评论
### 回答1: TensorFlow可以通过以下代码读取MNIST数据集: ```python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 读取MNIST数据集 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 打印训练集、验证集和测试集的大小 print("训练集大小:", mnist.train.num_examples) print("验证集大小:", mnist.validation.num_examples) print("测试集大小:", mnist.test.num_examples) # 打印训练集中第一个样本的标签和像素值 print("训练集第一个样本的标签:", mnist.train.labels[0]) print("训练集第一个样本的像素值:", mnist.train.images[0]) ``` 其中,`input_data.read_data_sets()`函数会自动下载MNIST数据集并将其存储在指定的文件夹中。`one_hot=True`表示将标签转换为one-hot编码。训练集、验证集和测试集分别存储在`mnist.train`、`mnist.validation`和`mnist.test`中。每个样本的标签存储在`labels`中,像素值存储在`images`中。 ### 回答2: TensorFlow是一个用于构建和训练机器学习模型的强大框架。在机器学习中,MNIST数据集是一个广泛使用的手写数字识别任务。TensorFlow提供了一个方便的API,可以用于读取MNIST数据集。 首先,要使用MNIST数据集,需要从TensorFlow的datasets模块中导入它: ```python from tensorflow.keras.datasets import mnist ``` 然后,我们可以使用load_data()方法来下载并读取MNIST数据集: ```python (x_train, y_train), (x_test, y_test) = mnist.load_data() ``` 上述代码将会下载MNIST数据集,分别读取训练和测试数据,将其分别存储在x_train、y_train、x_test和y_test四个变量中。 其中,x_train和x_test变量包含手写数字图像的像素值,每个图像由28x28个像素组成。y_train和y_test变量则包含相应图像的标签,即手写数字的真实值。 在读取MNIST数据集后,我们可以使用matplotlib等图形库来显示和可视化数据集。 例如,可以使用下面的代码显示MNIST数据集中的第一个训练样本: ```python import matplotlib.pyplot as plt plt.imshow(x_train[0], cmap='gray') plt.show() ``` 除了使用预先定义的MNIST数据集TensorFlow还提供了灵活的API,可以读取自定义的数据集。可以使用tf.data工具包或者直接从存储在磁盘上的文件中读取数据。 总之,TensorFlow提供了非常简单和灵活的API,可以方便地读取MNIST数据集。这使得开发者可以专注于模型的构建和训练,而不必花费太多时间和精力处理数据读取的问题。 ### 回答3: TensorFlow是一种非常强大的机器学习框架,它可以方便地实现各种模型,包括深度神经网络。MNIST是一个手写数字的数据集,它由6万张图片组成,其中5万张是训练集,1万张是测试集。在TensorFlow中,读取MNIST数据集非常简单,只需按照以下步骤操作即可。 首先,我们需要导入必要的库,包括TensorFlow本身和numpy。Numpy是Python中的一个常用数学库,可以方便地处理数组和矩阵。 ```python import tensorflow as tf import numpy as np ``` 接下来,我们可以从TensorFlow内置的数据集中加载MNIST数据集TensorFlow提供了一个方便的API来自动下载和管理MNIST数据集,我们只需调用一行代码即可。 ```python mnist = tf.keras.datasets.mnist ``` 接下来,我们可以将训练集和测试集分别加载到内存中,使用`load_data()`方法即可。此时,训练集和测试集将被存储为numpy数组。 ```python (train_images, train_labels), (test_images, test_labels) = mnist.load_data() ``` 最后,我们需要将数据集转换为TensorFlow中的张量。由于MNIST数据集是28x28的灰度图像,每个像素的灰度值介于0和255之间,我们需要进行一些数据预处理才能将其作为输入传递给神经网络模型。 ```python # 将图片灰度值缩小为0到1之间,并将其转换为浮点张量 train_images = train_images / 255.0 test_images = test_images / 255.0 # 添加一个维度作为通道维,使每个图像的形状为(28, 28, 1) train_images = np.expand_dims(train_images, axis=-1) test_images = np.expand_dims(test_images, axis=-1) # 将标签转换为独热编码 train_labels = tf.keras.utils.to_categorical(train_labels, num_classes=10) test_labels = tf.keras.utils.to_categorical(test_labels, num_classes=10) # 将numpy数组转换为TensorFlow张量 train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)) test_dataset = tf.data.Dataset.from_tensor_slices((test_images, test_labels)) ``` 现在,我们已经成功地将MNIST数据集加载到了TensorFlow中,并将其转换为可以用于训练和测试模型的张量。我们可以像任何其他TensorFlow数据集一样使用这些数据集,如构建迭代器或批处理数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值