keras实战-入门之自编码器

keras实战-入门之自编码器

自编码器

一种生成模型,就是一种你输入什么,最后让他输出什么,中间会生成一个向量,可以进行数据的降维,可以获得特征向量,比如把某个图压缩成一个N维向量,还可以用这个向量复原回图,降维的特性跟PCA有点类似。下面的代码就是个例子,用全连接将手写数字进行自编码,可以看到编码成维度较高的向量,复原会比较好,维度较低的不太好,估计是因为压缩的太厉害了,缺少了很多特征,或许用CNN会比较好点吧,或许是模型还不够复杂,无法在少量特征的情况下复原的比较好,有兴趣的朋友可以试试。

这个跟GAN的区别还是挺大的,这个是输入什么就输出什么,而GAN只是随机生成,几乎不会生成跟样本一模一样的东西,GAN是学习分布,自编码器是降维,压缩,然后解压。

import numpy as np
np.random.seed(11)
from keras.datasets import mnist
import matplotlib.pyplot as plt
Using TensorFlow backend.
(x_train_image,y_train_label),(x_test_image,y_test_label)=mnist.load_data()
plt.imshow(x_test_image[0],cmap='binary')
<matplotlib.image.AxesImage at 0x321e5630>

在这里插入图片描述

#提取特征的维度
latent_dim=100
x_train=x_train_image.reshape(60000,-1).astype('float32')
x_test=x_test_image.reshape(10000,-1).astype('float32')
print(x_train.shape)
print(x_test.shape)
(60000, 784)
(10000, 784)
x_train_normalize=x_train/255
x_test_normalize=x_test/255
from keras import Model
from keras.layers import Dense,Input
#函数式要定义输入
input_image=Input((784,))
#编码器
encoder=Dense(units=256,kernel_initializer='normal',activation='relu')(input_image)
encoder=Dense(units=128,kernel_initializer='normal',activation='relu')(encoder)
encoder_out=Dense(units=latent_dim,kernel_initializer='normal',activation='relu')(encoder)

#解码器,反过来
decoder=Dense(units=128,kernel_initializer='normal',activation='relu')(encoder_out)
decoder=Dense(units=256,kernel_initializer='normal',activation='relu')(decoder)
decoder_out=Dense(units=784,kernel_initializer='normal',activation='relu')(decoder)

autoencoder=Model(input_image,decoder_out)
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
autoencoder.compile(optimizer='adam', loss='mse')

autoencoder.fit(x_train_normalize, x_train_normalize, epochs=20, batch_size=256, shuffle=True,verbose=1)
WARNING:tensorflow:From F:\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/20
60000/60000 [==============================] - 6s 104us/step - loss: 0.0445
Epoch 2/20
60000/60000 [==============================] - 4s 64us/step - loss: 0.0212
Epoch 3/20
60000/60000 [==============================] - 6s 100us/step - loss: 0.0172
Epoch 4/20
60000/60000 [==============================] - 3s 44us/step - loss: 0.0153
Epoch 5/20
60000/60000 [==============================] - 3s 45us/step - loss: 0.0143
Epoch 6/20
60000/60000 [==============================] - 3s 45us/step - loss: 0.0135
Epoch 7/20
60000/60000 [==============================] - 3s 49us/step - loss: 0.0130
Epoch 8/20
60000/60000 [==============================] - 6s 107us/step - loss: 0.0125
Epoch 9/20
60000/60000 [==============================] - 3s 48us/step - loss: 0.0121
Epoch 10/20
60000/60000 [==============================] - 2s 41us/step - loss: 0.0117
Epoch 11/20
60000/60000 [==============================] - 2s 40us/step - loss: 0.0114
Epoch 12/20
60000/60000 [==============================] - 2s 41us/step - loss: 0.0112
Epoch 13/20
60000/60000 [==============================] - 5s 82us/step - loss: 0.0109:
Epoch 14/20
60000/60000 [==============================] - 5s 88us/step - loss: 0.0107
Epoch 15/20
60000/60000 [==============================] - 2s 40us/step - loss: 0.0106
Epoch 16/20
60000/60000 [==============================] - 2s 41us/step - loss: 0.0104
Epoch 17/20
60000/60000 [==============================] - 2s 40us/step - loss: 0.0103
Epoch 18/20
60000/60000 [==============================] - 2s 40us/step - loss: 0.0101
Epoch 19/20
60000/60000 [==============================] - 6s 94us/step - loss: 0.0100
Epoch 20/20
60000/60000 [==============================] - 4s 70us/step - loss: 0.0099





