Autoencoder 基于tensorflow2.0的代码

具体原理不讲了,网上资料相当多,但是感觉直接可以用的代码不多,所以基于各种资料实现了代码。

第一,利用autoencoder降噪,

第一部分:数据准备 

from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data() 
x_train = x_train.reshape((-1, 28 * 28)) / 255.0  # 除255的目的就是归一化 reshape的目的就是flatten()
x_test = x_test.reshape((-1, 28 * 28)) / 255.0

第二部分:构建网络结构

code_dim = 64
inputs = Input(shape=(x_train.shape[1],), name='inputs')  # 就是flatten
code = Dense(256, activation='relu', name='code1')(inputs)
code = Dense(128, activation='relu', name='code2')(code)
code = Dense(code_dim, activation='relu', name='code3')(code)

outputs = Dense(64, activation='relu', name='outputs1')(code)
outputs = Dense(128, activation='relu', name='outputs2')(outputs)
outputs = Dense(256, activation='relu', name='outputs3')(outputs)
outputs = Dense(x_train.shape[1], activation='softmax', name='decoder')(outputs)

第三模型训练

auto_encoder = Model(inputs, outputs)  # 训练模型 输入输出都是784维。
auto_encoder.compile(optimizer='adam',
                     loss='binary_crossentropy')
auto_encoder.fit(x_train, x_train, batch_size=64, epochs=5, validation_split=0.1)

第四预测结果与结果可视化

auto_encoder_result = auto_encoder.predict(x_test)

n = 5
for i in range(n):
    ax = plt.subplot(3, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(3, n, n + i + 1)
    plt.imshow(auto_encoder_result[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

全部代码过程

from tensorflow.keras.layers import Dense, Input, Flatten
from tensorflow.keras import Model
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
from tensorflow import keras
import numpy as np

############################# 数据准备 #############################
(x_train, y_train), (x_test, y_test) = mnist.load_data()
np.random.seed(1)
np.random.shuffle(x_train)
np.random.shuffle(y_train)
np.random.shuffle(x_test)
np.random.shuffle(y_test)  # 随机打乱顺序 
x_train = x_train.reshape((-1, 28 * 28)) / 255.0
x_test = x_test.reshape((-1, 28 * 28)) / 255.0

############################# 网络结构 #############################
code_dim = 64
inputs = Input(shape=(x_train.shape[1],), name='inputs')  # 就是flatten
code = Dense(256, activation='relu', name='code1')(inputs)
code = Dense(128, activation='relu', name='code2')(code)
code = Dense(64, activation='relu', name='code3')(code)

outputs = Dense(64, activation='relu', name='outputs1')(code)
outputs = Dense(128, activation='relu', name='outputs2')(outputs)
outputs = Dense(256, activation='relu', name='outputs3')(outputs)
outputs = Dense(x_train.shape[1], activation='softmax', name='decoder')(outputs)

############################# 模型训练 #############################
auto_encoder = Model(inputs, outputs)  # 训练模型 这时候的输入、输出就是一个
auto_encoder.summary()
keras.utils.plot_model(auto_encoder, show_shapes=True)
auto_encoder.compile(optimizer='adam',
                     loss='binary_crossentropy')
auto_encoder.fit(x_train, x_train, batch_size=64, epochs=5, validation_split=0.1)
############################# 模型预测 #############################
auto_encoder_result = auto_encoder.predict(x_test)
############################# 可视化 #############################
n = 5
for i in range(n):
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(2, n, n + i + 1)
    plt.imshow(auto_encoder_result[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()






上述代码最常见的作用就是清洗噪声,但是一般情况下autoencoder还需要用来降维,比如我们希望得到图片中64维那个地方的数据 再把它变成一个8*8的。即实现了把原始图片28*28变成了8*8。 

第二,利用autoencoder降维 

利用autoencoder方法对数据进行降维,代码如下:

from tensorflow.keras.layers import Dense, Input, Flatten
from tensorflow.keras import Model
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
from tensorflow import keras

############################# 数据准备 #############################
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape((-1, 28 * 28)) / 255.0
x_test = x_test.reshape((-1, 28 * 28)) / 255.0

############################# 数据准备 #############################
code_dim = 64
inputs = Input(shape=(x_train.shape[1],), name='inputs')  # 就是flatten
code = Dense(256, activation='relu', name='code1')(inputs)
code = Dense(128, activation='relu', name='code2')(code)
code = Dense(64, activation='relu', name='code3')(code)

outputs = Dense(64, activation='relu', name='outputs1')(code)
outputs = Dense(128, activation='relu', name='outputs2')(outputs)
outputs = Dense(256, activation='relu', name='outputs3')(outputs)
outputs = Dense(x_train.shape[1], activation='softmax', name='decoder')(outputs)

auto_encoder = Model(inputs, outputs)  # 训练模型 这时候的输入、输出就是一个
encoder_model = Model(inputs, code)  # 建立encoder模型,这个模型预测得到就是降维后的数据
auto_encoder.compile(optimizer='adam',
                     loss='binary_crossentropy',
                     )
auto_encoder.fit(x_train, x_train, batch_size=64, epochs=5, validation_split=0.1)
encoded = encoder_model.predict(x_test)  # 784 变成了 64 降维后的结果

n = 5
for i in range(n):
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(2, n,  n + i + 1)
    plt.imshow(encoded[i].reshape(8, 8))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

总结

上述代码全是基于全连接完成的,至于 CNN Autoencoder & LSTM Autoencoder会根据需要进行更新。

 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值