tensorflow中mnist数据集
tensorflow中mnist数据集
mnist数据集一共有7万张图片,是28 * 28 像素的0 到 9 手写识别数据集,其中6万张用于训练,1万张用于测试。每张图片包含784(28 * 28)个像素点,使用全连接网络可以将784个像素点组成长度为784的一维数组,作为输入特征。
导入数据集:
方式一:
mnist = tf.keras.datasets.mnist #导入mnist数据集 (x_train, y_train), (x_test, y_test) = mnist.load_data() #分别分配好训练集和测试集的输入和标签方式二:
1、下载数据集:https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
2、导入数据:
path = './mnist.npz' #数据路径 f = np.load(path) #加载数据 x_train, y_train = f['x_train'], f['y_train'] #导入训练集的输入和标签 x_test, y_test = f['x_test'], f['y_test'] #导入测试集的输入和标签 f.close()
实例代码:
import tensorflow as tf #导入模块
from matplotlib import pyplot as plt #导入绘图模块
import numpy as np
#加载数据集
# mnist = tf.keras.datasets.mnist #导入mnist数据集
# (x_train, y_train), (x_test, y_test) = mnist.load_data() #分别分配好训练集和测试集的输入和标签
path = './mnist.npz' #数据路径
f = np.load(path) #加载数据
x_train, y_train = f['x_train'], f['y_train'] #导入训练集的输入和标签
x_test, y_test = f['x_test'], f['y_test'] #导入测试集的输入和标签
f.close()
#可视化训练集输入特征的第一个元素
plt.imshow(x_train[0], cmap = "gray") #绘制灰度图
plt.show() #画出图像
#打印出训练集输入特征的第一个元素
print("x_train[0]:\n", x_train[0])
#打印出训练集标签的第一个元素
print("y_train[0]:\n", y_train[0])
#打印出整个训练集输入特征的形状
print("x_train.shape:\n", x_train.shape)
#打印出整个训练集标签的形状
print("y_train.shape:\n", y_train.shape)
#打印出整个测试集输入特征的形状
print("x_test.shape:\n", x_test.shape)
#打印出整个测试集标签的形状
print("y_test.shape:\n", y_test.shape)
结果为:
E:\Anaconda3\envs\TF2\python.exe C:/Users/Administrator/PycharmProjects/untitled8/Mnist数据集.py
x_train[0]:
[[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 3 18 18 18 126 136
175 26 166 255 247 127 0 0 0 0]
[ 0 0 0 0 0 0 0 0 30 36 94 154 170 253 253 253 253 253
225 172 253 242 195 64 0 0 0 0]
[ 0 0 0 0 0 0 0 49 238 253 253 253 253 253 253 253 253 251
93 82 82 56 39 0 0 0 0 0]
[ 0 0 0 0 0 0 0 18 219 253 253 253 253 253 198 182 247 241
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 80 156 107 253 253 205 11 0 43 154
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 14 1 154 253 90 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 139 253 190 2 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 11 190 253 70 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 35 241 225 160 108 1
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 81 240 253 253 119
25 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45 186 253 253
150 27 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 93 252
253 187 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 249
253 249 64 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 46 130 183 253
253 207 2 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 39 148 229 253 253 253
250 182 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 24 114 221 253 253 253 253 201
78 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 23 66 213 253 253 253 253 198 81 2
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 18 171 219 253 253 253 253 195 80 9 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 55 172 226 253 253 253 253 244 133 11 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 136 253 253 253 212 135 132 16 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]]
y_train[0]:
5
x_train.shape:
(60000, 28, 28)
y_train.shape:
(60000,)
x_test.shape:
(10000, 28, 28)
y_test.shape:
(10000,)Process finished with exit code 0
上一篇: 直接最小二乘法拟合椭圆