Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vae_ssl: variable name issue and n not match #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions examples/semi_supervised_vae/vae_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,31 @@ def main():

# Labeled
x_labeled_ph = tf.placeholder(tf.float32, shape=[None, x_dim], name="x_l")
x_labeled = tf.cast(
x_labeled_bin = tf.cast(
tf.less(tf.random_uniform(tf.shape(x_labeled_ph)), x_labeled_ph),
tf.int32)
y_labeled_ph = tf.placeholder(tf.int32, shape=[None, n_class], name="y_l")
variational = qz_xy(x_labeled, y_labeled_ph, z_dim, n_particles)
variational = qz_xy(x_labeled_bin, y_labeled_ph, z_dim, n_particles)

labeled_lower_bound = tf.reduce_mean(
zs.variational.elbo(model,
observed={"x": x_labeled, "y": y_labeled_ph},
observed={"x": x_labeled_bin, "y": y_labeled_ph},
variational=variational,
axis=0))

# Unlabeled
# TODO: n not match.

unlabeled_model = build_gen(n * n_class, x_dim, n_class, z_dim, n_particles)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd better not rebuild the model? other workaround possible? @csy530216

x_unlabeled_ph = tf.placeholder(tf.float32, shape=[None, x_dim],
name="x_u")
x_unlabeled = tf.cast(
x_unlabeled_bin = tf.cast(
tf.less(tf.random_uniform(tf.shape(x_unlabeled_ph)), x_unlabeled_ph),
tf.int32)
y_diag = tf.eye(n_class, dtype=tf.int32)
y_u = tf.reshape(tf.tile(y_diag[None, ...], [n, 1, 1]), [-1, n_class])
x_u = tf.reshape(tf.tile(x_unlabeled[:, None, ...], [1, n_class, 1]),
x_u = tf.reshape(tf.tile(x_unlabeled_bin[:, None, ...], [1, n_class, 1]),
[-1, x_dim])
variational = qz_xy(x_u, y_u, z_dim, n_particles)
lb_z = zs.variational.elbo(model,
lb_z = zs.variational.elbo(unlabeled_model,
observed={"x": x_u, "y": y_u},
variational=variational,
axis=0)
Expand All @@ -124,12 +123,12 @@ def main():
tf.reduce_sum(qy_u * (lb_z - log_qy_u), 1))

# Build classifier
qy_logits_l = qy_x(x_labeled, n_class)
qy_logits_l = qy_x(x_labeled_bin, n_class)
qy_l = tf.nn.softmax(qy_logits_l)
pred_y = tf.argmax(qy_l, 1)
acc = tf.reduce_sum(
tf.cast(tf.equal(pred_y, tf.argmax(y_labeled_ph, 1)), tf.float32) /
tf.cast(tf.shape(x_labeled)[0], tf.float32))
tf.cast(tf.shape(x_labeled_bin)[0], tf.float32))
onehot_cat = zs.distributions.OnehotCategorical(qy_logits_l)
log_qy_x = onehot_cat.log_prob(y_labeled_ph)
classifier_cost = -beta * tf.reduce_mean(log_qy_x)
Expand Down Expand Up @@ -185,9 +184,9 @@ def main():
t * test_batch_size: (t + 1) * test_batch_size]
test_ll_labeled, test_ll_unlabeled, test_acc = sess.run(
[labeled_lower_bound, unlabeled_lower_bound, acc],
feed_dict={x_labeled: test_x_batch,
feed_dict={x_labeled_ph: test_x_batch,
y_labeled_ph: test_y_batch,
x_unlabeled: test_x_batch,
x_unlabeled_ph: test_x_batch,
n_particles: lb_samples,
n: test_batch_size})
test_lls_labeled.append(test_ll_labeled)
Expand Down
24 changes: 12 additions & 12 deletions examples/semi_supervised_vae/vae_ssl_adaptive_is.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,55 +91,55 @@ def main():

# Labeled
x_labeled_ph = tf.placeholder(tf.float32, shape=[None, x_dim], name="x_l")
x_labeled = tf.cast(
x_labeled_bin = tf.cast(
tf.less(tf.random_uniform(tf.shape(x_labeled_ph)), x_labeled_ph),
tf.int32)
y_labeled_ph = tf.placeholder(tf.int32, shape=[None, n_class], name="y_l")
proposal = labeled_proposal(x_labeled, y_labeled_ph, z_dim, n_particles)
proposal = labeled_proposal(x_labeled_bin, y_labeled_ph, z_dim, n_particles)

# adapting the proposal
labeled_klpq_obj = zs.variational.klpq(
model,
observed={"x": x_labeled, "y": y_labeled_ph},
observed={"x": x_labeled_bin, "y": y_labeled_ph},
variational=proposal,
axis=0)
labeled_q_cost = tf.reduce_mean(labeled_klpq_obj.importance())

# learning model parameters
labeled_lower_bound = tf.reduce_mean(
zs.variational.importance_weighted_objective(
model, observed={'x': x_labeled, 'y': y_labeled_ph},
model, observed={'x': x_labeled_bin, 'y': y_labeled_ph},
variational=proposal, axis=0))

# Unlabeled
x_unlabeled_ph = tf.placeholder(tf.float32, shape=[None, x_dim],
name="x_u")
x_unlabeled = tf.cast(
x_unlabeled_bin = tf.cast(
tf.less(tf.random_uniform(tf.shape(x_unlabeled_ph)), x_unlabeled_ph),
tf.int32)
proposal = unlabeled_proposal(x_unlabeled, n_class, z_dim, n_particles)
proposal = unlabeled_proposal(x_unlabeled_bin, n_class, z_dim, n_particles)

# adapting the proposal
unlabeled_klpq_obj = zs.variational.klpq(
model,
observed={'x': x_unlabeled},
observed={'x': x_unlabeled_bin},
variational=proposal,
axis=0)
unlabeled_q_cost = tf.reduce_mean(unlabeled_klpq_obj.importance())

# learning model parameters
unlabeled_lower_bound = tf.reduce_mean(
zs.variational.importance_weighted_objective(
model, observed={'x': x_unlabeled}, variational=proposal,
model, observed={'x': x_unlabeled_bin}, variational=proposal,
axis=0))

# Build classifier
qy_logits_l = qy_x(x_labeled, n_class)
qy_logits_l = qy_x(x_labeled_bin, n_class)
qy_l = tf.nn.softmax(qy_logits_l)
pred_y = tf.argmax(qy_l, 1)
acc = tf.reduce_sum(
tf.cast(tf.equal(pred_y, tf.argmax(y_labeled_ph, 1)), tf.float32) /
tf.cast(tf.shape(x_labeled)[0], tf.float32))
tf.cast(tf.shape(x_labeled_bin)[0], tf.float32))
onehot_cat = zs.distributions.OnehotCategorical(qy_logits_l)
log_qy_x = onehot_cat.log_prob(y_labeled_ph)
classifier_cost = -beta * tf.reduce_mean(log_qy_x)
Expand Down Expand Up @@ -211,9 +211,9 @@ def main():
t * test_batch_size: (t + 1) * test_batch_size]
test_ll_labeled, test_ll_unlabeled, test_acc = sess.run(
[labeled_lower_bound, unlabeled_lower_bound, acc],
feed_dict={x_labeled: test_x_batch,
feed_dict={x_labeled_ph: test_x_batch,
y_labeled_ph: test_y_batch,
x_unlabeled: test_x_batch,
x_unlabeled_ph: test_x_batch,
n_particles: ll_samples,
n: test_batch_size})
test_lls_labeled.append(test_ll_labeled)
Expand Down