From e5fa5e1f348c243a71c8dd66f554e33553f5000a Mon Sep 17 00:00:00 2001 From: Akshaya Shanbhogue Date: Thu, 7 Dec 2023 23:57:00 -0800 Subject: [PATCH] Updating variable names to be accurate Specifically, the existing code is treats some variables as variance while the variables are referred to as sigma, which is the standard notation of standard deviation. Fixing the variable names to avoid confusion. --- .../networks/decoding_network.py | 26 +++++++++---------- .../networks/inference_network.py | 16 ++++++------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/contextualized_topic_models/networks/decoding_network.py b/contextualized_topic_models/networks/decoding_network.py index 4aeabf4..78b18f6 100644 --- a/contextualized_topic_models/networks/decoding_network.py +++ b/contextualized_topic_models/networks/decoding_network.py @@ -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 @@ -156,8 +156,8 @@ 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, ) @@ -165,30 +165,30 @@ def forward(self, x, x_bert, labels=None): 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) diff --git a/contextualized_topic_models/networks/inference_network.py b/contextualized_topic_models/networks/inference_network.py index f028738..94fb8ca 100644 --- a/contextualized_topic_models/networks/inference_network.py +++ b/contextualized_topic_models/networks/inference_network.py @@ -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) @@ -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): @@ -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) @@ -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