def prior(name, y_onehot, hps): with tf.variable_scope(name): n_z = hps.top_shape[-1] h = tf.zeros([tf.shape(y_onehot)[0]]+hps.top_shape[:2]+[2*n_z]) if hps.learntop: h = Z.conv2d_zeros('p', h, 2*n_z) if hps.ycond: h += tf.reshape(Z.linear_zeros("y_emb", y_onehot, 2*n_z), [-1, 1, 1, 2 * n_z]) pz = Z.gaussian_diag(h[:, :, :, :n_z], h[:, :, :, n_z:]) def logp(z1): objective = pz.logp(z1) return objective def sample(eps=None, eps_std=None): if eps is not None: # Already sampled eps. Don't use eps_std z = pz.sample2(eps) elif eps_std is not None: # Sample with given eps_std z = pz.sample2(pz.eps * tf.reshape(eps_std, [-1, 1, 1, 1])) else: # Sample normally z = pz.sample return z def eps(z1): return pz.get_eps(z1) return logp, sample, eps
def split2d_prior(z): n_z2 = int(z.get_shape()[3]) n_z1 = n_z2 h = Z.conv2d_zeros("conv", z, 2 * n_z1) mean = h[:, :, :, 0::2] logs = h[:, :, :, 1::2] return Z.gaussian_diag(mean, logs)
def split2d_prior(z, hps): shape = Z.int_shape(z) n_z2 = int(z.get_shape()[3]) n_z1 = n_z2 h = tf.zeros([tf.shape(z)[0]] + shape[1:3] + [2 * n_z1]) if hps.learnprior: h = Z.conv2d_zeros("conv", z, 2 * n_z1) mean = h[:, :, :, 0::2] logs = h[:, :, :, 1::2] return Z.gaussian_diag(mean, logs)
def split3d_prior(y, shape, z_prior, level): n_z = shape[-1] h = tf.zeros([shape[0]] + shape[1:4] + [2 * n_z]) mean = h[:, :, :, :, :n_z] logsd = h[:, :, :, :, n_z:] if y is not None: temp_v = Z.linear_zeros("y_emb", y, n_z) mean += tf.reshape(temp_v, [-1, 1, 1, 1, n_z]) if z_prior is not None: mean, logsd = Z.condFun(mean, logsd, z_prior, level) # n_z2 = int(z.get_shape()[3]) # n_z1 = n_z2 # h = Z.conv2d_zeros("conv", z, 2 * n_z1) # # mean = h[:, :, :, 0::2] # logs = h[:, :, :, 1::2] return Z.gaussian_diag(mean, logsd)
def _create_prior(self, z): '''Create a unit normal Gaussian object with same shape as z.''' mu = tf.zeros_like(z, dtype='float32') logs = tf.zeros_like(z, dtype='float32') return Z.gaussian_diag(mu, logs)