Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def output(module_name, immediate_output_dict):
return F.softmax(immediate_output_dict[module_name][0], dim=1)
task1 = EmmentalTask(
name="task_1",
module_pool=nn.ModuleDict(
{"m1": nn.Linear(10, 10, bias=False), "m2": nn.Linear(10, 2, bias=False)}
),
task_flow=[
{"name": "m1", "module": "m1", "inputs": [("_input_", "data")]},
{"name": "m2", "module": "m2", "inputs": [("m1", 0)]},
],
loss_func=partial(ce_loss, "m2"),
output_func=partial(output, "m2"),
scorer=Scorer(metrics=["accuracy"]),
)
new_task1 = EmmentalTask(
name="task_1",
module_pool=nn.ModuleDict(
{"m1": nn.Linear(10, 5, bias=False), "m2": nn.Linear(5, 2, bias=False)}
),
task_flow=[
{"name": "m1", "module": "m1", "inputs": [("_input_", "data")]},
{"name": "m2", "module": "m2", "inputs": [("m1", 0)]},
],
loss_func=partial(ce_loss, "m2"),
output_func=partial(output, "m2"),
scorer=Scorer(metrics=["accuracy"]),
)
def test_scorer(caplog):
"""Unit test of scorer."""
caplog.set_level(logging.INFO)
golds = np.array([1, 0, 1, 0, 1, 0])
preds = np.array([1, 1, 1, 1, 1, 0])
probs = np.array(
[[0.2, 0.8], [0.4, 0.6], [0.1, 0.9], [0.3, 0.7], [0.3, 0.7], [0.8, 0.2]]
)
def sum(gold, probs, preds, uids):
return np.sum(preds)
scorer = Scorer(
metrics=["accuracy", "accuracy@2", "f1"], customize_metric_funcs={"sum": sum}
)
assert scorer.score(golds, probs, preds) == {
"accuracy": 0.6666666666666666,
"accuracy@2": 1.0,
"f1": 0.7499999999999999,
"sum": 5,
}
),
task_flow=[
{
"name": "input",
"module": "input_module",
"inputs": [("_input_", "data")],
},
{
"name": f"{task_name}_pred_head",
"module": f"{task_name}_pred_head",
"inputs": [("input", 0)],
},
],
loss_func=partial(ce_loss, task_name),
output_func=partial(output, task_name),
scorer=Scorer(metrics=task_metrics[task_name]),
)
for task_name in ["task1", "task2"]
]
# Build model
mtl_model = EmmentalModel(name="all", tasks=tasks)
# Create learner
emmental_learner = EmmentalLearner()
# Learning
emmental_learner.learn(
mtl_model,
[train_dataloader1, train_dataloader2, dev_dataloader1, dev_dataloader2],
)
del base_task_module_pool[base_task_predictor_action["module"]] # type: ignore
base_task_task_flow = task.task_flow[:-1]
tasks = []
slice_module_pool = nn.ModuleDict()
for module_name, module in task.module_pool.items():
slice_module_pool[module_name] = module
slice_actions = [action for action in base_task_task_flow]
if slice_ind_head_module is None:
slice_ind_head_module = nn.Linear(task_feature_size, 2) # type: ignore
assert isinstance(slice_ind_head_module, nn.Module)
if slice_scorer is None or not isinstance(slice_scorer, Scorer):
slice_scorer = Scorer(metrics=["f1"])
# Create slice indicator tasks.
# (Note: indicator only has two classes, e.g, in the slice or out)
for slice_name in slice_func_dict.keys():
# Create task name
ind_task_name = f"{task.name}_slice:ind_{slice_name}"
# Create ind module
ind_head_module_name = f"{ind_task_name}_head"
ind_head_module = copy.deepcopy(slice_ind_head_module)
ind_head_dropout_module_name = f"{task.name}_slice:dropout_{slice_name}"
ind_head_dropout_module = nn.Dropout(p=dropout)
# Create module_pool
"name": f"{task_name}_pred_head",
"module": f"{task_name}_pred_head",
"inputs": None,
},
]
else:
raise ValueError(f"Unrecognized model {model}.")
tasks.append(
EmmentalTask(
name=task_name,
module_pool=module_pool,
task_flow=task_flow,
loss_func=partial(loss, f"{task_name}_pred_head"),
output_func=partial(output, f"{task_name}_pred_head"),
scorer=Scorer(metrics=["accuracy", "precision", "recall", "f1"]),
)
)
return tasks
base_task_task_flow = task.task_flow[:-1]
tasks = []
slice_module_pool = nn.ModuleDict()
for module_name, module in task.module_pool.items():
slice_module_pool[module_name] = module
slice_actions = [action for action in base_task_task_flow]
if slice_ind_head_module is None:
slice_ind_head_module = nn.Linear(task_feature_size, 2) # type: ignore
assert isinstance(slice_ind_head_module, nn.Module)
if slice_scorer is None or not isinstance(slice_scorer, Scorer):
slice_scorer = Scorer(metrics=["f1"])
# Create slice indicator tasks.
# (Note: indicator only has two classes, e.g, in the slice or out)
for slice_name in slice_func_dict.keys():
# Create task name
ind_task_name = f"{task.name}_slice:ind_{slice_name}"
# Create ind module
ind_head_module_name = f"{ind_task_name}_head"
ind_head_module = copy.deepcopy(slice_ind_head_module)
ind_head_dropout_module_name = f"{task.name}_slice:dropout_{slice_name}"
ind_head_dropout_module = nn.Dropout(p=dropout)
# Create module_pool
ind_module_pool = nn.ModuleDict(
def __init__(
self,
name: Optional[str] = None,
tasks: Optional[Union[EmmentalTask, List[EmmentalTask]]] = None,
) -> None:
"""Initialize EmmentalModel."""
super().__init__()
self.name = name if name is not None else type(self).__name__
# Initiate the model attributes
self.module_pool: ModuleDict = ModuleDict()
self.task_names: Set[str] = set()
self.task_flows: Dict[str, Any] = dict() # TODO: make it concrete
self.loss_funcs: Dict[str, Callable] = dict()
self.output_funcs: Dict[str, Callable] = dict()
self.scorers: Dict[str, Scorer] = dict()
self.weights: Dict[str, float] = dict()
# Build network with given tasks
if tasks is not None:
self.add_tasks(tasks)
if Meta.config["meta_config"]["verbose"]:
logger.info(
f"Created emmental model {self.name} that contains "
f"task {self.task_names}."
)
# Move model to specified device
self._move_to_device()
def __init__(
self,
name: Optional[str] = None,
tasks: Optional[Union[EmmentalTask, List[EmmentalTask]]] = None,
) -> None:
super().__init__()
self.name = name if name is not None else type(self).__name__
# Initiate the model attributes
self.module_pool: ModuleDict = ModuleDict()
self.task_names: Set[str] = set()
self.task_flows: Dict[str, Any] = dict() # TODO: make it concrete
self.loss_funcs: Dict[str, Callable] = dict()
self.output_funcs: Dict[str, Callable] = dict()
self.scorers: Dict[str, Scorer] = dict()
self.weights: Dict[str, float] = dict()
# Build network with given tasks
if tasks is not None:
self._build_network(tasks)
if Meta.config["meta_config"]["verbose"]:
logger.info(
f"Created emmental model {self.name} that contains "
f"task {self.task_names}."
)
# Move model to specified device
self._move_to_device()