Guest User

Untitled

a guest
Jan 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. # represents a batch of training data
  2. self.X = tf.placeholder(tf.float32, shape=(None, D))
  3.  
  4. # encoder
  5. self.encoder_layers = []
  6. M_in = D
  7. for M_out in hidden_layer_sizes[:-1]:
  8. h = DenseLayer(M_in, M_out)
  9. self.encoder_layers.append(h)
  10. M_in = M_out
  11.  
  12.  
  13.  
  14. # for convenience, we'll refer to the final encoder size as M
  15. # also the input to the decoder size
  16. M = hidden_layer_sizes[-1]
  17.  
  18. h = DenseLayer(M_in, 2 * M, f=lambda x: x)
  19. self.encoder_layers.append(h)
  20.  
  21. # get the mean and variance / std dev of Z.
  22. # note that the variance must be > 0
  23. # we can get a sigma (standard dev) > 0 from an unbounded variable by
  24. # passing it through the softplus function.
  25. # add a small amount for smoothing.
  26. current_layer_value = self.X
  27. for layer in self.encoder_layers:
  28. current_layer_value = layer.forward(current_layer_value)
  29. self.means = current_layer_value[:, :M]
  30. self.stddev = tf.nn.softplus(current_layer_value[:, M:]) + 1e-6
  31.  
  32. # get a sample of Z
  33. with st.value_type(st.SampleValue()):
  34. self.Z = st.StochasticTensor(Normal(loc=self.means, scale=self.stddev))
  35.  
  36.  
  37. # decoder
  38. self.decoder_layers = []
  39. M_in = M
  40. for M_out in reversed(hidden_layer_sizes[:-1]):
  41. h = DenseLayer(M_in, M_out)
  42. self.decoder_layers.append(h)
  43. M_in = M_out
  44. # since there needs to be M_out means + M_out variances
  45.  
  46.  
  47. h = DenseLayer(M_in, D, f=lambda x: x)
  48. self.decoder_layers.append(h)
  49.  
  50.  
  51. posterior_predictive_logits = logits # save for later
  52. loc=current_layer_value
  53. scale=tf.nn.softplus(current_layer_value) + 1e-6
  54.  
  55.  
  56. # get the output
  57. self.X_hat_distribution = Normal(loc,scale)
  58. # take samples from X_hat
  59. # we will call this the posterior predictive sample
  60. self.posterior_predictive = self.X_hat_distribution.sample()
  61. self.posterior_predictive_probs = tf.nn.relu(logits)
  62.  
  63. # take sample from a Z ~ N(0, 1)
  64. # and put it through the decoder
  65. # we will call this the prior predictive sample
  66. standard_normal = Normal(
  67. loc=np.zeros(M, dtype=np.float32),
  68. scale=np.ones(M, dtype=np.float32)
  69. )
  70.  
  71. Z_std = standard_normal.sample(1)
  72. current_layer_value = Z_std
  73. for layer in self.decoder_layers:
  74. current_layer_value = layer.forward(current_layer_value)
  75. #logits = current_layer_value
  76.  
  77. loc=current_layer_value
  78. scale=tf.nn.softplus(current_layer_value) + 1e-6
  79. logits = current_layer_value
  80. prior_predictive_dist = Normal(loc,scale)
  81. self.prior_predictive = prior_predictive_dist.sample()
  82. self.prior_predictive_probs = tf.nn.relu(logits)
  83.  
  84.  
  85. # prior predictive from input
  86. # only used for generating visualization
  87. self.Z_input = tf.placeholder(tf.float32, shape=(None, M))
  88. current_layer_value = self.Z_input
  89. for layer in self.decoder_layers:
  90. current_layer_value = layer.forward(current_layer_value)
  91. logits = tf.nn.relu(current_layer_value)
  92. self.prior_predictive_from_input_probs = tf.nn.relu(logits)
  93.  
  94.  
  95. # now build the cost
  96. kl = tf.reduce_sum(
  97. tf.contrib.distributions.kl_divergence(self.Z.distribution, standard_normal), 1)
  98.  
  99. expected_log_likelihood = tf.reduce_sum(self.X_hat_distribution.log_prob(self.X),1)
  100.  
  101. #expected_log_likelihood = tf.reduce_sum(self.X - self.X_hat_distribution.log_prob(self.X),1)
  102. #expected_log_likelihood = tf.reduce_sum(tf.pow(self.X - self.posterior_predictive ,2)+0.01)
  103.  
  104.  
  105.  
  106.  
  107. self.elbo = tf.reduce_sum(expected_log_likelihood - kl)
  108. self.train_op = tf.train.RMSPropOptimizer(learning_rate=0.001).minimize(-self.elbo)
  109.  
  110. # set up session and variables for later
  111. self.init_op = tf.global_variables_initializer()
  112. self.sess = tf.InteractiveSession()
  113. self.sess.run(self.init_op)
Add Comment
Please, Sign In to add comment