-
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 all commits
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, 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.""" | ||
|
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.