Skip to content

Commit

Permalink
Save models in seprate directories
Browse files Browse the repository at this point in the history
Instead of grouping PE and AP models in the predicate_extraction and
argument_prediction directories, models now now have a individual
directory, which will contain those two subdirectories inside.
  • Loading branch information
Pligabue committed Dec 7, 2023
1 parent 10be824 commit 60563af
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
18 changes: 13 additions & 5 deletions triple_extractor_ptbr_pligabue/argument_prediction/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import cast, Optional

from ..constants import DEFAULT_SENTENCE_SIZE, ARGUMENT_PREDICTION_MODEL_DIR, DEFAULT_MODEL_NAME
from ..constants import DEFAULT_SENTENCE_SIZE, MODEL_DIR, ARGUMENT_PREDICTION_MODEL_DIR_NAME, DEFAULT_MODEL_NAME
from ..bert import bert
from ..predicate_extraction.types import ArgPredInputs

Expand All @@ -21,12 +21,20 @@ def __init__(self, *layer_units: int, name: Optional[str] = None,
super().__init__(sentence_size)
self._config_model(*layer_units)

@staticmethod
def full_model_path(name: str):
return MODEL_DIR / name

@staticmethod
def ap_model_path(name: str):
return ArgumentPredictor.full_model_path(name) / ARGUMENT_PREDICTION_MODEL_DIR_NAME

@classmethod
def load(cls, name: str = DEFAULT_MODEL_NAME):
return cls(name=name)

def _load_model(self, name: str):
path = ARGUMENT_PREDICTION_MODEL_DIR / name
path = self.ap_model_path(name)
if path.is_dir():
model = tf.keras.models.load_model(path)
self.model = cast(tf.keras.Model, model)
Expand Down Expand Up @@ -70,9 +78,9 @@ def _config_model(self, *layer_units: int):
self.model.layers[2].trainable = False

def save(self, name: str):
path = ARGUMENT_PREDICTION_MODEL_DIR / name
if not ARGUMENT_PREDICTION_MODEL_DIR.is_dir():
ARGUMENT_PREDICTION_MODEL_DIR.mkdir()
self.full_model_path(name).mkdir(exist_ok=True)
path = self.ap_model_path(name)
path.mkdir(exist_ok=True)
self.model.save(path)

def compile(self, optimizer=None, loss=None, metrics=None):
Expand Down
4 changes: 2 additions & 2 deletions triple_extractor_ptbr_pligabue/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
OBJECT_PATTERN = r"<OBJECT>(.*)</OBJECT>"
SPECIAL_TOKEN_IDS = [101, 102, 0]
MODEL_DIR = Path(__file__).parent / "models"
PREDICATE_EXTRACTION_MODEL_DIR = MODEL_DIR / "predicate_extraction"
ARGUMENT_PREDICTION_MODEL_DIR = MODEL_DIR / "argument_prediction"
PREDICATE_EXTRACTION_MODEL_DIR_NAME = "predicate_extraction"
ARGUMENT_PREDICTION_MODEL_DIR_NAME = "argument_prediction"
DEFAULT_MODEL_NAME = "default"
DEFAULT_PRED_THRESHOLD = 0.2
DEFAULT_ARG_THREHSOLD = 0.15
21 changes: 14 additions & 7 deletions triple_extractor_ptbr_pligabue/predicate_extraction/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import cast, Optional

from ..constants import DEFAULT_SENTENCE_SIZE, PREDICATE_EXTRACTION_MODEL_DIR, DEFAULT_MODEL_NAME
from ..constants import DEFAULT_SENTENCE_SIZE, MODEL_DIR, PREDICATE_EXTRACTION_MODEL_DIR_NAME, DEFAULT_MODEL_NAME
from ..bert import bert
from .constants import ACCEPTANCE_THRESHOLD, O_THRESHOLD
from .data_formatter import DataFormatter
Expand All @@ -20,16 +20,23 @@ def __init__(self, *dense_layer_units: int, name: Optional[str] = None,
super().__init__(sentence_size)
self._config_model(*dense_layer_units)

@staticmethod
def full_model_path(name: str):
return MODEL_DIR / name

@staticmethod
def pe_model_path(name: str):
return PredicateExtractor.full_model_path(name) / PREDICATE_EXTRACTION_MODEL_DIR_NAME

@classmethod
def load(cls, name: str = DEFAULT_MODEL_NAME):
path = PREDICATE_EXTRACTION_MODEL_DIR / name
if path.is_dir():
if cls.pe_model_path(name).is_dir():
return cls(name=name)
else:
raise Exception(f"Model {str} does not exist.")

def _load_model(self, name: str):
path = PREDICATE_EXTRACTION_MODEL_DIR / name
path = self.pe_model_path(name)
if path.is_dir():
model = tf.keras.models.load_model(path)
self.model = cast(tf.keras.Model, model)
Expand All @@ -52,9 +59,9 @@ def _config_model(self, *dense_layer_units: int):
self.model.layers[1].trainable = False

def save(self, name: str = DEFAULT_MODEL_NAME):
path = PREDICATE_EXTRACTION_MODEL_DIR / name
if not PREDICATE_EXTRACTION_MODEL_DIR.is_dir():
PREDICATE_EXTRACTION_MODEL_DIR.mkdir()
self.full_model_path(name).mkdir(exist_ok=True)
path = self.pe_model_path(name)
path.mkdir(exist_ok=True)
self.model.save(path)

def compile(self, optimizer=None, loss=None, metrics=None):
Expand Down

0 comments on commit 60563af

Please sign in to comment.