TensorFlow 数据集归一化,测试结果

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)

print("min:", np.min(x_train), "max:", np.max(x_train))

# 4.数据归一化
# x = (x - u) / std
scaler = StandardScaler()

# x_train:[None, 28, 28] -> [None, 784] -> [None, 28, 28]
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 1))
x_train_scaled = x_train_scaled.reshape(-1, 28, 28)

x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1, 1))
x_valid_scaled = x_valid_scaled.reshape(-1, 28, 28)

x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1, 1))
x_test_scaled = x_test_scaled.reshape(-1, 28, 28)

print("min:", np.min(x_train_scaled), "max:", np.max(x_train_scaled))


def main():

    # 1.网络模型构建
    model = keras.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")
    ])

    # 2.编译模型
    # reason for sparse: y->index => y->one_hot->[]
    model.compile(loss="sparse_categorical_crossentropy",  # 损失函数
                  optimizer="sgd",                         # 模型优化方法
                  metrics=["accuracy"])                    # 指标 准确度

    # 3.打印模型
    model.summary()

    # 4.训练模型
    history = model.fit(x_train_scaled, y_train, epochs=10,
                        validation_data=(x_valid_scaled, y_valid))

    # 5.测试集
    model.evaluate(x_test_scaled, y_test)


if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值