上期我们介绍了
生成对抗网络 | 原理及训练过程
同样地,我们依旧通过实验来巩固我们刚刚所学的知识点。本次实验是基于Jupyer Notebook、Anaconda Python3.7与Keras环境。数据集是利用Minst手写体图像数据集。
![8be2d3f8adbe30b8c5356f27fcd465c7.png](https://i-blog.csdnimg.cn/blog_migrate/7c0e8db799b97d895499dc38e242bfdb.png)
1. # chapter5/5_3_GAN.ipynb2. 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
X_train shape: (60000, 1, 28,28)
60000 train samples
10000 test samples
2. 超参数设置1. shp = X_train.shape[1:] 2. dropout_rate = 0.25 3. 4. # 优化器 5. opt = Adam(lr=1e-4)