Update scripts/trainer.py
Browse files- scripts/trainer.py +1 -40
scripts/trainer.py
CHANGED
|
@@ -125,46 +125,7 @@ def slide_level_val_step(
|
|
| 125 |
|
| 126 |
return _epoch_loss, _epoch_logits, _epoch_labels
|
| 127 |
|
| 128 |
-
|
| 129 |
-
class BaseTrainer:
|
| 130 |
-
"""Base trainer class with ``train``, ``evaluate``, ``save`` and ``load``
|
| 131 |
-
methods. ``train`` and ``evaluate`` methods should be overriden."""
|
| 132 |
-
|
| 133 |
-
def __init__(self):
|
| 134 |
-
pass
|
| 135 |
-
|
| 136 |
-
def train(
|
| 137 |
-
self,
|
| 138 |
-
train_set: Subset,
|
| 139 |
-
val_set: Subset,
|
| 140 |
-
) -> Tuple[Dict[str, float], Dict[str, float]]:
|
| 141 |
-
"""Training function."""
|
| 142 |
-
raise NotImplementedError
|
| 143 |
-
|
| 144 |
-
def evaluate(
|
| 145 |
-
self,
|
| 146 |
-
test_set: Subset,
|
| 147 |
-
) -> Dict[str, float]:
|
| 148 |
-
"""Inference function."""
|
| 149 |
-
raise NotImplementedError
|
| 150 |
-
|
| 151 |
-
def save(self, filepath: Union[Path, str]):
|
| 152 |
-
"""Model serialization."""
|
| 153 |
-
filepath = Path(filepath).with_suffix(".pkl")
|
| 154 |
-
with filepath.open("wb") as p:
|
| 155 |
-
pickle.dump(self, p)
|
| 156 |
-
|
| 157 |
-
@classmethod
|
| 158 |
-
def load(cls, filepath: Union[Path, str]):
|
| 159 |
-
"""Model loading."""
|
| 160 |
-
del cls
|
| 161 |
-
filepath = Path(filepath).with_suffix(".pkl")
|
| 162 |
-
with filepath.open("rb") as p:
|
| 163 |
-
obj = pickle.load(p)
|
| 164 |
-
return obj
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
class TorchTrainer(BaseTrainer):
|
| 168 |
"""Trainer class for training and evaluating PyTorch models.
|
| 169 |
|
| 170 |
Parameters
|
|
|
|
| 125 |
|
| 126 |
return _epoch_loss, _epoch_logits, _epoch_labels
|
| 127 |
|
| 128 |
+
class TorchTrainer:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
"""Trainer class for training and evaluating PyTorch models.
|
| 130 |
|
| 131 |
Parameters
|