Download Deception/Code/Multitask_Learning/multi_task.py from ankurani/deception: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/datasets/ankurani/deception/resolve/refs%2Fpr%2F1/Deception/Code/Multitask_Learning/multi_task.py
- Command line
-
hf download hf://datasets/ankurani/deception@refs/pr/1/Deception/Code/Multitask_Learning/multi_task.py
-
curl -L -o multi_task.py https://huggingface.co/datasets/ankurani/deception/resolve/refs%2Fpr%2F1/Deception/Code/Multitask_Learning/multi_task.py
1.35 kB
| import torch.nn as nn | |
| class MultiTaskModel(nn.Module): | |
| def __init__(self, base_net, task_heads, device): | |
| super().__init__() | |
| self.base_net = base_net | |
| self.task_heads = nn.ModuleList(task_heads) | |
| self.device = device | |
| def forward(self, x): | |
| base_output = self.base_net(x) | |
| # Forward pass through task-specific heads | |
| task_outputs = [head(base_output) for head in self.task_heads] | |
| return task_outputs | |
| def predict(self, x): | |
| base_output = self.base_net(x) | |
| # Forward pass through task-specific heads | |
| task_outputs = [head.predict(base_output) for head in self.task_heads] | |
| return task_outputs | |
| def accuracy(self, predictions, targets): | |
| accuracies = [head.accuracy(prediction, target) for head, prediction, target in zip( | |
| self.task_heads, predictions, targets)] | |
| return accuracies | |
| def recall(self, predictions, targets): | |
| recalls = [head.recall(prediction, target) for head, prediction, target in zip( | |
| self.task_heads, predictions, targets)] | |
| return recalls | |
| def precision(self, predictions, targets): | |
| precisions = [head.precision(prediction, target) for head, prediction, target in zip( | |
| self.task_heads, predictions, targets)] | |
| return precisions |