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

Updating variable names to be accurate #146

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
26 changes: 13 additions & 13 deletions contextualized_topic_models/networks/decoding_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ def reparameterize(mu, logvar):
def forward(self, x, x_bert, labels=None):
"""Forward pass."""
# batch_size x n_components
posterior_mu, posterior_log_sigma = self.inf_net(x, x_bert, labels)
posterior_sigma = torch.exp(posterior_log_sigma)
posterior_mu, posterior_log_variance = self.inf_net(x, x_bert, labels)
posterior_variance = torch.exp(posterior_log_variance)

# generate samples from theta
theta = F.softmax(self.reparameterize(posterior_mu, posterior_log_sigma), dim=1)
theta = F.softmax(self.reparameterize(posterior_mu, posterior_log_variance), dim=1)
theta = self.drop_theta(theta)

# prodLDA vs LDA
Expand Down Expand Up @@ -156,39 +156,39 @@ def forward(self, x, x_bert, labels=None):
self.prior_mean,
self.prior_variance,
posterior_mu,
posterior_sigma,
posterior_log_sigma,
posterior_variance,
posterior_log_variance,
word_dist,
estimated_labels,
)

def get_posterior(self, x, x_bert, labels=None):
"""Get posterior distribution."""
# batch_size x n_components
posterior_mu, posterior_log_sigma = self.inf_net(x, x_bert, labels)
posterior_mu, posterior_log_variance = self.inf_net(x, x_bert, labels)

return posterior_mu, posterior_log_sigma
return posterior_mu, posterior_log_variance

def get_theta(self, x, x_bert, labels=None):
with torch.no_grad():
# batch_size x n_components
posterior_mu, posterior_log_sigma = self.get_posterior(x, x_bert, labels)
# posterior_sigma = torch.exp(posterior_log_sigma)
posterior_mu, posterior_log_variance = self.get_posterior(x, x_bert, labels)
# posterior_variance = torch.exp(posterior_log_variance)

# generate samples from theta
theta = F.softmax(
self.reparameterize(posterior_mu, posterior_log_sigma), dim=1
self.reparameterize(posterior_mu, posterior_log_variance), dim=1
)

return theta

def sample(self, posterior_mu, posterior_log_sigma, n_samples: int = 20):
def sample(self, posterior_mu, posterior_log_variance, n_samples: int = 20):
with torch.no_grad():
posterior_mu = posterior_mu.unsqueeze(0).repeat(n_samples, 1, 1)
posterior_log_sigma = posterior_log_sigma.unsqueeze(0).repeat(n_samples, 1, 1)
posterior_log_variance = posterior_log_variance.unsqueeze(0).repeat(n_samples, 1, 1)
# generate samples from theta
theta = F.softmax(
self.reparameterize(posterior_mu, posterior_log_sigma), dim=-1
self.reparameterize(posterior_mu, posterior_log_variance), dim=-1
)

return theta.mean(dim=0)
16 changes: 8 additions & 8 deletions contextualized_topic_models/networks/inference_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def __init__(self, input_size, bert_size, output_size, hidden_sizes,
self.f_mu = nn.Linear(hidden_sizes[-1], output_size)
self.f_mu_batchnorm = nn.BatchNorm1d(output_size, affine=False)

self.f_sigma = nn.Linear(hidden_sizes[-1], output_size)
self.f_sigma_batchnorm = nn.BatchNorm1d(output_size, affine=False)
self.f_variance = nn.Linear(hidden_sizes[-1], output_size)
self.f_variance_batchnorm = nn.BatchNorm1d(output_size, affine=False)

self.dropout_enc = nn.Dropout(p=self.dropout)

Expand All @@ -67,9 +67,9 @@ def forward(self, x, x_bert, labels=None):
x = self.hiddens(x)
x = self.dropout_enc(x)
mu = self.f_mu_batchnorm(self.f_mu(x))
log_sigma = self.f_sigma_batchnorm(self.f_sigma(x))
log_variance = self.f_variance_batchnorm(self.f_variance(x))

return mu, log_sigma
return mu, log_variance


class CombinedInferenceNetwork(nn.Module):
Expand Down Expand Up @@ -119,8 +119,8 @@ def __init__(self, input_size, bert_size, output_size, hidden_sizes,
self.f_mu = nn.Linear(hidden_sizes[-1], output_size)
self.f_mu_batchnorm = nn.BatchNorm1d(output_size, affine=False)

self.f_sigma = nn.Linear(hidden_sizes[-1], output_size)
self.f_sigma_batchnorm = nn.BatchNorm1d(output_size, affine=False)
self.f_variance = nn.Linear(hidden_sizes[-1], output_size)
self.f_variance_batchnorm = nn.BatchNorm1d(output_size, affine=False)

self.dropout_enc = nn.Dropout(p=self.dropout)

Expand All @@ -139,6 +139,6 @@ def forward(self, x, x_bert, labels=None):
x = self.hiddens(x)
x = self.dropout_enc(x)
mu = self.f_mu_batchnorm(self.f_mu(x))
log_sigma = self.f_sigma_batchnorm(self.f_sigma(x))
log_variance = self.f_variance_batchnorm(self.f_variance(x))

return mu, log_sigma
return mu, log_variance