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

multilabel support for data_silo.calculate_class_weights #389

Merged
merged 3 commits into from
May 31, 2020
Merged
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
6 changes: 5 additions & 1 deletion farm/data_handler/data_silo.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,12 @@ def calculate_class_weights(self, task_name, source="train"):
else:
raise Exception("source argument expects one of [\"train\", \"all\"]")
for dataset in datasets:
if dataset is not None:
if "multilabel" in self.processor.tasks[task_name]["task_type"]:
for x in dataset:
observed_labels += [label_list[label_id] for label_id in (x[tensor_idx] == 1).nonzero()]
else:
observed_labels += [label_list[x[tensor_idx].item()] for x in dataset]

#TODO scale e.g. via logarithm to avoid crazy spikes for rare classes
class_weights = list(compute_class_weight("balanced", np.asarray(label_list), observed_labels))
return class_weights
Expand Down