我自己实战练习应用的IDE是Jupyter-notebook,由于我的博客无法很好的直接上传展示Jupyter,所以我会将代码和输出情况拆分来展示:
import sys
assert sys.version_info >= (3, 5)
import sklearn
assert sklearn.__version__ >= "0.20"
try:
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
assert tf.__version__ >= "2.0"
import numpy as np
import os
np.random.seed(42)
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "ann"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID)
os.makedirs(IMAGES_PATH, exist_ok=True)
def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300):
path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension)
print("Saving figure", fig_id)
if tight_layout:
plt.tight_layout()
plt.savefig(path, format=fig_extension, dpi=resolution)
import tensorflow as tf
from tensorflow import keras
tf.__version__
keras.__version__
【输出】:
'2.0.0'
'2.2.4-tf'
fashion_mnist = keras.datasets.fashion_mnist # 加载数据集
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data() # 加载训练集 测试集
【说明】:正常这个位置工作量很大,由于这个数据集已经处理的差不多,所以不用再怎么处理了。
# 查看训练集相关的性质
print(X_train_full.shape)
print(X_train_full.dtype)
【输出】:
(60000, 28, 28)
uint8
# 分验证0-5000集与测试集5000-60000
# 除以255为了归一化,图片取值范围0-255,归一化
X_valid, X_train = X_train_full[:5000] / 255., X_train_full[5000:] / 255.
y_valid, y_train = y_train_full[:5000], y_train_full[5000:] # 分割验证集和测试集
X_test = X_test / 255. # 测试集归一化
# 查看训练集中第一张图片
plt.imshow(X_train[0], cmap="binary")
plt.axis('off')
plt.show()
y_train # 查看标签:0-9的数字对于0-9的类别
【输出】:
array([4, 0, 7, ..., 3, 0, 5], dtype=uint8)
class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
class_names[y_train[0]]
【输出】:
'Coat'
print(X_valid.shape) # 验证集大小
print(X_test.shape) # 测试集大小
【输出】:
(5000, 28, 28)
(10000, 28, 28)
# 查看训练集前40张图片
n_rows = 4
n_cols = 10
plt.figure(figsize=(n_cols * 1.2, n_rows * 1.2))
for row in range(n_rows):
for col in range(n_cols):
index = n_cols * row + col
plt.subplot(n_rows, n_cols, index + 1)
plt.imshow(X_train[index], cmap="binary", interpolation="nearest")
plt.axis('off')
plt.title(class_names[y_train[index]], fontsize=12)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
plt.show()
model = keras.models.Sequential() # 建一个序列
model.add(keras.layers.Flatten(input_shape=[28, 28])) # 往序列里加一个层,第一个层展开
model.add(keras.layers.Dense(300, activation="relu")) # 加一个Dense层
model.add(keras.layers.Dense(100, activation="relu")) # 加一个Dense层
model.add(keras.layers.Dense(10, activation="softmax"))# 再加一个Dense层,分类问题:最后一层激活函数:softmax
model.summary() # 查看模型结构
【输出】:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
_________________________________________________________________
dense (Dense) (None, 300) 235500
_________________________________________________________________
dense_1 (Dense) (None, 100) 30100
_________________________________________________________________
dense_2 (Dense) (None, 10) 1010
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
hidden1 = model.layers[1] # 访问第一层
hidden1.name
【输出】:
'dense'
weights, biases = hidden1.get_weights() # 查看第一层的权重
weights
weights.shape
biases.shape
【输出】:
array([[-0.07419343, -0.06533033, -0.07415544, ..., -0.06727954,
-0.04447283, 0.01043332],
[ 0.0559309 , 0.00454898, -0.01323185, ..., 0.07062382,
0.03942343, -0.02208545],
[ 0.06476399, -0.00898749, 0.04943166, ..., -0.01289836,
-0.03373614, -0.06176911],
...,
[-0.04891405, -0.0484167 , 0.01653042, ..., -0.0545395 ,
-0.05972821, -0.00235039],
[ 0.06061363, -0.0555383 , 0.00755734, ..., -0.05427159,
0.05316937, -0.05920539],
[-0.02689213, -0.00029842, -0.03498013, ..., -0.00165496,
0.00435399, 0.0532587 ]], dtype=float32)
(784, 300)
(300,)
model.compile(loss="sparse_categorical_crossentropy",
optimizer="sgd",
metrics=["accuracy"])
history = model.fit(X_train, y_train, epochs=30,
validation_data=(X_valid, y_valid))
【输出(一部分)】:
Train on 55000 samples, validate on 5000 samples
Epoch 1/30
55000/55000 [==============================] - 5s 89us/sample - loss: 0.7252 - accuracy: 0.7631 - val_loss: 0.5020 - val_accuracy: 0.8362
Epoch 2/30
55000/55000 [==============================] - 3s 63us/sample - loss: 0.4889 - accuracy: 0.8293 - val_loss: 0.4353 - val_accuracy: 0.8548
Epoch 3/30
55000/55000 [==============================] - 3s 59us/sample - loss: 0.4441 - accuracy: 0.8433 - val_loss: 0.4151 - val_accuracy: 0.8616
Epoch 4/30
55000/55000 [==============================] - 3s 62us/sample - loss: 0.4172 - accuracy: 0.8540 - val_loss: 0.4007 - val_accuracy: 0.8626
.................................................................
Epoch 28/30
55000/55000 [==============================] - 3s 57us/sample - loss: 0.2361 - accuracy: 0.9155 - val_loss: 0.3030 - val_accuracy: 0.8956
Epoch 29/30
55000/55000 [==============================] - 3s 56us/sample - loss: 0.2321 - accuracy: 0.9168 - val_loss: 0.3291 - val_accuracy: 0.8814
Epoch 30/30
55000/55000 [==============================] - 3s 59us/sample - loss: 0.2286 - accuracy: 0.9171 - val_loss: 0.2886 - val_accuracy: 0.8950
history.params # 神经网络的参数
【输出】:
{'batch_size': 32,
'epochs': 30,
'steps': 1719,
'samples': 55000,
'verbose': 0,
'do_validation': True,
'metrics': ['loss', 'accuracy', 'val_loss', 'val_accuracy']}
import pandas as pd
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()
model.evaluate(X_test, y_test)
```s
【输出(一部分)】:
```python
10000/1 [ ========================== ] 0s 30us/sample - loss: 0.2128 - accuracy: 0.8854
[0.32216803691387175, 0.8854]
X_new = X_test[:3]
y_proba = model.predict(X_new)
y_proba.round(2)
【输出】:分别代表概率情况
array([[0. , 0. , 0. , 0. , 0. , 0.01, 0. , 0.01, 0. , 0.98],
[0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]],
dtype=float32)
y_pred = model.predict_classes(X_new) # 测试集预测答案“数字编号”
y_pred
np.array(class_names)[y_pred] # “数字编号”对应的名称
y_new = y_test[:3] # 测试集实际答案“数字编号”
y_new
【输出】:
array([9, 2, 1], dtype=int64)
array(['Ankle boot', 'Pullover', 'Trouser'], dtype='<U11')
array([9, 2, 1], dtype=uint8)
# 打印预测测试集的这3张图片
plt.figure(figsize=(7.2, 2.4))
for index, image in enumerate(X_new):
plt.subplot(1, 3, index + 1)
plt.imshow(image, cmap="binary", interpolation="nearest")
plt.axis('off')
plt.title(class_names[y_test[index]], fontsize=12)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
plt.show()
评论