├── .DS_Store ├── README.md ├── autoencoder.pdf ├── autoencoder ├── autoencoder.py ├── conv_autoencoder.py ├── denoising_autoencoder.py ├── sparse_autoencoder.py └── stack_autoencoder.py └── imgs ├── ae_encoder_result.png ├── ae_generate_comparison.png ├── ae_structure.png ├── convAe_accuracy.png ├── convAe_generate_comparison.png ├── convAe_loss.png ├── convAe_structure.png ├── denoisingAe_encoder_result.png ├── denoisingAe_generate_comparison.png ├── denoisingAe_structure.png ├── noising_data.png ├── sparseAe_encoder_result.png ├── sparseAe_generate_comparison.png ├── sparseAe_structure.png ├── stackAe_encoder_result.png ├── stackAe_generate_comparison.png └── stackAe_structure.png /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Autoencoder 2 | 主要包括自编码器及其变形的理论+实践。 3 | 4 | ## PDF整理 5 | PDF来源于本人的理解+整理,部分图片来源于网上,已有标注,PDF对应博客详见:https://blog.csdn.net/quiet_girl/article/details/84401029 。 6 | 7 | 因时间原因,代码中epoch设置的较小,实际状况下,肯定要更大。 8 | 9 | ## 主要内容 10 | 暂时代码包括普通自编码器(Autoencoder.py)、栈式自编码器(StackAutoencoder)、稀疏自编码器(SparseAutoencoder.py)和去噪自编码器(DenoisingAutoencoder.py)的简单实现,代码每一步都有注释。 11 | 12 | 关于收缩自编码器、变分自编码器、CNN自编码器等后更。 13 | 14 | - 基于框架:Keras2.0.4 15 | - 数据集:Mnist 16 | 17 | 具体设置等请参见代码或者博客 18 | 19 | ## 代码运行结果: 20 | 21 | ### 1、普通自编码器: 22 | 23 | - 简单自动编码器架构图 24 |
自动编码器架构图
25 | 26 | - Encoder层输出结果可视化 27 |
自动编码器Encoder层输出结果可视化
28 | 29 | - Autoencoder生成图片和原图片对比 30 |
自动编码器生成图片和原图片对比
31 | 32 | ### 2、栈式自编码器: 33 | 34 | - 栈式自动编码器架构图 35 |
栈式自动编码器架构图
36 | 37 | - Encoder层输出结果可视化 38 |
栈式自动编码器Encoder层输出结果可视化
39 | 40 | - Stack Autoencoder生成图片和原图片对比 41 |
栈式自动编码器生成图片和原图片对比
42 | 43 | ### 3、稀疏自编码器: 44 | 45 | - 稀疏自动编码器架构图 46 |
稀疏自动编码器架构图
47 | 48 | - Encoder层输出结果可视化 49 |
栈式自动编码器Encoder层输出结果可视化
50 | 51 | - Sparse Autoencoder生成图片和原图片对比 52 |
栈式自动编码器生成图片和原图片对比
53 | 54 | ### 4、去噪自编码器: 55 | 56 | - 去噪自动编码器架构图 57 |
栈式自动编码器架构图
58 | 59 | - Encoder层输出结果可视化 60 |
栈式自动编码器Encoder层输出结果可视化
61 | 62 | - Denoising Autoencoder原图片和添加噪声后图片对比 63 |
栈式自动编码器原图片和添加噪声后图片对比
64 | 65 | - Denoising Autoencoder生成图片和原图片对比 66 |
栈式自动编码器生成图片和原图片对比
67 | 68 | ### 5、卷积自编码器: 69 | 70 | - 卷积自动编码器架构图 71 |
卷积自动编码器架构图
72 | 73 | - Convolutional Autoencoder生成图片和原图片对比 74 |
卷积自动编码器生成图片和原图片对比
75 | 76 | - Convolutional Autoencoder训练accuracy和loss变化图 77 |
78 | 卷积自动编码器accuracy变化 79 | 卷积自动编码器loss变化 80 |
81 | 82 | -------------------------------------------------------------------------------- /autoencoder.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/autoencoder.pdf -------------------------------------------------------------------------------- /autoencoder/autoencoder.py: -------------------------------------------------------------------------------- 1 | # python3 2 | # -*- coding: utf-8 -*- 3 | # @Author : lina 4 | # @Time : 2018/11/23 13:05 5 | """ 6 | Autoencoder with single hidden layer. 7 | """ 8 | import numpy as np 9 | 10 | from keras.datasets import mnist 11 | from keras.models import Model 12 | from keras.layers import Dense, Input 13 | import matplotlib.pyplot as plt 14 | 15 | np.random.seed(33) # random seed,to reproduce results. 16 | 17 | ENCODING_DIM_INPUT = 784 18 | ENCODING_DIM_OUTPUT = 2 19 | EPOCHS = 20 20 | BATCH_SIZE = 64 21 | 22 | def train(x_train): 23 | """ 24 | build autoencoder. 25 | :param x_train: the train data 26 | :return: encoder and decoder 27 | """ 28 | # input placeholder 29 | input_image = Input(shape=(ENCODING_DIM_INPUT, )) 30 | 31 | # encoding layer 32 | hidden_layer = Dense(ENCODING_DIM_OUTPUT, activation='relu')(input_image) 33 | # decoding layer 34 | decode_output = Dense(ENCODING_DIM_INPUT, activation='relu')(hidden_layer) 35 | 36 | # build autoencoder, encoder, decoder 37 | autoencoder = Model(inputs=input_image, outputs=decode_output) 38 | encoder = Model(inputs=input_image, outputs=hidden_layer) 39 | 40 | # compile autoencoder 41 | autoencoder.compile(optimizer='adam', loss='mse') 42 | 43 | # training 44 | autoencoder.fit(x_train, x_train, epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True) 45 | 46 | return encoder, autoencoder 47 | 48 | def plot_representation(encode_images, y_test): 49 | """ 50 | plot the hidden result. 51 | :param encode_images: the images after encoding 52 | :param y_test: the label. 53 | :return: 54 | """ 55 | # test and plot 56 | plt.scatter(encode_images[:, 0], encode_images[:, 1], c=y_test, s=3) 57 | plt.colorbar() 58 | plt.show() 59 | 60 | def show_images(decode_images, x_test): 61 | """ 62 | plot the images. 63 | :param decode_images: the images after decoding 64 | :param x_test: testing data 65 | :return: 66 | """ 67 | n = 10 68 | plt.figure(figsize=(20, 4)) 69 | for i in range(n): 70 | ax = plt.subplot(2, n, i+1) 71 | ax.imshow(x_test[i].reshape(28, 28)) 72 | plt.gray() 73 | ax.get_xaxis().set_visible(False) 74 | ax.get_yaxis().set_visible(False) 75 | 76 | ax = plt.subplot(2, n, i + 1 + n) 77 | ax.imshow(decode_images[i].reshape(28, 28)) 78 | plt.gray() 79 | ax.get_xaxis().set_visible(False) 80 | ax.get_yaxis().set_visible(False) 81 | plt.show() 82 | 83 | if __name__ == '__main__': 84 | # Step1: load data x_train: (60000, 28, 28), y_train: (60000,) x_test: (10000, 28, 28), y_test: (10000,) 85 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 86 | 87 | # Step2: normalize 88 | x_train = x_train.astype('float32') / 255. 89 | x_test = x_test.astype('float32') / 255. 90 | 91 | # Step3: reshape data, x_train: (60000, 784), x_test: (10000, 784), one row denotes one sample. 92 | x_train = x_train.reshape((x_train.shape[0], -1)) 93 | x_test = x_test.reshape((x_test.shape[0], -1)) 94 | 95 | # Step4: train 96 | encoder, autoencoder = train(x_train=x_train) 97 | 98 | # test and plot 99 | encode_images = encoder.predict(x_test) 100 | plot_representation(encode_images, y_test) 101 | 102 | # show images 103 | decode_images = autoencoder.predict(x_test) 104 | show_images(decode_images, x_test) 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /autoencoder/conv_autoencoder.py: -------------------------------------------------------------------------------- 1 | # python3 2 | # -*- coding: utf-8 -*- 3 | # @Author : lina 4 | # @Time : 2018/11/23 13:05 5 | """ 6 | Convolutional Autoencoder. 7 | """ 8 | import numpy as np 9 | 10 | from keras.datasets import mnist 11 | from keras.models import Model 12 | from keras.layers import Conv2D, MaxPool2D,Input, UpSampling2D 13 | import matplotlib.pyplot as plt 14 | 15 | np.random.seed(33) # random seed,to reproduce results. 16 | 17 | CHANNEL_1 = 16 18 | CHANNEL_2 = 8 19 | CHANNEL_OUTPUT = 1 20 | EPOCHS = 20 21 | BATCH_SIZE = 64 22 | 23 | def train(x_train): 24 | """ 25 | build autoencoder. 26 | :param x_train: the train data 27 | :return: encoder and decoder 28 | """ 29 | # input placeholder 30 | input_image = Input(shape=(28, 28, 1)) 31 | 32 | # encoding layer 33 | x = Conv2D(CHANNEL_1, (3, 3), activation='relu', padding="same")(input_image) 34 | x = MaxPool2D((2, 2), padding='same')(x) 35 | x = Conv2D(CHANNEL_2, (3, 3), activation='relu', padding='same')(x) 36 | encoded = MaxPool2D((2, 2), padding='same')(x) 37 | 38 | # decoding layer 39 | x = Conv2D(CHANNEL_2, (3, 3), activation='relu', padding='same')(encoded) 40 | x = UpSampling2D((2, 2))(x) 41 | x = Conv2D(CHANNEL_1, (3, 3),activation='relu', padding='same')(x) 42 | x = UpSampling2D((2, 2))(x) 43 | decoded = Conv2D(CHANNEL_OUTPUT, (3, 3), activation='sigmoid', padding='same')(x) 44 | 45 | # build autoencoder, encoder, decoder 46 | autoencoder = Model(inputs=input_image, outputs=decoded) 47 | encoder = Model(inputs=input_image, outputs=encoded) 48 | 49 | # compile autoencoder 50 | autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) 51 | 52 | autoencoder.summary() 53 | 54 | # training 55 | # need return history, otherwise can not use history["acc"] 56 | history_record = autoencoder.fit(x_train, x_train, epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True, ) 57 | 58 | return encoder, autoencoder, history_record 59 | 60 | def plot_accuray(history_record): 61 | """ 62 | plot the accuracy and loss line. 63 | :param history_record: 64 | :return: 65 | """ 66 | accuracy = history_record.history["acc"] 67 | loss = history_record.history["loss"] 68 | epochs = range(len(accuracy)) 69 | plt.plot(epochs, accuracy, 'bo', label='Training accuracy') 70 | plt.title('Training accuracy') 71 | plt.legend() 72 | plt.figure() 73 | plt.plot(epochs, loss, 'bo', label='Training loss') 74 | plt.title('Training loss') 75 | plt.legend() 76 | plt.show() 77 | 78 | def show_images(decode_images, x_test): 79 | """ 80 | plot the images. 81 | :param decode_images: the images after decoding 82 | :param x_test: testing data 83 | :return: 84 | """ 85 | n = 10 86 | plt.figure(figsize=(20, 4)) 87 | for i in range(n): 88 | ax = plt.subplot(2, n, i+1) 89 | ax.imshow(x_test[i].reshape(28, 28)) 90 | plt.gray() 91 | ax.get_xaxis().set_visible(False) 92 | ax.get_yaxis().set_visible(False) 93 | 94 | ax = plt.subplot(2, n, i + 1 + n) 95 | ax.imshow(decode_images[i].reshape(28, 28)) 96 | plt.gray() 97 | ax.get_xaxis().set_visible(False) 98 | ax.get_yaxis().set_visible(False) 99 | plt.show() 100 | 101 | 102 | 103 | if __name__ == '__main__': 104 | # Step1: load data x_train: (60000, 28, 28), y_train: (60000,) x_test: (10000, 28, 28), y_test: (10000,) 105 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 106 | 107 | # Step2: normalize 108 | x_train = x_train.astype('float32') / 255. 109 | x_test = x_test.astype('float32') / 255. 110 | 111 | # Step3: reshape data, x_train: (60000, 28, 28, 1), x_test: (10000, 28, 28, 1), one row denotes one sample. 112 | x_train = x_train.reshape((x_train.shape[0], 28, 28, 1)) 113 | x_test = x_test.reshape((x_test.shape[0], 28, 28, 1)) 114 | 115 | # Step4: train 116 | encoder, autoencoder, history_record = train(x_train=x_train) 117 | 118 | # show images 119 | decode_images = autoencoder.predict(x_test) 120 | show_images(decode_images, x_test) 121 | 122 | plot_accuray(history_record) 123 | 124 | 125 | 126 | -------------------------------------------------------------------------------- /autoencoder/denoising_autoencoder.py: -------------------------------------------------------------------------------- 1 | # python3 2 | # -*- coding: utf-8 -*- 3 | # @Author : lina 4 | # @Time : 2018/11/23 13:05 5 | """ 6 | Denoising Autoencoder with 4 layer encoder and 4 layer decoder. 7 | """ 8 | import numpy as np 9 | 10 | from keras.datasets import mnist 11 | from keras.models import Model 12 | from keras.layers import Dense, Input 13 | import matplotlib.pyplot as plt 14 | 15 | np.random.seed(33) # random seed,to reproduce results. 16 | 17 | ENCODING_DIM_INPUT = 784 18 | ENCODING_DIM_LAYER1 = 128 19 | ENCODING_DIM_LAYER2 = 64 20 | ENCODING_DIM_LAYER3 = 10 21 | ENCODING_DIM_OUTPUT = 2 22 | EPOCHS = 20 23 | BATCH_SIZE = 64 24 | NOISE_FACTOR = 0.5 25 | 26 | def train(x_train_noisy, x_train): 27 | 28 | # input placeholder 29 | input_image = Input(shape=(ENCODING_DIM_INPUT, )) 30 | 31 | # encoding layer 32 | encode_layer1 = Dense(ENCODING_DIM_LAYER1, activation='relu')(input_image) 33 | encode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(encode_layer1) 34 | encode_layer3 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_layer2) 35 | encode_output = Dense(ENCODING_DIM_OUTPUT)(encode_layer3) 36 | 37 | # decoding layer 38 | decode_layer1 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_output) 39 | decode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(decode_layer1) 40 | decode_layer3 = Dense(ENCODING_DIM_LAYER1, activation='relu')(decode_layer2) 41 | decode_output = Dense(ENCODING_DIM_INPUT, activation='tanh')(decode_layer3) 42 | 43 | # build autoencoder, encoder 44 | autoencoder = Model(inputs=input_image, outputs=decode_output) 45 | encoder = Model(inputs=input_image, outputs=encode_output) 46 | 47 | # compile autoencoder 48 | autoencoder.compile(optimizer='adam', loss='mse') 49 | 50 | # training 51 | autoencoder.fit(x_train_noisy, x_train, epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True) 52 | 53 | return encoder, autoencoder 54 | 55 | def add_noise(x_train, x_test): 56 | """ 57 | add noise. 58 | :return: 59 | """ 60 | x_train_noisy = x_train + NOISE_FACTOR * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 61 | x_test_noisy = x_test + NOISE_FACTOR * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 62 | 63 | x_train_noisy = np.clip(x_train_noisy, 0., 1.) # limit into [0, 1] 64 | x_test_noisy = np.clip(x_test_noisy, 0., 1.) # limit into [0, 1] 65 | 66 | return x_train_noisy, x_test_noisy 67 | 68 | def plot_representation(encode_images, y_test): 69 | """ 70 | plot the hidden result. 71 | :param encode_images: the images after encoding 72 | :param y_test: the label. 73 | :return: 74 | """ 75 | # test and plot 76 | plt.scatter(encode_images[:, 0], encode_images[:, 1], c=y_test, s=3) 77 | plt.colorbar() 78 | plt.show() 79 | 80 | def show_images(decode_images, x_test): 81 | """ 82 | plot the images. 83 | :param decode_images: the images after decoding 84 | :param x_test: testing data 85 | :return: 86 | """ 87 | n = 10 88 | plt.figure(figsize=(20, 4)) 89 | for i in range(n): 90 | ax = plt.subplot(2, n, i+1) 91 | ax.imshow(x_test[i].reshape(28, 28)) 92 | plt.gray() 93 | ax.get_xaxis().set_visible(False) 94 | ax.get_yaxis().set_visible(False) 95 | 96 | ax = plt.subplot(2, n, i + 1 + n) 97 | ax.imshow(decode_images[i].reshape(28, 28)) 98 | plt.gray() 99 | ax.get_xaxis().set_visible(False) 100 | ax.get_yaxis().set_visible(False) 101 | plt.show() 102 | 103 | if __name__ == '__main__': 104 | # Step1: load data x_train: (60000, 28, 28), y_train: (60000,) x_test: (10000, 28, 28), y_test: (10000,) 105 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 106 | 107 | # Step2: normalize 108 | x_train = x_train.astype('float32') / 255. 109 | x_test = x_test.astype('float32') / 255. 110 | 111 | # Step3: reshape data, x_train: (60000, 784), x_test: (10000, 784), one row denotes one sample. 112 | x_train = x_train.reshape((x_train.shape[0], -1)) 113 | x_test = x_test.reshape((x_test.shape[0], -1)) 114 | 115 | # Step4: add noisy 116 | x_train_noisy, x_test_noisy = add_noise(x_train, x_test) 117 | # show the contrast before noising and after noising. 118 | show_images(x_test_noisy, x_test) 119 | 120 | # Step5: train 121 | encoder,autoencoder = train(x_train_noisy=x_train_noisy, x_train=x_train) 122 | 123 | # test and plot 124 | encode_images = encoder.predict(x_test_noisy) 125 | plot_representation(encode_images, y_test) 126 | 127 | # show images 128 | decode_images = autoencoder.predict(x_test_noisy) 129 | show_images(decode_images, x_test_noisy) 130 | -------------------------------------------------------------------------------- /autoencoder/sparse_autoencoder.py: -------------------------------------------------------------------------------- 1 | # python3 2 | # -*- coding: utf-8 -*- 3 | # @Author : lina 4 | # @Time : 2018/11/23 14:05 5 | """ 6 | Sparse Autoencoder with 4 layer encoder and 4 layer decoder. 7 | """ 8 | import numpy as np 9 | 10 | from keras.datasets import mnist 11 | from keras.models import Model 12 | from keras.layers import Dense, Input, regularizers 13 | import matplotlib.pyplot as plt 14 | 15 | np.random.seed(33) # random seed,to reproduce results. 16 | 17 | ENCODING_DIM_INPUT = 784 18 | ENCODING_DIM_LAYER1 = 128 19 | ENCODING_DIM_LAYER2 = 64 20 | ENCODING_DIM_LAYER3 = 10 21 | ENCODING_DIM_OUTPUT = 2 22 | EPOCHS = 20 23 | BATCH_SIZE = 64 24 | 25 | def train(x_train): 26 | 27 | # input placeholder 28 | input_image = Input(shape=(ENCODING_DIM_INPUT, )) 29 | 30 | # encoding layer 31 | # *****this code is changed compared with Autoencoder, adding the activity_regularizer to make the input sparse. 32 | encode_layer1 = Dense(ENCODING_DIM_LAYER1, activation='relu', activity_regularizer=regularizers.l1(10e-6))(input_image) 33 | # ****************************** 34 | encode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(encode_layer1) 35 | encode_layer3 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_layer2) 36 | encode_output = Dense(ENCODING_DIM_OUTPUT)(encode_layer3) 37 | 38 | # decoding layer 39 | decode_layer1 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_output) 40 | decode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(decode_layer1) 41 | decode_layer3 = Dense(ENCODING_DIM_LAYER1, activation='relu')(decode_layer2) 42 | decode_output = Dense(ENCODING_DIM_INPUT, activation='tanh')(decode_layer3) 43 | 44 | # build autoencoder, encoder 45 | autoencoder = Model(inputs=input_image, outputs=decode_output) 46 | encoder = Model(inputs=input_image, outputs=encode_output) 47 | 48 | # compile autoencoder 49 | autoencoder.compile(optimizer='adam', loss='mse') 50 | 51 | # training 52 | autoencoder.fit(x_train, x_train, epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True) 53 | 54 | return encoder, autoencoder 55 | 56 | def plot_representation(encode_images, y_test): 57 | """ 58 | plot the hidden result. 59 | :param encode_images: the images after encoding 60 | :param y_test: the label. 61 | :return: 62 | """ 63 | # test and plot 64 | plt.scatter(encode_images[:, 0], encode_images[:, 1], c=y_test, s=3) 65 | plt.colorbar() 66 | plt.show() 67 | 68 | def show_images(decode_images, x_test): 69 | """ 70 | plot the images. 71 | :param decode_images: the images after decoding 72 | :param x_test: testing data 73 | :return: 74 | """ 75 | n = 10 76 | plt.figure(figsize=(20, 4)) 77 | for i in range(n): 78 | ax = plt.subplot(2, n, i+1) 79 | ax.imshow(x_test[i].reshape(28, 28)) 80 | plt.gray() 81 | ax.get_xaxis().set_visible(False) 82 | ax.get_yaxis().set_visible(False) 83 | 84 | ax = plt.subplot(2, n, i + 1 + n) 85 | ax.imshow(decode_images[i].reshape(28, 28)) 86 | plt.gray() 87 | ax.get_xaxis().set_visible(False) 88 | ax.get_yaxis().set_visible(False) 89 | plt.show() 90 | 91 | if __name__ == '__main__': 92 | # Step1: load data x_train: (60000, 28, 28), y_train: (60000,) x_test: (10000, 28, 28), y_test: (10000,) 93 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 94 | 95 | # Step2: normalize 96 | x_train = x_train.astype('float32') / 255. 97 | x_test = x_test.astype('float32') / 255. 98 | 99 | # Step3: reshape data, x_train: (60000, 784), x_test: (10000, 784), one row denotes one sample. 100 | x_train = x_train.reshape((x_train.shape[0], -1)) 101 | x_test = x_test.reshape((x_test.shape[0], -1)) 102 | 103 | # Step4: train 104 | encoder, autoencoder = train(x_train=x_train) 105 | 106 | # test and plot 107 | encode_images = encoder.predict(x_test) 108 | plot_representation(encode_images, y_test) 109 | 110 | # show images 111 | decode_images = autoencoder.predict(x_test) 112 | show_images(decode_images, x_test) 113 | -------------------------------------------------------------------------------- /autoencoder/stack_autoencoder.py: -------------------------------------------------------------------------------- 1 | # python3 2 | # -*- coding: utf-8 -*- 3 | # @Author : lina 4 | # @Time : 2018/11/23 13:05 5 | """ 6 | Autoencoder with 4 layer encoder and 4 layer decoder. 7 | """ 8 | import numpy as np 9 | 10 | from keras.datasets import mnist 11 | from keras.models import Model 12 | from keras.layers import Dense, Input 13 | import matplotlib.pyplot as plt 14 | 15 | np.random.seed(33) # random seed,to reproduce results. 16 | 17 | ENCODING_DIM_INPUT = 784 18 | ENCODING_DIM_LAYER1 = 128 19 | ENCODING_DIM_LAYER2 = 64 20 | ENCODING_DIM_LAYER3 = 10 21 | ENCODING_DIM_OUTPUT = 2 22 | EPOCHS = 20 23 | BATCH_SIZE = 64 24 | 25 | def train(x_train): 26 | 27 | # input placeholder 28 | input_image = Input(shape=(ENCODING_DIM_INPUT, )) 29 | 30 | # encoding layer 31 | encode_layer1 = Dense(ENCODING_DIM_LAYER1, activation='relu')(input_image) 32 | encode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(encode_layer1) 33 | encode_layer3 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_layer2) 34 | encode_output = Dense(ENCODING_DIM_OUTPUT)(encode_layer3) 35 | 36 | # decoding layer 37 | decode_layer1 = Dense(ENCODING_DIM_LAYER3, activation='relu')(encode_output) 38 | decode_layer2 = Dense(ENCODING_DIM_LAYER2, activation='relu')(decode_layer1) 39 | decode_layer3 = Dense(ENCODING_DIM_LAYER1, activation='relu')(decode_layer2) 40 | decode_output = Dense(ENCODING_DIM_INPUT, activation='tanh')(decode_layer3) 41 | 42 | # build autoencoder, encoder 43 | autoencoder = Model(inputs=input_image, outputs=decode_output) 44 | encoder = Model(inputs=input_image, outputs=encode_output) 45 | 46 | # compile autoencoder 47 | autoencoder.compile(optimizer='adam', loss='mse') 48 | 49 | # training 50 | autoencoder.fit(x_train, x_train, epochs=EPOCHS, batch_size=BATCH_SIZE, shuffle=True) 51 | 52 | return encoder, autoencoder 53 | 54 | def plot_representation(encode_images, y_test): 55 | """ 56 | plot the hidden result. 57 | :param encode_images: the images after encoding 58 | :param y_test: the label. 59 | :return: 60 | """ 61 | # test and plot 62 | plt.scatter(encode_images[:, 0], encode_images[:, 1], c=y_test, s=3) 63 | plt.colorbar() 64 | plt.show() 65 | 66 | def show_images(decode_images, x_test): 67 | """ 68 | plot the images. 69 | :param decode_images: the images after decoding 70 | :param x_test: testing data 71 | :return: 72 | """ 73 | n = 10 74 | plt.figure(figsize=(20, 4)) 75 | for i in range(n): 76 | ax = plt.subplot(2, n, i+1) 77 | ax.imshow(x_test[i].reshape(28, 28)) 78 | plt.gray() 79 | ax.get_xaxis().set_visible(False) 80 | ax.get_yaxis().set_visible(False) 81 | 82 | ax = plt.subplot(2, n, i + 1 + n) 83 | ax.imshow(decode_images[i].reshape(28, 28)) 84 | plt.gray() 85 | ax.get_xaxis().set_visible(False) 86 | ax.get_yaxis().set_visible(False) 87 | plt.show() 88 | 89 | if __name__ == '__main__': 90 | # Step1: load data x_train: (60000, 28, 28), y_train: (60000,) x_test: (10000, 28, 28), y_test: (10000,) 91 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 92 | 93 | # Step2: normalize 94 | x_train = x_train.astype('float32') / 255. 95 | x_test = x_test.astype('float32') / 255. 96 | 97 | # Step3: reshape data, x_train: (60000, 784), x_test: (10000, 784), one row denotes one sample. 98 | x_train = x_train.reshape((x_train.shape[0], -1)) 99 | x_test = x_test.reshape((x_test.shape[0], -1)) 100 | 101 | # Step4: train 102 | encoder,autoencoder = train(x_train=x_train) 103 | 104 | # test and plot 105 | encode_images = encoder.predict(x_test) 106 | plot_representation(encode_images, y_test) 107 | 108 | # show images 109 | decode_images = autoencoder.predict(x_test) 110 | show_images(decode_images, x_test) 111 | -------------------------------------------------------------------------------- /imgs/ae_encoder_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/ae_encoder_result.png -------------------------------------------------------------------------------- /imgs/ae_generate_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/ae_generate_comparison.png -------------------------------------------------------------------------------- /imgs/ae_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/ae_structure.png -------------------------------------------------------------------------------- /imgs/convAe_accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/convAe_accuracy.png -------------------------------------------------------------------------------- /imgs/convAe_generate_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/convAe_generate_comparison.png -------------------------------------------------------------------------------- /imgs/convAe_loss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/convAe_loss.png -------------------------------------------------------------------------------- /imgs/convAe_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/convAe_structure.png -------------------------------------------------------------------------------- /imgs/denoisingAe_encoder_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/denoisingAe_encoder_result.png -------------------------------------------------------------------------------- /imgs/denoisingAe_generate_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/denoisingAe_generate_comparison.png -------------------------------------------------------------------------------- /imgs/denoisingAe_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/denoisingAe_structure.png -------------------------------------------------------------------------------- /imgs/noising_data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/noising_data.png -------------------------------------------------------------------------------- /imgs/sparseAe_encoder_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/sparseAe_encoder_result.png -------------------------------------------------------------------------------- /imgs/sparseAe_generate_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/sparseAe_generate_comparison.png -------------------------------------------------------------------------------- /imgs/sparseAe_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/sparseAe_structure.png -------------------------------------------------------------------------------- /imgs/stackAe_encoder_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/stackAe_encoder_result.png -------------------------------------------------------------------------------- /imgs/stackAe_generate_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/stackAe_generate_comparison.png -------------------------------------------------------------------------------- /imgs/stackAe_structure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nana0606/autoencoder/fad7b59987943516b1ea7299f434310abe670b4f/imgs/stackAe_structure.png --------------------------------------------------------------------------------