import tensorflow as tf
import matplotlib.pyplot as plt
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data() #导入数据
plt.imshow(train_images[0])
#print(train_images[0])
#print(train_labels[0])
plt.show()
print(train_images.shape)
print(test_images.shape)
train_images = train_images/255.0
test_images = test_images/255.0
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation = tf.nn.relu),
tf.keras.layers.Dense(10,activation = tf.nn.softmax)])
model.compile(optimizer = 'Adam',loss='sparse_categorical_crossentropy')
model.fit(train_images,train_labels,epochs=5)
model.evaluate(test_images,test_labels)
如下是我的结果:
用shape测试过训练集是60000和验证集是10000,但最后训练数目和验证数目为1875和313,为原来的1/32,为啥????
先说训练集,这里需要引入batch_size的概念,意思就是每次训练的批次大小为多少,60000 = 1875 x 32(这个就是batch_size),意思就是每次训练的批次大小为32,那么60000大小的数据集,总共就是1875个批次
再说验证集,312 x 32(这个就是batch_size) + 16(最后一个批次的大小) = 10000,意思就是每次验证的批次大小为32,那么10000大小的数据集,总共就是312+1个批次,最后一个批次大小为16(tensorflow有两种选择,通过设置参数选择是否丢弃这个批次)