tensorflow2 使用批归一化与dropout

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras as keras
from sklearn.preprocessing import StandardScaler

fashsion_minist = tf.keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashsion_minist.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:]

scaler = StandardScaler()

# todo 搞一下这个reshape
x_train_scaler = 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 show_single_image(image_arr):
    plt.imshow(image_arr)
    plt.show()


model = keras.models.Sequential()

model.add(keras.layers.Flatten(input_shape=[28, 28]))

for _ in range(20):
    model.add(keras.layers.Dense(100, activation='relu'))
    model.add(keras.layers.BatchNormalization())

# dropout 一般在全连接层后设置
model.add(keras.layers.AlphaDropout(rate=0.5))
# AlphaDropout: 1. 均值和方差不变 2. 归一化性质也不变  更好用
# model.add(keras.layers.Dropout(rate=0.5))
model.add(keras.layers.Dense(10, activation='softmax'))

keras.optimizers.Adam()
model.compile(loss="sparse_categorical_crossentropy",
              optimizer="sgd",
              metrics=["accuracy"])

model.summary()
history = model.fit(x_train, y_train, batch_size=256, epochs=10, validation_data=(x_valid, y_valid))
print(history.history)


def plot_learning_curves(history):
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.grid(True)
    plt.ylim(0, 1)
    plt.show()


plot_learning_curves(history)
model.evaluate(x_test, y_test)

上一篇:12号主机tensorflow2环境


下一篇:ubuntu20.04下安装cuda11,cudn以及tensorflow2-GPU版本