实践:auto encoder生成手写数字

说明:

课程小作业,简单记录

有用链接:

做法:

  1. 加载包
import tensorflow as tf
from tensorflow import keras
import numpy as np
from tensorflow.keras import layers, optimizers, datasets, Model
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Input, Activation
  1. 手写数字数据
mnist = datasets.mnist
(x0, y0), (x1, y1) = datasets.mnist.load_data()
train_x = x0.reshape(-1,784)
train_y = x0.reshape(-1,784)
test_x = x1.reshape(-1,784)
test_y = x1.reshape(-1,784)
  1. 简单的encoder-decoder
    (784-1000-500-250-2-250-500-1000-784)的前向NN。
#encoder
enc_input = Input([784,])
x = Dense(1000,activation= 'relu')(input_x)
x = Dense(500,activation= 'relu')(x) 
x = Dense(250,activation= 'relu')(x) 
enc_output = Dense(2)(x) 
encoder = Model(input_x,enc_output)
#decoder
dec_input = Input([2,])
x = Dense(250,activation= 'relu')(dec_input) 
x = Dense(500,activation= 'relu')(x) 
x = Dense(1000,activation= 'relu')(x) 
x = Dense(784)(x) 
dec_output = x
decoder = Model(dec_input,dec_output)
#合并
input_x = Input([784,])
code = encoder(input_x )
output = decoder(code)

model = Model(input_x,output)
  1. 训练,提前停止,画loss曲线
  • 训练
optimizer = optimizers.Adam(0.00001)
model.compile(optimizer=optimizer,
              loss='mse')
early_stopping=tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=1,
                              patience=5, verbose=0, mode='auto',
                              baseline=None, restore_best_weights=False)
history = model.fit(train_x,train_y,validation_data = (test_x,test_y),batch_size =128,epochs = 300,callbacks = [early_stopping])
  • 画图
plt.figure()
epochs=range(len(history.history['loss']))
plt.plot(epochs,history.history['loss'],'b',label='Training loss')
plt.plot(epochs,history.history['val_loss'],'r',label='Validation val_loss')
plt.title('Traing and Validation loss')
plt.legend()
plt.show()

在这里插入图片描述

  1. 查看encoder结果(二维)的散点图
inp = test_x[-1000:].reshape((-1,784))
code_x = encoder(inp).numpy()
dt = []
for i in range(10):
    dt.append(code_x[y1[-1000:]==i])
color =['c', 'b', 'g', 'r', 'orange', 'y', 'k', 'silver','pink','purple']
plt.figure(figsize=(12,12))
for i in range(10):
    plot = dt[i]
    plt.scatter(plot[:,0],plot[:,1],c=color[i],label = i)
    plt.legend(loc = 'best')
plt.show()

在这里插入图片描述

  1. 查看decoder生成的效果
import random
decode = np.array(random.sample(list(code_x),20))
print(decode.shape)
pred_dec = decoder(decode).numpy().reshape(-1,28,28)
print(pred_dec.shape)
fig, axes = plt.subplots(4, 5,figsize=(5, 5))
for j in range(4):
    for i in range(5):
        axes[j][i].imshow(pred_dec[i*(j+1)])
        axes[j][i].axis('off')
plt.show()

在这里插入图片描述

还可以尝试:

  • VAE
  • GAN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值