-
Notifications
You must be signed in to change notification settings - Fork 188
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 incompatible types in assignment #344
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import copy | ||
import random | ||
import timeit | ||
from typing import Any, Optional, Type, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -223,14 +224,16 @@ def do_optimizer_initializations(self, optimizer, learning_rate): | |
opt_method = optimizer.split(':')[1] | ||
|
||
# optimizater initialization | ||
self.optimizer: Optional[Union[torch.optim.Adam, torch.optim.RMSprop]] = None | ||
if opt_method == "adam": | ||
self.optimizer = torch.optim.Adam(self.cfs, lr=learning_rate) | ||
elif opt_method == "rmsprop": | ||
self.optimizer = torch.optim.RMSprop(self.cfs, lr=learning_rate) | ||
|
||
def compute_yloss(self): | ||
"""Computes the first part (y-loss) of the loss function.""" | ||
yloss = 0.0 | ||
yloss: Any = 0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, multiple types of values are stored. |
||
criterion: Optional[Union[torch.nn.BCEWithLogitsLoss, torch.nn.ReLU]] = None | ||
for i in range(self.total_CFs): | ||
if self.yloss_type == "l2_loss": | ||
temp_loss = torch.pow((self.get_model_output(self.cfs[i]) - self.target_cf_class), 2)[0] | ||
|
@@ -307,7 +310,7 @@ def compute_diversity_loss(self): | |
def compute_regularization_loss(self): | ||
"""Adds a linear equality constraints to the loss functions - | ||
to ensure all levels of a categorical variable sums to one""" | ||
regularization_loss = 0.0 | ||
regularization_loss: Any = 0.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, multiple types of values are stored. |
||
for i in range(self.total_CFs): | ||
for v in self.encoded_categorical_feature_indexes: | ||
regularization_loss += torch.pow((torch.sum(self.cfs[i][v[0]:v[-1]+1]) - 1.0), 2) | ||
|
@@ -425,7 +428,7 @@ def find_counterfactuals(self, query_instance, desired_class, optimizer, learnin | |
test_pred = self.predict_fn(torch.tensor(query_instance).float())[0] | ||
if desired_class == "opposite": | ||
desired_class = 1.0 - np.round(test_pred) | ||
self.target_cf_class = torch.tensor(desired_class).float() | ||
self.target_cf_class: Any = torch.tensor(desired_class).float() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, multiple types of values are stored. |
||
|
||
self.min_iter = min_iter | ||
self.max_iter = max_iter | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,8 +341,7 @@ def initialize_CFs(self, query_instance, init_near_query_instance=False): | |
one_init.append(np.random.uniform(self.minx[0][i], self.maxx[0][i])) | ||
else: | ||
one_init.append(query_instance[0][i]) | ||
one_init = np.array([one_init], dtype=np.float32) | ||
self.cfs[n].assign(one_init) | ||
self.cfs[n].assign(np.array([one_init], dtype=np.float32)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Substitute directly. This prevents |
||
|
||
def round_off_cfs(self, assign=False): | ||
"""function for intermediate projection of CFs.""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,8 +136,9 @@ def train(self, pre_trained=False): | |
train_loss = 0.0 | ||
train_size = 0 | ||
|
||
train_dataset = torch.tensor(self.vae_train_feat).float() | ||
train_dataset = torch.utils.data.DataLoader(train_dataset, batch_size=self.batch_size, shuffle=True) | ||
train_dataset = torch.utils.data.DataLoader( | ||
torch.tensor(self.vae_train_feat).float(), # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set |
||
batch_size=self.batch_size, shuffle=True) | ||
for train in enumerate(train_dataset): | ||
self.cf_vae_optimizer.zero_grad() | ||
|
||
|
@@ -178,8 +179,7 @@ def generate_counterfactuals(self, query_instance, total_CFs, desired_class="opp | |
final_cf_pred = [] | ||
final_test_pred = [] | ||
for i in range(len(query_instance)): | ||
train_x = test_dataset[i] | ||
train_x = torch.tensor(train_x).float() | ||
train_x = torch.tensor(test_dataset[i]).float() | ||
train_y = torch.argmax(self.pred_model(train_x), dim=1) | ||
|
||
curr_gen_cf = [] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,8 +81,9 @@ def train(self, constraint_type, constraint_variables, constraint_direction, con | |
train_loss = 0.0 | ||
train_size = 0 | ||
|
||
train_dataset = torch.tensor(self.vae_train_feat).float() | ||
train_dataset = torch.utils.data.DataLoader(train_dataset, batch_size=self.batch_size, shuffle=True) | ||
train_dataset = torch.utils.data.DataLoader( | ||
torch.tensor(self.vae_train_feat).float(), # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set |
||
batch_size=self.batch_size, shuffle=True) | ||
for train in enumerate(train_dataset): | ||
self.cf_vae_optimizer.zero_grad() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, multiple types of values are stored.