<jupyter_start><jupyter_text>程序说明时间：2018年1月3日说明：该程序构造一个基于变分自动编码器的加法器。Reference: ["Auto-Encoding Variational Bayes"](https://arxiv.org/abs/1312.6114)数据集：MNIST  1.加载keras模块<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from keras.layers import Input, Dense, Lambda, Add, concatenate
from keras.models import Model, load_model
from keras import backend as K
from keras import objectives
import h5py
#from keras.datasets import mnist
import sum_input<jupyter_output><empty_output><jupyter_text>2.变量初始化<jupyter_code>batch_size = 100
original_dim = 784
latent_dim = 32
intermediate_dim = 256
nb_epoch = 50
epsilon_std = 1.0

x = Input(batch_shape=(batch_size, original_dim),name='x_input')
x2 = Input(batch_shape=(batch_size, original_dim),name='x2_input')
x_sum = Input(batch_shape=(batch_size, original_dim),name='sum_input')

hidden_ly = Dense(intermediate_dim, activation='relu',name='Dense_1')
mn_ly = Dense(latent_dim)
var_ly = Dense(latent_dim)

h = hidden_ly(x)
z_mean = mn_ly(h)
z_log_var = var_ly(h)

h2 = hidden_ly(x2)
z_mean2 = mn_ly(h2)
z_log_var2 = var_ly(h2)


def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
                              stddev=epsilon_std)
    return z_mean + K.exp(z_log_var / 2) * epsilon
# note that "output_shape" isn't necessary with the TensorFlow backend
#z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
z = Lambda(sampling)([z_mean, z_log_var])
z2 = Lambda(sampling)([z_mean2, z_log_var2])
# we instantiate these layers separately so as to reuse them later
decoder_h_ly = Dense(intermediate_dim, activation='relu',name='Dense_3')
decoder_mean_ly = Dense(original_dim, activation='sigmoid',name='Dense_4')

h_decoded = decoder_h_ly(z) #####
x_decoded_mean = decoder_mean_ly(h_decoded)

h_decoded2 = decoder_h_ly(z2) #####
x_decoded_mean2 = decoder_mean_ly(h_decoded2)

decoder_h_sum_ly = Dense(intermediate_dim, activation='relu',name='Dense_7')
decoder_mean_sum_ly = Dense(original_dim, activation='sigmoid',name='Dense_8')
#added_z = Add()([z, z2])
added_z = concatenate([z, z2], axis=-1)
h_decoded_sum = decoder_h_sum_ly(added_z) #####
decoded_mean_sum = decoder_mean_sum_ly(h_decoded_sum)
def vae_loss(inputs=[x, x2, x_sum], outputs=[x_decoded_mean, x_decoded_mean2, decoded_mean_sum]):
    xent_loss = original_dim * objectives.binary_crossentropy(x_sum, decoded_mean_sum)
    x_loss = original_dim * objectives.binary_crossentropy(x, x_decoded_mean)
    x2_loss = original_dim * objectives.binary_crossentropy(x2, x_decoded_mean2)
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    return 3*xent_loss + x_loss + x2_loss + kl_loss
vae = Model(inputs=[x, x2, x_sum], outputs=[x_decoded_mean, x_decoded_mean2, decoded_mean_sum])
#vae = Model(x_sum, x, x2, x_decoded_mean, x_decoded_mean2, decoded_mean_sum)
vae.compile(optimizer='rmsprop', loss=vae_loss)
# train the VAE on MNIST digits
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
mnist = sum_input.read_data_sets('./Data')
#batch_1, batch_2, batch_3 = mnist.train.next_batch(40000)

vae=load_model('model.h5')
#vae.fit({'x_input':batch_1, 'x2_input':batch_2, 'sum_input':batch_3},{'input_4':batch_1, 'input_5':batch_2, 'input_6':batch_3},
#        shuffle=False,
#        epochs=nb_epoch,
#        batch_size=batch_size)
for i in np.arange(30):
    batch_1, batch_2, batch_3 = mnist.train.next_batch(10000)
    print(i)
    vae.fit([batch_1, batch_2, batch_3],[batch_1, batch_2, batch_3],
         shuffle=False,
         epochs=100,
         batch_size=batch_size)
# build a model to project inputs on the latent space

encoder = Model(x, z_mean)
# display a 2D plot of the digit classes in the latent space
x_test_encoded = encoder.predict(batch_1, batch_size=batch_size)
plt.figure(figsize=(6, 6))
s = [0.5 for n in range(len(x_test_encoded[:, 1]))]
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], marker='.',s=s)
#plt.colorbar()
plt.show()
encoder = Model(x, z_mean)
decoder_half_input = Input(shape=(latent_dim,))

decoder_input = Input(shape=(latent_dim*2,))

test_1, test_2, test_3 = mnist.test.next_batch(batch_size)#4000)
#whl = Model(inputs=[x,x2,x_sum],outputs=[x_decoded_mean, x_decoded_mean2, decoded_mean_sum])
print(test_1.shape)
zz = encoder.predict(test_1, batch_size=batch_size)
zz1 = encoder.predict(test_2, batch_size=batch_size)

ste1 = decoder_h_ly(decoder_half_input)
agen1 = decoder_mean_ly(ste1)
autodec = Model(decoder_half_input,agen1)

image1 = autodec.predict(zz, batch_size=batch_size)
image2 = autodec.predict(zz1, batch_size=batch_size)


print(zz.shape)
print(zz1.shape)
#decoder_input = concatenate([z, z1])
zzm = np.hstack([zz,zz1])
print(zzm.shape)

summ = decoder_h_sum_ly(decoder_input)
sum_img = decoder_mean_sum_ly(summ)
sum_gen = Model(decoder_input,sum_img)
out_sum_img = sum_gen.predict(zzm, batch_size=batch_size)
print(out_sum_img.shape)
#rint(sum_img.shape)
n = 10  # how many digits we will display
sam =40
plt.figure(figsize=(20, 4))
plt.figure(figsize=(20, 4))
for i in range(10):
    # display original
    ax = plt.subplot(4, n, i + 1)
    im1 = image1[i+sam].reshape(28,28)
    plt.imshow(im1)
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    ax = plt.subplot(4, n, i + 1 + n)
    im2 = image2[i+sam].reshape(28,28)
    plt.imshow(im2)
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # display reconstruction
    ax = plt.subplot(4, n, i + 1 + n*2)
#    plt.imshow(decoded_imgs[i+sam].reshape(28, 28))
    plt.imshow(test_3[i+sam].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    
    ax = plt.subplot(4, n, i + 1 + n*3)
    plt.imshow(out_sum_img[i+sam].reshape(28, 28))
#    plt.imshow(test_sum[i+sam].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()
vae.save('model.h5')
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

SVG(model_to_dot(vae).create(prog='dot', format='svg'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13845905867182462, 'fraction_numerical': 0.03175370728562218, 'mean_word_length': 5.886792452830188, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '335803', 'license': 'MIT', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2542, 'n_words': 430, 'path': 'summator_keras_vae_share.ipynb', 'repo_name': 'kekegg/Summater', 'size': '6204'}