import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
2.6.0
sys.version_info(major=3, minor=9, micro=6, releaselevel='final', serial=0)
matplotlib 3.5.2
numpy 1.22.3
pandas 1.4.2
sklearn 1.1.1
tensorflow 2.6.0
keras.api._v2.keras 2.6.0
使用一个比较经典的数据fashion_mnist
fashion_mnist = keras.datasets.fashion_mnist
# 用load_data导入数据
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 将训练数据集分为验证集和训练集
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
# 打印数据集的格式
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 2us/step
40960/29515 [=========================================] - 0s 2us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 22s 1us/step
26435584/26421880 [==============================] - 22s 1us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
16384/5148 [===============================================================================================] - 0s 0s/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 3s 1us/step
4431872/4422102 [==============================] - 3s 1us/step
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
# 打印一张图片
def show_single_image(img_arr):
plt.imshow(img_arr, cmap="binary")
plt.show()
# 打印第一张图片
show_single_image(x_train[0])
# 打印一批照片
def show_imgs(n_rows, n_cols, x_data, y_data, class_names):
assert len(x_data) == len(y_data)
assert n_rows * n_cols < len(x_data)
plt.figure(figsize = (n_cols * 1.4, n_rows * 1.6))
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_data[index], cmap="binary",
interpolation = 'nearest')
plt.axis('off') # 把坐标系去掉
plt.title(class_names[y_data[index]]) # 打印标题
plt.show()
class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress',
'Coat', 'Sandal', 'Shirt', 'Sneaker',
'Bag', 'Ankle boot']
show_imgs(4, 5, x_train, y_train, class_names)
# tf.keras.model.Sequential()
'''
model = keras.models.Sequential()
# flatten 展平,把28*28的二维矩阵展平成28*28的一维向量
model.add(keras.layers.Flatten(input_shape=[28, 28]))
# Dense 全连接层
model.add(keras.layers.Dense(300, activation="relu"))
# Dense 全连接层, 100和之前的300全连接
model.add(keras.layers.Dense(100, activation="relu"))
# Dense 全连接层
model.add(keras.layers.Dense(10, activation="softmax"))
'''
model = keras.models.Sequential([
keras.layers.Flatten(input_shape=[28, 28]),
keras.layers.Dense(300, activation="relu"),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(10, activation="softmax")
])
# relu: y = max(0, x)
# softmax: 将向量编程概率分布 x = [x1, x2, x3]
# y = [e^x1/sum, e^x2/sum, e^x3/sum], sum = e^x1 + e^x2 + e^x3
# reason for sparse: y->index. y ->one_hot->[]
# 如果y是向量,就用categorical_crossentropy,如果不是,就用sparse_categorical_crossentropy
# optimizer 就是模型的求解方法
model.compile(loss="sparse_categorical_crossentropy",
optimizer = keras.optimizers.SGD(0.001),
metrics = ["accuracy"])
model.layers
[<keras.layers.core.Flatten at 0x2269afc98b0>,
<keras.layers.core.Dense at 0x2269f7fc070>,
<keras.layers.core.Dense at 0x2269f7fc3d0>,
<keras.layers.core.Dense at 0x2269f7f9f70>]
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) (None, 784) 0
_________________________________________________________________
dense_3 (Dense) (None, 300) 235500
_________________________________________________________________
dense_4 (Dense) (None, 100) 30100
_________________________________________________________________
dense_5 (Dense) (None, 10) 1010
=================================================================
Total params: 266,610
Trainable params: 266,610
Non-trainable params: 0
_________________________________________________________________
注:tf2较后来的版本默认batch_size=32,每一批取32个数据,(55000/32=1719)
history = model.fit(x_train, y_train, epochs=10,
validation_data=(x_valid, y_valid))
Epoch 1/10
1719/1719 [==============================] - 3s 1ms/step - loss: 1.8436 - accuracy: 0.7233 - val_loss: 0.6021 - val_accuracy: 0.7814
Epoch 2/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.5714 - accuracy: 0.7830 - val_loss: 0.5542 - val_accuracy: 0.7974
Epoch 3/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.5027 - accuracy: 0.8141 - val_loss: 0.5051 - val_accuracy: 0.8314
Epoch 4/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4594 - accuracy: 0.8316 - val_loss: 0.4948 - val_accuracy: 0.8258
Epoch 5/10
1719/1719 [==============================] - 4s 2ms/step - loss: 0.4294 - accuracy: 0.8417 - val_loss: 0.4561 - val_accuracy: 0.8432
Epoch 6/10
1719/1719 [==============================] - 3s 2ms/step - loss: 0.4054 - accuracy: 0.8513 - val_loss: 0.4567 - val_accuracy: 0.8398
Epoch 7/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3896 - accuracy: 0.8559 - val_loss: 0.4642 - val_accuracy: 0.8422
Epoch 8/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3750 - accuracy: 0.8627 - val_loss: 0.4647 - val_accuracy: 0.8432
Epoch 9/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3648 - accuracy: 0.8664 - val_loss: 0.4238 - val_accuracy: 0.8574
Epoch 10/10
1719/1719 [==============================] - 2s 1ms/step - loss: 0.3537 - accuracy: 0.8695 - val_loss: 0.4348 - val_accuracy: 0.8566
history.history
{'loss': [1.8436256647109985,
0.5714119076728821,
0.5026618242263794,
0.45940732955932617,
0.4294375479221344,
0.40538403391838074,
0.38960757851600647,
0.3750316798686981,
0.36476728320121765,
0.353736937046051],
'accuracy': [0.7233090996742249,
0.7829636335372925,
0.8141272664070129,
0.831636369228363,
0.8417090773582458,
0.8512908816337585,
0.8559454679489136,
0.8627272844314575,
0.866381824016571,
0.869490921497345],
'val_loss': [0.602108895778656,
0.5542056560516357,
0.5050806403160095,
0.49478277564048767,
0.4561069905757904,
0.4567115306854248,
0.464207261800766,
0.46472445130348206,
0.4237830638885498,
0.43475541472435],
'val_accuracy': [0.7814000248908997,
0.7973999977111816,
0.8313999772071838,
0.8258000016212463,
0.8432000279426575,
0.8398000001907349,
0.842199981212616,
0.8432000279426575,
0.8574000000953674,
0.8565999865531921]}
def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()
plot_learning_curves(history)
model.evaluate(x_test, y_test, verbose=0)
[0.47178930044174194, 0.8377000093460083]