Keras搭建的自编码模型

  1. http://blog.csdn.net/u012458963/article/details/72566596
         https://kiseliu.github.io/2016/08/16/building-autoencoders-in-keras/


  1. import numpy as np  
  2. np.random.seed(1337)  # for reproducibility  
  3.   
  4. from keras.datasets import mnist  
  5. from keras.models import Model #泛型模型  
  6. from keras.layers import Dense, Input  
  7. import matplotlib.pyplot as plt  
  8.   
  9. # X shape (60,000 28x28), y shape (10,000, )  
  10. (x_train, _), (x_test, y_test) = mnist.load_data()  
  11.   
  12. # 数据预处理  
  13. x_train = x_train.astype('float32') / 255. - 0.5       # minmax_normalized  
  14. x_test = x_test.astype('float32') / 255. - 0.5         # minmax_normalized  
  15. x_train = x_train.reshape((x_train.shape[0], -1))  
  16. x_test = x_test.reshape((x_test.shape[0], -1))  
  17. print(x_train.shape)  
  18. print(x_test.shape)  
  19.   
  20. # 压缩特征维度至2维  
  21. encoding_dim = 2  
  22.   
  23. # this is our input placeholder  
  24. input_img = Input(shape=(784,))  
  25.   
  26. # 编码层  
  27. encoded = Dense(128, activation='relu')(input_img)  
  28. encoded = Dense(64, activation='relu')(encoded)  
  29. encoded = Dense(10, activation='relu')(encoded)  
  30. encoder_output = Dense(encoding_dim)(encoded)  
  31.   
  32. # 解码层  
  33. decoded = Dense(10, activation='relu')(encoder_output)  
  34. decoded = Dense(64, activation='relu')(decoded)  
  35. decoded = Dense(128, activation='relu')(decoded)  
  36. decoded = Dense(784, activation='tanh')(decoded)  
  37.   
  38. # 构建自编码模型  
  39. autoencoder = Model(inputs=input_img, outputs=decoded)  
  40.   
  41. # 构建编码模型  
  42. encoder = Model(inputs=input_img, outputs=encoder_output)  
  43.   
  44. # compile autoencoder  
  45. autoencoder.compile(optimizer='adam', loss='mse')  
  46.   
  47. # training  
  48. autoencoder.fit(x_train, x_train, epochs=20, batch_size=256, shuffle=True)  
  49.   
  50. # plotting  
  51. encoded_imgs = encoder.predict(x_test)  
  52. plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=y_test, s=3)  
  53. plt.colorbar()  
  54. plt.show()  
------------------------------------------------------------
 http://blog.csdn.net/u012458963/article/details/72566596
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是使用 Keras 搭建的一维卷积自编码器的代码: ```python from keras.layers import Input, Conv1D, MaxPooling1D, UpSampling1D from keras.models import Model # 定义输入层 input_layer = Input(shape=(input_dim, 1)) # 编码器部分 encoded = Conv1D(16, 3, activation='relu', padding='same')(input_layer) encoded = MaxPooling1D(2, padding='same')(encoded) encoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) encoded = MaxPooling1D(2, padding='same')(encoded) encoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) encoded = MaxPooling1D(2, padding='same')(encoded) # 解码器部分 decoded = Conv1D(8, 3, activation='relu', padding='same')(encoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(8, 3, activation='relu', padding='same')(decoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(16, 3, activation='relu')(decoded) decoded = UpSampling1D(2)(decoded) decoded = Conv1D(1, 3, activation='sigmoid', padding='same')(decoded) # 定义模型 autoencoder = Model(input_layer, decoded) # 编译模型 autoencoder.compile(optimizer='adam', loss='binary_crossentropy') ``` 其,`input_dim` 是输入数据的维度,可以根据实际情况进行修改。在编码器部分,我们使用了三个一维卷积层和两个最大池化层,来将输入数据编码成一个低维的表示。在解码器部分,我们使用了三个一维卷积层和两个上采样层,来将编码后的数据解码成与输入数据相同的维度。整个模型使用的损失函数是二元交叉熵。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值