File size: 2,591 Bytes
a2ffd07 | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | from abc import ABC, abstractmethod
from typing import List, Dict, Tuple, Any
from torch import Tensor
from dataclasses import dataclass
@dataclass
class GraphName:
feature_graph: str = 'feature_graph'
computational_graph: str = 'computational_graph'
class Node(ABC):
@property
@abstractmethod
def name(self) -> str:
pass
@abstractmethod
def __eq__(self, other) -> bool:
pass
@abstractmethod
def __repr__(self) -> str:
pass
@abstractmethod
def __hash__(self) -> int:
pass
class Index(ABC):
@property
@abstractmethod
def as_index(self) -> Tuple[int|slice, ...]:
pass
@abstractmethod
def __eq__(self, other) -> bool:
pass
@abstractmethod
def __repr__(self) -> str:
pass
@abstractmethod
def __hash__(self) -> int:
pass
class Graph(ABC):
@abstractmethod
def add_node(self, *args, **kwargs) -> None:
pass
@abstractmethod
def add_edge(self, *args, **kwargs) -> None:
pass
@abstractmethod
def delete_node(self, *args, **kwargs) -> None:
pass
@abstractmethod
def delete_edge(self, *args, **kwargs) -> None:
pass
@abstractmethod
def update_node(self, *args, **kwargs) -> None:
pass
@abstractmethod
def update_edge(self, *args, **kwargs) -> None:
pass
@abstractmethod
def find_deleted_nodes(self, *args, **kwargs) -> List[Tuple[Node, Index]]:
pass
@abstractmethod
def find_deleted_edges(self, *args, **kwargs) -> List[Tuple[Node, Index, Node, Index]]:
pass
@abstractmethod
def iterate_nodes(self) -> List[Tuple[Node, Index]]:
pass
@abstractmethod
def iterate_edges(self) -> List[Tuple[Node, Index, Node, Index]]:
pass
@abstractmethod
def forward(self, *args, **kwargs) -> Tuple[Tensor, Dict[str, Any]]:
pass
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
@abstractmethod
def forward_backward_gradient(self, *args, **kwargs) -> Tuple[
Dict[Tuple[Node, Index], Any], # node effects
Dict[Tuple[Node, Index, Node, Index], Any], # edge effects
]:
pass
@abstractmethod
def model_setup(self) -> None:
pass
@abstractmethod
def run_model(self, *args, **kwargs) -> Tuple[Any, Any]:
pass
@abstractmethod
def graph_type(self) -> str:
pass
|