上期我们介绍了
同样地,我们依旧通过实验来巩固我们刚刚所学的知识点。本次实验是基于Jupyer Notebook、Anaconda Python3.7与Keras环境。数据集是利用Minst手写体图像数据集。
5.3.1 代码
1. # chapter5/5_3_GAN.ipynb
2. import random
3. import numpy as np
4. from keras.layers import Input
5. from keras.layers.core import Reshape,Dense,Dropout,Activation,Flatten
6. from keras.layers.advanced_activations import LeakyReLU
7. from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, Deconv2D, UpSampling2D
8. from keras.regularizers import *
9. from keras.layers.normalization import *
10. from keras.optimizers import *
11. from keras.datasets import mnist
12. import matplotlib.pyplot as plt
13. from keras.models import Model
14. from tqdm import tqdm
15. from IPython import display
1. 读取数据集
1. img_rows, img_cols = 28, 28
2.
3. # 数据集的切分与混洗(shuffle)
4. (X_train, y_train), (X_test, y_test) = mnist.load_data()
5.
6. X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
7. X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
8. X_train = X_train.astype('float32')
9. X_test = X_test.astype('float32')
10. X_train /= 255
11. X_test /= 255
12.
13. print(np.min(X_train), np.max(X_train))
14. print('X_train shape:', X_train.shape)
15. print(X_train.shape[0], 'train samples')
16. print(X_test.shape[0], 'test samples')
0.0 1.0