-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation.py
52 lines (35 loc) · 1.26 KB
/
operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from abc import ABC
from abc import abstractmethod
from functools import reduce
from typing import List
import torch
class Op(ABC):
@abstractmethod
def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
raise NotImplementedError()
class NumericOp(Op):
idx: int
def update_idx(self, idx: int) -> None:
self.idx = idx
def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
return inputs.select(1, self.idx)
class LogicOp(Op):
def __init__(self, *ops: Op) -> None:
self.ops = ops
class Col(NumericOp):
def __init__(self, subtask_name: str, label: str):
self.subtask_name = subtask_name
self.label = label
class Sum(NumericOp):
def __init__(self, subtask_name: str, labels: List[str]):
self.subtask_name = subtask_name
self.labels = labels
class And(LogicOp):
def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
subs = [op(inputs) for op in self.ops]
return reduce(torch.multiply, subs)
class Or(LogicOp):
def __call__(self, inputs: torch.Tensor) -> torch.Tensor:
subs = [op(inputs) for op in self.ops]
return 1 - reduce(torch.multiply, [1-res for res in subs],
torch.tensor(1.0).to(inputs.device))