TensorFlow 构建模型与训练
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow_core.python import keras
from sklearn.preprocessing import StandardScaler
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
assert tf.__version__.startswith('2.')
# 0.打印导入模块的版本
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, sklearn, pd, tf, keras:
print("%s version:%s" % (module.__name__, module.__version__))
# 1.加载fashion_mnist数据集
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
# 2.拆分验证集, 训练集
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
# 3.打印 验证集, 训练集, 测试集
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)
# 4.数据集初始化
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(
x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_valid_scaled = scaler.transform(
x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
x_test_scaled = scaler.transform(
x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28, 28)
def plot_learning_curves(history):
"""显示学习过程图
:param history: 训练历史信息
"""
# 显示数据
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
# 显示Y轴范围
plt.gca().set_ylim(0, 1)
plt.show()
def main():
# 1.网络模型构建
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(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
# 2.编译模型
# reason for sparse: y->index => y->one_hot->[]
# sparse_categorical_crossentropy y->index 数据是索引模式
# categorical_crossentropy y->one_hot 数据是one_hot模式
model.compile(loss="sparse_categorical_crossentropy", # 损失函数
optimizer="adam", # 模型优化方法
metrics=["accuracy"]) # 指标 准确度
# 3.打印模型
print(model.layers)
model.summary()
# 4.训练模型
# [None, 784] * w + b ->[None, 100] w.shape[784, 300], b = [300]
history = model.fit(x_train_scaled, y_train, # 训练集数据
epochs=10, # 循环次数
validation_data=(x_valid_scaled, y_valid)) # 验证集数据
# 5.训练结果显示
print(type(history))
plot_learning_curves(history)
if __name__ == '__main__':
main()
学习过程显示结果
loss:损失函数结果
accuracy: 正确率
val_loss 验证损失函数结果
val_accuracy:验证集损失函数结果
显示图片
def show_images(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_images(3, 5, x_train, y_train, class_names)