基本分类(Fashion MNIST)
使用tf.keras
,是一种在TensorFlow中构建和训练模型的高阶api
本指南使用 Fashion MNIST 数据集,其中包含 70000 张灰度图像,涵盖 10 个类别。以下图像显示了单件服饰在较低分辨率(28x28 像素)下的效果:
主要接口解释
keras.datasets
自带的一些数据集
tf.nn.relu
使用relu激活函数
tf.nn.softmax
使用sotfmax激活函数
tf.train.AdamOptimizer()
使用AdamOptimizer 优化算法
keras.layers.Flatten
keras.layers.Dense
model = keras.Sequential([])
model.compile(optimizer = tf.train.AdamOptimizer(), loss='sparse_categorical_crossentropy', metrics = ['accuracy'])
-
这里 s p a r s e _ c a t e g o r i c a l _ c r o s s e n t r o p y sparse\_categorical\_crossentropy sparse_categorical_crossentropy 和 c a t e g o r i c a l _ c r o s s e n t r o p y categorical\_crossentropy categorical_crossentropy 的区别是:
如果你的Label是one-hot encoding的就用 c a t e g o r i c a l _ c r o s s e n t r o p y categorical\_crossentropy categorical_crossentropy;
如果Label是一个integer,就用 s p a r s e _ c a t e g o r i c a l _ c r o s s e n t r o p y sparse\_categorical\_crossentropy sparse_categorical_crossentropy
model.fit(train_images, train_labels, epochs = 5)
model.evaluate(test_images, test_labels)
img = (np.expand_dims(img,0))
- np.expand_dims的用法:
>>> x = np.array([1,2])
>>> x.shape
(2,)
>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1) # Equivalent to x[:,newaxis]
>>> y
array([[1],
[2]])
>>> y.shape
(2, 1)
predictions_single = model.predict(img)
可以一次性对样本批次或样本集进行预测。因此,即使我们使用单个图像,仍需要将其添加到列表中:
cross entropy
s o f t m a x softmax softmax函数最常用的损失函数就是交叉熵。
如何理解该函数呢?
首先我们先来了解一下信息熵的概念,其衡量的是一种”意外程度“,计算公式为:
H
(
x
)
=
−
∑
i
=
1
n
p
(
x
i
)
l
o
g
2
P
(
x
i
)
,
w
h
i
c
h
i
=
1
,
2
,
⋯
 
,
n
H(x)=-\sum_{i=1}^np(x_i)log_2P(x_i),\ which \ i=1,2,\cdots,n
H(x)=−i=1∑np(xi)log2P(xi), which i=1,2,⋯,n
举个例子来进一步说明,现在中美两国打乒乓球比赛,历史交手共64次,其中中国胜利63次。那这次中国胜利的信息量是
−
l
o
g
2
63
64
≈
0.023
-log_2{63 \over 64} \approx 0.023
−log26463≈0.023 而美国队是
−
l
o
g
2
1
64
=
6
-log_2{1\over 64} =6
−log2641=6 所以,比赛结果的信息熵为:
0.023
∗
63
64
+
6
∗
1
64
=
0.1164
0.023* {63\over64}+6* {1\over 64} =0.1164
0.023∗6463+6∗641=0.1164
这个熵还是比较小的,即结果蛮确定的,结果很意外的概率比较小!
…后面推导待补
完整代码
load_data.py
from tensorflow.python.keras.utils import get_file
import gzip
import numpy as np
def load_datas():
base = r"file:///C:/Users/your_username/Desktop/test_tensor/data/fashion-mnist/"
files = [
'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]
paths = []
for fname in files:
paths.append(get_file(fname, origin=base + fname))
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import load_data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = load_data.load_datas()
train_images = train_images / 255.0
test_images = test_images / 255.0
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# print(train_images.shape)
# plt.figure()
# plt.imshow(train_images[0])
# plt.colorbar()
# plt.grid(False)
# plt.figure(figsize=(10,10))
# for i in range(25):
# plt.subplot(5,5,i+1)
# plt.xticks([])
# plt.yticks([])
# plt.grid(False)
# plt.imshow(train_images[i], cmap=plt.cm.binary)
# plt.xlabel(class_names[train_labels[i]])
# plt.show()
model = keras.Sequential([
keras.layers.Flatten(input_shape = (28, 28)),
keras.layers.Dense(100, activation = tf.nn.relu),
keras.layers.Dense(10, activation = tf.nn.softmax)
])
model.compile(optimizer = tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(train_images, train_labels, epochs = 5)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('\nTest accuracy:', test_acc)
predictions = model.predict(test_images)
print(predictions[0],np.argmax(predictions[0]),test_labels[0])
img = test_images[0]
print(img.shape)
img = (np.expand_dims(img,0))
print(img.shape)
predictions_single = model.predict(img)
print(predictions_single)