Implementation Notes

2017/03/29

```{python id:”izdlk700”} def word_level_encoder(input):

input :[batch_size,max_time_seq] eg:[4,20] 这首诗有4句话,每句话最多20个字

sentence_final_states:[batch_size,word_level_hidden_dim] 返回每一句话的hidden representation

GRUCell1=tf.nn.rnn_cell.GRUCell(word_level_hidden_dim)
outputs,final_states=tf.nn.dynamic_rnn(GRUCell1,input,...)
return sentence_final_states ```

```{python id:”j0uhszp7”} def context_level_encoder(final_states):

context_outputs:[batch_size,context_level_hidden_dim] 返回截止的目前内容的hidden representation

first_rev_states:[1,latent_dim] 返回第一句话反向GRU的final_state

GRUCell2=tf.nn.rnn_cell.GRUCell(context_level_hidden_dim)
context_outputs,final_states=tf.nn.dynamic_rnn(GRUCell2,sentence_final_states,...)
outputs_reversed=tf.reverse(context_outputs,[False,True,False])
GRUCell3=tf.nn.rnn_cell.GRUCell(latent_dim)
init_state=zeros_initializer
_,first_rev_states=tf.nn.dynamic_rnn(GRUCell3,outputs_reversed,init_state,...)
return context_outputs,first_rev_states ```

```{python id:”j0uik3xl”} #*** tf.nn.dynamic_rnn(GRUCell’,hs_con_enc,…) def learning_X_enc2lat(context_outputs,first_rev_states):

q_zs:[batch_size,latent_dim]

q_means:[batch_size,latent_dim]

q_logvars:[batch_size,latent_dim]

q_zs=[]
q_means=[]
q_logvars=[]
# hs_con_enc=[]
for i in range(batch_size):
    if i==0:
        h_con_enc=tf.concat(context_outputs[0],first_rev_states)
    else:
        h_con_enc=tf.concat(context_outputs[i],zs[i-1])
    sample_means=[]
    sample_zs=[]
    sample_logvars=[]
    for j in L:
        mean,logvar=MLP1(h_con_enc)
        z=sample(mean,logvar)
        sample_means.append(mean)
        sample_logvars.append(logvar)
        sample_zs.append(z)

q_zs.append(tf.reduce_mean(sample_zs))
q_means.append(tf.reduce_mean(sample_means))
q_logvars.append(tf.reduce_mean(sample_logvars))
return q_zs,q_means,q_logvars ```

```{python id:”j0uj59sm”} #*** tf.nn.dynamic_rnn(GRUCell’‘,hs_con_dec,…) def reconstruct_X_enc2lat(): p_zs=[] p_means=[] p_logvars=[] hs_con_dec=[] for i in range(batch_size): if i==0: z=tf.random_normal([1,latent_dim]) else: h_con_enc=tf.concat([hs_con_dec[i-1], zs[i-1]], 0) mean, logvar=MLP1(h_con_enc) p_means.append(mean) p_logvars.append(logvar) z=sample(mean, logvar)

    h_con_dec=lat2dec(z)
    hs_con_dec.append(h_con_dec)
    p_zs.append(z)

return p_zs,p_means,p_logvars ```

```{python id:”j0ujlszk”} def kl_objective(q_means,q_logvars,p_means,p_logvars,mask_weights):


```{python id:"j0ujq89m"}
def lat2dec(z):
# w :[latent_dim,context_level_hidden_dim]
# b :[context_level_hidden_dim]

    return zw+b

```{python id:”j0uk4nk1”} def learning_X_lat2dec(q_zs): hs_con_dec=lat2dec(q_zs) return hs_con_dec


```{python  id:"j0ukal0c"}
def word_level_decoder(hs_con_dec,decoder_inputs):
# decoder_inputs : [batch_size,max_seq_length+1]
    for i in range(batch_size):
        for j in range(max_seq_length+1):
            decoder_inputs[i][j]=tf.concat(decoder_inputs[i][j],hs_con_dec[i])
    #GRUCell4=tf.nn.rnn_cell.GRUCell(word_level_hidden_dim+sentence_dim)   
    return logits

{python id:"j0ukal0c"} def reconstruct_objective(logits,groud_truth_inputs,mask_weights):