def q_net(x, n_h, n_particles): with zs.BayesianNet() as proposal: h1_logits = layers.fully_connected(tf.to_float(x), n_h, activation_fn=None) h1 = zs.Bernoulli('h1', h1_logits, n_samples=n_particles, group_ndims=1, dtype=tf.float32) h2_logits = layers.fully_connected(h1, n_h, activation_fn=None) h2 = zs.Bernoulli('h2', h2_logits, group_ndims=1, dtype=tf.float32) h3_logits = layers.fully_connected(h2, n_h, activation_fn=None) h3 = zs.Bernoulli('h3', h3_logits, group_ndims=1, dtype=tf.float32) return proposal
def sbn(observed, n, n_x, n_h, n_particles): with zs.BayesianNet(observed=observed) as model: h3_logits = tf.zeros([n, n_h]) h3 = zs.Bernoulli('h3', h3_logits, n_samples=n_particles, group_ndims=1, dtype=tf.float32) h2_logits = layers.fully_connected(h3, n_h, activation_fn=None) h2 = zs.Bernoulli('h2', h2_logits, group_ndims=1, dtype=tf.float32) h1_logits = layers.fully_connected(h2, n_h, activation_fn=None) h1 = zs.Bernoulli('h1', h1_logits, group_ndims=1, dtype=tf.float32) x_logits = layers.fully_connected(h1, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def q_net(x, n_h, n_particles): with zs.BayesianNet() as variational: h1_logits = tf.layers.dense(tf.to_float(x), n_h) h1 = zs.Bernoulli('h1', h1_logits, n_samples=n_particles, group_ndims=1, dtype=tf.float32) h2_logits = tf.layers.dense(h1, n_h) h2 = zs.Bernoulli('h2', h2_logits, group_ndims=1, dtype=tf.float32) h3_logits = tf.layers.dense(h2, n_h) h3 = zs.Bernoulli('h3', h3_logits, group_ndims=1, dtype=tf.float32) return variational
def sbn(observed, n, n_x, n_h, n_particles): with zs.BayesianNet(observed=observed) as model: h3_logits = tf.zeros([n, n_h]) h3 = zs.Bernoulli('h3', h3_logits, n_samples=n_particles, group_ndims=1, dtype=tf.float32) h2_logits = tf.layers.dense(h3, n_h) h2 = zs.Bernoulli('h2', h2_logits, group_ndims=1, dtype=tf.float32) h1_logits = tf.layers.dense(h2, n_h) h1 = zs.Bernoulli('h1', h1_logits, group_ndims=1, dtype=tf.float32) x_logits = tf.layers.dense(h1, n_x) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae_conv(observed, n, n_x, n_z, n_particles, is_training): with zs.BayesianNet(observed=observed) as model: normalizer_params = {'is_training': is_training, 'updates_collections': None} z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., n_samples=n_particles, group_event_ndims=1) lx_z = tf.reshape(z, [-1, 1, 1, n_z]) lx_z = layers.conv2d_transpose( lx_z, 128, kernel_size=3, padding='VALID', normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) lx_z = layers.conv2d_transpose( lx_z, 64, kernel_size=5, padding='VALID', normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) lx_z = layers.conv2d_transpose( lx_z, 32, kernel_size=5, stride=2, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) lx_z = layers.conv2d_transpose( lx_z, 1, kernel_size=5, stride=2, activation_fn=None) x_logits = tf.reshape(lx_z, [n_particles, n, -1]) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return model
def vae(observed, n, n_x, n_z, n_samples, is_training): with zs.BayesianNet(observed=observed) as model: normalizer_params = {'is_training': is_training, 'updates_collections': None} z_mean = tf.zeros([n, n_z]) # the mean of z is the zero vector z_std = 1. # the covariance of z is the identity matrix, here a scalar is sufficient because it will be broadcasted to a vector and then used as the diagonal of the covariance matrix in zhusuan ''' TODO1: sampling z using the Gaussian distribution of zhusuan > given input - z_mean, z_std, n_samples - set group_event_ndims as 1 > e.g. - x = zs.Bernoulli('x', mu, group_event_ndims=1) ''' lx_z_1 = layers.fully_connected( z, 500, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) # a mlp layer of 500 hidden units with z as the input ''' TODO2: add one more mlp layer with 500 hidden units > given input - lx_z_1, size of hidden units, normalizer_params - add batch_norm as the normalizer_fn > e.g. see the above line ''' x_logits = layers.fully_connected(lx_z_2, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return model
def vae(observed, n, x_dim, z_dim, n_particles, is_training): with zs.BayesianNet(observed=observed) as model: z_logits = tf.zeros([n, z_dim]) z = zs.Bernoulli('z', z_logits, group_ndims=1, n_samples=n_particles, dtype=tf.float32) lx_z = tf.layers.dense(z, 500, use_bias=False) lx_z = tf.layers.batch_normalization(lx_z, training=is_training) lx_z = tf.nn.relu(lx_z) lx_z = tf.layers.dense(lx_z, 500, use_bias=False) lx_z = tf.layers.batch_normalization(lx_z, training=is_training) lx_z = tf.nn.relu(lx_z) x_logits = tf.layers.dense(lx_z, x_dim) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae(observed, n, n_x, n_z): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., group_event_ndims=1) lx_z = layers.fully_connected(z, 500) lx_z = layers.fully_connected(lx_z, 500) x_logits = layers.fully_connected(lx_z, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return model, x_logits
def q_net(x, n_h, n_particles, is_training): with zs.BayesianNet() as variational: h1_logits = layers.fully_connected(tf.to_float(x), n_h, activation_fn=None) h1 = zs.Bernoulli('h1', h1_logits, n_samples=n_particles, group_event_ndims=1) h2_logits = layers.fully_connected(tf.to_float(h1), n_h, activation_fn=None) h2 = zs.Bernoulli('h2', h2_logits, group_event_ndims=1) h3_logits = layers.fully_connected(tf.to_float(h2), n_h, activation_fn=None) h3 = zs.Bernoulli('h3', h3_logits, group_event_ndims=1) return variational
def vae(observed, x_dim, z_dim, n, n_particles=1): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) lx_z = tf.layers.dense(z, 500, activation=tf.nn.relu) lx_z = tf.layers.dense(lx_z, 500, activation=tf.nn.relu) x_logits = tf.layers.dense(lx_z, x_dim) x_mean = zs.Implicit("x_mean", tf.sigmoid(x_logits), group_ndims=1) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae(observed, n, n_x, n_z, n_particles, is_training): with zs.BayesianNet(observed=observed) as model: normalizer_params = { 'is_training': is_training, 'updates_collections': None } z_logits = tf.zeros([n, n_z]) z = zs.Bernoulli('z', z_logits, n_samples=n_particles, group_ndims=1) lx_z = layers.fully_connected(tf.to_float(z), 500, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) lx_z = layers.fully_connected(lx_z, 500, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) x_logits = layers.fully_connected(lx_z, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae(observed, x_dim, z_dim, n, y): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1) z_plus_y = tf.concat([z, tf.cast(y, tf.float32)], axis=1) lx_z = tf.layers.dense(z_plus_y, 500, activation=tf.nn.relu) lx_z = tf.layers.dense(lx_z, 500, activation=tf.nn.relu) x_logits = tf.layers.dense(lx_z, x_dim) x_mean = zs.Implicit("x_mean", tf.sigmoid(x_logits), group_ndims=1) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model, x_mean
def sbn(observed, n, n_x, n_h, n_particles, is_training): with zs.BayesianNet(observed=observed) as model: h3_logits = tf.zeros([n, n_h]) h3 = zs.Bernoulli('h3', h3_logits, n_samples=n_particles, group_event_ndims=1) h2_logits = layers.fully_connected(tf.to_float(h3), n_h, activation_fn=None) h2 = zs.Bernoulli('h2', h2_logits, group_event_ndims=1) h1_logits = layers.fully_connected(tf.to_float(h2), n_h, activation_fn=None) h1 = zs.Bernoulli('h1', h1_logits, group_event_ndims=1) x_logits = layers.fully_connected(tf.to_float(h1), n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return model
def p_net(observed, n, x_dim, z_dim): ''' Decoder: p(x|z) ''' with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1) lx_z = layers.fully_connected(z, 500) lx_z = layers.fully_connected(lx_z, 500) x_logits = layers.fully_connected(lx_z, x_dim, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def M2(observed, n, n_x, n_y, n_z, n_particles): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., n_samples=n_particles, group_ndims=1) y_logits = tf.zeros([n, n_y]) y = zs.OnehotCategorical('y', y_logits, n_samples=n_particles) lx_zy = layers.fully_connected(tf.concat([z, tf.to_float(y)], 2), 500) lx_zy = layers.fully_connected(lx_zy, 500) x_logits = layers.fully_connected(lx_zy, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae(observed, n, x_dim, z_dim, n_particles): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) lx_z = layers.fully_connected(z, 500) lx_z = layers.fully_connected(lx_z, 500) x_logits = layers.fully_connected(lx_z, x_dim, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model, x_logits
def wae(observed, n, n_x, n_z, n_particles): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) lx_z = tf.layers.dense(z, units=256, activation=tf.nn.relu) lx_z = tf.layers.dense(lx_z, units=256, activation=tf.nn.relu) x = tf.layers.dense(lx_z, units=784, activation=None) zs.Bernoulli('x', x, group_ndims=1, dtype=tf.int32) return model, z, tf.sigmoid(x)
def vae(observed, n, n_x, n_z, n_particles): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., n_samples=n_particles, group_ndims=1) lx_z = tf.layers.dense(z, 500, activation=tf.nn.relu) lx_z = tf.layers.dense(lx_z, 500, activation=tf.nn.relu) x_logits = tf.layers.dense(lx_z, n_x) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def q_net(x, z_dim, n_particles, is_training): with zs.BayesianNet() as variational: lz_x = tf.layers.dense(tf.to_float(x), 500, use_bias=False) lz_x = tf.layers.batch_normalization(lz_x, training=is_training) lz_x = tf.nn.relu(lz_x) lz_x = tf.layers.dense(lz_x, 500, use_bias=False) lz_x = tf.layers.batch_normalization(lz_x, training=is_training) lz_x = tf.nn.relu(lz_x) z_logits = tf.layers.dense(lz_x, z_dim) z = zs.Bernoulli('z', z_logits, group_ndims=1, n_samples=n_particles, dtype=tf.float32) return variational
def cvae(observed, x_dim, y_dim, z_dim, n, n_particles=1): with zs.BayesianNet(observed=observed) as model: y = zs.Empirical('y', tf.int32, (n, y_dim)) z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) z = tf.to_float(z[0]) yz = tf.concat([tf.to_float(y), z], axis=1) lx_yz = tf.layers.dense(tf.to_float(yz), 500, activation=tf.nn.relu) lx_yz = tf.layers.dense(lx_yz, 500, activation=tf.nn.relu) x_logits = tf.layers.dense(lx_yz, x_dim) x_mean = zs.Implicit('x_mean', tf.sigmoid(x_logits), group_ndims=1) x = zs.Bernoulli('x', logits=x_logits, group_ndims=1) return model
def vae(observed, n, n_x, n_z, n_particles): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) lx_z = layers.fully_connected(z, 7 * 7 * ngf * 2) lx_z = tf.reshape(lx_z, [-1, 7, 7, ngf * 2]) lx_z = deconv_res_block(lx_z, [7, 7, ngf * 2]) lx_z = deconv_res_block(lx_z, [14, 14, ngf * 2], resize=True) lx_z = deconv_res_block(lx_z, [14, 14, ngf * 2]) lx_z = deconv_res_block(lx_z, [28, 28, ngf], resize=True) lx_z = deconv_res_block(lx_z, [28, 28, ngf]) lx_z = conv2d_transpose(lx_z, [28, 28, 1], kernel_size=(3, 3), stride=(1, 1), activation_fn=None) x_logits = tf.reshape(lx_z, [n_particles, -1, n_x]) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model, x_logits
def q_net(x, n_z, n_particles, is_training): with zs.BayesianNet() as variational: normalizer_params = { 'is_training': is_training, 'updates_collections': None } lz_x = layers.fully_connected(tf.to_float(x), 500, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) lz_x = layers.fully_connected(lz_x, 500, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params) z_logits = layers.fully_connected(lz_x, n_z, activation_fn=None) z = zs.Bernoulli('z', z_logits, n_samples=n_particles, group_ndims=1) return variational
def vae(observed, x_dim, z_dim, n, n_particles=1): with zs.BayesianNet(observed=observed) as model: y = zs.OnehotCategorical('y', logits=tf.ones([n, 10]), group_ndims=1, n_samples=n_particles) y = tf.to_float(y) y = tf.reshape(y, (1, n, 10)) z_mean = tf.zeros([n, z_dim]) z = zs.Normal('z', z_mean, std=1., group_ndims=1, n_samples=n_particles) z = tf.concat([z, y], 2) lx_z = tf.layers.dense(z, 500, activation=tf.nn.relu) lx_z = tf.layers.dense(lx_z, 500, activation=tf.nn.relu) x_logits = tf.layers.dense(lx_z, x_dim) x_mean = zs.Implicit("x_mean", tf.sigmoid(x_logits), group_ndims=1) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model
def vae(observed, n, n_x, n_h, n_z): with zs.BayesianNet(observed=observed) as model: pi = tf.get_variable(name='pi', dtype=tf.float32, initializer=tf.truncated_normal([1, n_z], stddev=0.1)) zpi = tf.tile(pi, [n, 1]) # zpi = tf.zeros([n, n_z]) z = zs.OnehotDiscrete('z', zpi, group_ndims=0) h_mean = layers.fully_connected(tf.to_float(z), n_h, activation_fn=None) h_logstd = layers.fully_connected(tf.to_float(z), n_h, activation_fn=None) h = zs.Normal('h', h_mean, logstd=h_logstd, group_ndims=1) lx_h = layers.fully_connected(h, 512) lx_h = layers.fully_connected(lx_h, 512) x_logits = layers.fully_connected(lx_h, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_ndims=1) return model, x_logits, pi
def vae(observed, n, y, is_training): with zs.BayesianNet(observed=observed) as decoder: normalizer_params = {'is_training': is_training, 'updates_collections': None} z_mean = tf.zeros([n, n_z]) z = zs.Normal('z', z_mean, std=1., n_samples=1, group_event_ndims=1) z = tf.reshape(z,[-1 ,n_z]) yb = tf.reshape(y, [-1, 1, 1, n_code]) lx_z = layers.fully_connected(tf.concat([z, y], 1), 1024, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02) ) lx_z = layers.fully_connected(tf.concat([lx_z, y], 1), ngf * 8 * 4 * 4, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02) ) lx_z = tf.reshape(lx_z, [-1, 4, 4, ngf * 8]) # assert tf.shape(lx_z)[0] == n lx_z = layers.conv2d_transpose(conv_cond_concat(lx_z, yb), ngf * 4, 5, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) lx_z = layers.conv2d_transpose(conv_cond_concat(lx_z, yb), ngf * 2, 5, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) lx_z = layers.conv2d_transpose(conv_cond_concat(lx_z, yb), ngf * 1, 5, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) lx_z = layers.conv2d_transpose(conv_cond_concat(lx_z, yb), 1, 5, stride=2, activation_fn=None, weights_initializer=init_ops.RandomNormal(stddev=0.02)) x_logits = tf.reshape(lx_z, [1, -1, n_x]) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return decoder, x_logits
def vae_conv(observed, n, x_dim, z_dim, n_particles, nf=16): with zs.BayesianNet(observed=observed) as model: z_mean = tf.zeros([n, z_dim]) z = zs.Normal("z", z_mean, std=1., group_ndims=1, n_samples=n_particles) lx_z = tf.layers.dense(z, 7 * 7 * nf * 2, activation=tf.nn.relu) lx_z = tf.reshape(lx_z, [-1, 7, 7, nf * 2]) lx_z = deconv_resnet_block(lx_z, [7, 7, nf * 2]) lx_z = deconv_resnet_block(lx_z, [14, 14, nf * 2], resize=True) lx_z = deconv_resnet_block(lx_z, [14, 14, nf * 2]) lx_z = deconv_resnet_block(lx_z, [28, 28, nf], resize=True) lx_z = deconv_resnet_block(lx_z, [28, 28, nf]) lx_z = conv2d_transpose(lx_z, [28, 28, 1], kernel_size=(3, 3), stride=(1, 1), activation_fn=None) x_logits = tf.reshape(lx_z, [n_particles, -1, x_dim]) x = zs.Bernoulli("x", x_logits, group_ndims=1) return model, x_logits
def vae(observed, batch_size, n_x, n_h, n_z, n_particles): with zs.BayesianNet(observed=observed) as model: log_pi = tf.get_variable('log_pi', n_z, initializer=tf.zeros_initializer()) mu = tf.get_variable('mu', [n_h, n_z], initializer=tf.random_uniform_initializer(-1, 1)) log_sigma = tf.get_variable('log_sigma', [n_h, n_z], initializer=tf.random_normal_initializer( 0, 0.1)) n_log_pi = tf.tile(tf.expand_dims(log_pi, 0), [batch_size, 1]) # (batch_size, n_z) z = zs.OnehotCategorical( 'z', n_log_pi, n_samples=n_particles, group_event_ndims=0) # (n_particles, batch_size, n_z) z_tensor = tf.reshape(z.tensor, [-1, n_z]) h_mean = tf.matmul(tf.to_float(z_tensor), tf.transpose( mu)) # (n_particles x batch_size, n_z) OneHot val_shape [n_z] h_logstd = tf.matmul(tf.to_float(z_tensor), tf.transpose(log_sigma)) h_mean = tf.reshape( h_mean, [-1, batch_size, n_h]) # (n_particles, batch_size, n_h) h_logstd = tf.reshape(h_logstd, [-1, batch_size, n_h]) # returned tensor of log_prob() has shape (... + )batch_shape[:-group_event_ndims] = (n_particles, batch_size). h = zs.Normal( 'h', h_mean, h_logstd, group_event_ndims=1 ) # Multivariate Normal. val_shape []. see zhusuan Basic Concepts. lx_h = layers.fully_connected(h, 500) lx_h = layers.fully_connected(lx_h, 500) x_logits = layers.fully_connected( lx_h, n_x, activation_fn=None) # (n_particles, batch_size, n_x) x = zs.Bernoulli( 'x', x_logits, group_event_ndims=1 ) # (n_particles, batch_size, n_x) n_x=784 pixel as one event return model, x_logits, z.tensor
def vae(observed, n, n_x, n_z, n_k, tau, n_particles, relaxed=False): with zs.BayesianNet(observed=observed) as model: z_stacked_logits = tf.zeros([n, n_z, n_k]) if relaxed: z = zs.ExpConcrete('z', tau, z_stacked_logits, n_samples=n_particles, group_event_ndims=1) z = tf.exp(tf.reshape(z, [n_particles, n, n_z * n_k])) else: z = zs.OnehotCategorical('z', z_stacked_logits, dtype=tf.float32, n_samples=n_particles, group_event_ndims=1) z = tf.reshape(z, [n_particles, n, n_z * n_k]) lx_z = layers.fully_connected(z, 200, activation_fn=tf.tanh) lx_z = layers.fully_connected(lx_z, 200, activation_fn=tf.tanh) x_logits = layers.fully_connected(lx_z, n_x, activation_fn=None) x = zs.Bernoulli('x', x_logits, group_event_ndims=1) return model
def VLAE(observed, n, is_training): with zs.BayesianNet(observed=observed) as decoder: normalizer_params = { 'is_training': is_training, 'updates_collections': None } z_char_mean = tf.zeros([n, char_dim]) z_char = zs.Normal('z_char', z_char_mean, std=1., n_samples=1, group_event_ndims=1) z_char = tf.reshape(z_char, [-1, char_dim]) z_font_mean = tf.zeros([n, font_dim]) z_font = zs.Normal('z_font', z_font_mean, std=1., n_samples=1, group_event_ndims=1) z_font = tf.reshape(z_font, [-1, font_dim]) latent2 = z_char latent1 = z_font ladder2 = layers.fully_connected( latent2, ngf * 8 * 4 * 4, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) ladder2 = tf.reshape(ladder2, [-1, 4, 4, ngf * 8]) ladder2 = layers.conv2d_transpose( ladder2, ngf * 8, 4, stride=1, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) ladder2 = layers.conv2d_transpose( ladder2, ngf * 4, 4, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) inference1 = layers.conv2d_transpose( ladder2, ngf * 4, 4, stride=1, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) ladder1 = layers.fully_connected( latent1, ngf * 4 * 8 * 8, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) ladder1 = tf.reshape(ladder1, [-1, 8, 8, ngf * 4]) ladder1 = tf.concat([ladder1, inference1], 3) ladder1 = layers.conv2d_transpose( ladder1, ngf * 2, 4, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) ladder1 = layers.conv2d_transpose( ladder1, ngf * 2, 4, stride=1, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) inference0 = layers.conv2d_transpose( ladder1, ngf, 4, stride=2, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) # ladder0 = layers.fully_connected(latent0, ngf * 1 * 32 * 32, activation_fn=tf.nn.relu, # normalizer_fn=layers.batch_norm, # normalizer_params=normalizer_params, # weights_initializer=init_ops.RandomNormal(stddev=0.02)) # ladder0 = tf.reshape(ladder0, [-1, 32, 32, ngf * 1]) # ladder0 = tf.concat([ladder0, inference0], 3) ladder0 = layers.conv2d_transpose( inference0, ngf, 4, stride=1, activation_fn=tf.nn.relu, normalizer_fn=layers.batch_norm, normalizer_params=normalizer_params, weights_initializer=init_ops.RandomNormal(stddev=0.02)) x_logits = layers.conv2d_transpose( ladder0, 1, 4, stride=2, activation_fn=tf.identity, weights_initializer=init_ops.RandomNormal(stddev=0.02)) x_logits = tf.reshape(x_logits, [1, -1, n_x]) x = zs.Bernoulli('x', x_logits, n_samples=1, group_event_ndims=1) return decoder, x_logits
def vae(observed, n, n_particles, is_training, dim_h=40, dim_z=10, dim_x=784): '''decoder: z-->h-->x n: batch_size dim_z: K = 10 dim_x: 784 dim_h: D = 40 ''' with zs.BayesianNet(observed=observed) as model: normalizer_params = { 'is_training': is_training, 'updates_collections': None } pai = tf.get_variable('pai', shape=[dim_z], dtype=tf.float32, trainable=True, initializer=tf.constant_initializer(1.0)) n_pai = tf.tile(tf.expand_dims(pai, 0), [n, 1]) z = zs.OnehotCategorical('z', logits=n_pai, dtype=tf.float32, n_samples=n_particles) mu = tf.get_variable('mu', shape=[dim_z, dim_h], dtype=tf.float32, initializer=tf.random_uniform_initializer( -1, 1)) log_sigma = tf.get_variable( 'log_sigma', shape=[dim_z, dim_h], dtype=tf.float32, initializer=tf.random_uniform_initializer(-3, -2)) h_mean = tf.reshape( tf.matmul(tf.reshape(z, [-1, dim_z]), mu), [n_particles, -1, dim_h]) # [n_particles, None, dim_x] h_logstd = tf.reshape( tf.matmul(tf.reshape(z, [-1, dim_z]), log_sigma), [n_particles, -1, dim_h]) h = zs.Normal( 'h', mean=h_mean, logstd=h_logstd, #n_samples=n_particles, group_event_ndims=1) lx_h = layers.fully_connected( h, 512, # normalizer_fn=layers.batch_norm, # normalizer_params=normalizer_params ) lx_h = layers.fully_connected( lx_h, 512, # normalizer_fn=layers.batch_norm, # normalizer_params=normalizer_params ) x_logits = layers.fully_connected( lx_h, dim_x, activation_fn=None) # the log odds of being 1 x = zs.Bernoulli( 'x', x_logits, #n_samples=n_particles, group_event_ndims=1) return model, x_logits, h, z.tensor