<keras.callbacks.History at 0x4cb61d30>
encoded_imgs = autoencoder.predict(x_test_normalize)
#显示编码器生成的图和真实的图
def show_images(index):
    image=encoded_imgs[index,:]*255
    image=image.reshape(28,28)
    plt.figure(figsize=(10,10))
    plt.subplot(1,2,1)
    plt.title('encoder')
    plt.imshow(image,cmap='binary')
    plt.subplot(1,2,2)
    plt.title('true')
    plt.imshow(x_test_image[index],cmap='binary')
    plt.show()
show_images(0)

在这里插入图片描述

show_images(1)

在这里插入图片描述

#特征变2维,看看可视化的样子
latent_dim=2
#函数式要定义输入
input_image=Input((784,))
#编码器
encoder=Dense(units=128,kernel_initializer='normal',activation='relu')(input_image)
encoder=Dense(units=64,kernel_initializer='normal',activation='relu')(encoder)
encoder=Dense(units=8,kernel_initializer='normal',activation='relu')(encoder)
encoder_out=Dense(units=latent_dim,kernel_initializer='normal')(encoder)

# 构建编码模型
encoder_model = Model(inputs=input_image, outputs=encoder_out)

#解码器,反过来
decoder=Dense(units=8,kernel_initializer='normal',activation='relu')(encoder_out)
decoder=Dense(units=64,kernel_initializer='normal',activation='relu')(decoder)
decoder=Dense(units=128,kernel_initializer='normal',activation='relu')(decoder)
decoder_out=Dense(units=784,kernel_initializer='normal',activation='relu')(decoder)



#构建整个编码器
autoencoder=Model(input_image,decoder_out)
autoencoder.compile(optimizer='adam', loss='mse')


autoencoder.fit(x_train_normalize, x_train_normalize, epochs=10, batch_size=256, shuffle=True,verbose=1)
Epoch 1/10
60000/60000 [==============================] - 3s 57us/step - loss: 0.0771
Epoch 2/10
60000/60000 [==============================] - 3s 44us/step - loss: 0.0680
Epoch 3/10
60000/60000 [==============================] - 5s 76us/step - loss: 0.0603
Epoch 4/10
60000/60000 [==============================] - 7s 110us/step - loss: 0.0552
Epoch 5/10
60000/60000 [==============================] - 3s 45us/step - loss: 0.0518
Epoch 6/10
60000/60000 [==============================] - 3s 45us/step - loss: 0.0493
Epoch 7/10
60000/60000 [==============================] - 3s 46us/step - loss: 0.0472
Epoch 8/10
60000/60000 [==============================] - 3s 47us/step - loss: 0.0457
Epoch 9/10
60000/60000 [==============================] - 7s 119us/step - loss: 0.0448
Epoch 10/10
60000/60000 [==============================] - 4s 64us/step - loss: 0.0442





<keras.callbacks.History at 0x62001dd8>
encoded_imgs = autoencoder.predict(x_test_normalize)
show_images(0)

在这里插入图片描述

show_images(1)

在这里插入图片描述

#显示编码后的2维特征向量的分布,貌似会有聚类的效果
encoded_latent = encoder_model.predict(x_test_normalize)
print(encoded_latent.shape)
plt.scatter(encoded_latent[:, 0], encoded_latent[:, 1], c=y_test_label, s=3,cmap='rainbow')
plt.colorbar()
plt.show()
(10000, 2)

在这里插入图片描述

好了,今天就到这里了,希望对学习理解有帮助,大神看见勿喷,仅为自己的学习理解,能力有限,请多包涵,侵删。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值