Skip to content

Commit

Permalink
Moved create_vector_net_from_module to a class method of VectorNet
Browse files Browse the repository at this point in the history
  • Loading branch information
phurwicz committed Dec 22, 2020
1 parent 27f88a6 commit 8fcba7a
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions hover/core/neural.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import torch
import torch.nn.functional as F
from datetime import datetime
from deprecated import deprecated
from hover.utils.metrics import classification_accuracy
from wasabi import msg as logger
from sklearn.metrics import confusion_matrix
from snorkel.classification import cross_entropy_with_probs
import numpy as np


@deprecated(
version="0.3.3",
reason="will be removed in a future version; please use VectorNet.from_module() instead.",
)
def create_vector_net_from_module(specific_class, model_module_name, labels):
"""
Create a TextVectorNet model, or of its child class.
- param specific_class(class): TextVectorNet or its child class.
- param model_module_name(str): path to a local Python module in the working directory whose __init__.py file contains a get_vectorizer() callable, get_architecture() callable, and a get_state_dict_path() callable.
- param labels(list of str): the classification labels, e.g. ["POSITIVE", "NEGATIVE"].
Deprecated into a trivial invocation of VectorNet's class method.
"""
from importlib import import_module

model_module = import_module(model_module_name)

# Load the model by retrieving the inp-to-vec function, architecture, and state dict
model = specific_class(
model_module.get_vectorizer(),
model_module.get_architecture(),
model_module.get_state_dict_path(),
labels,
)

return model
return specific_class.from_module(model_module_name, labels)


class VectorNet(object):
Expand Down Expand Up @@ -82,6 +71,28 @@ def __init__(self, vectorizer, architecture, state_dict_path, labels):
self.nn_optimizer = torch.optim.Adam(self.nn.parameters())
self._dynamic_params = {"optimizer": {"lr": 0.01, "betas": (0.9, 0.999)}}

@classmethod
def from_module(cls, model_module_name, labels):
"""
Create a VectorNet model, or of its child class.
- param model_module_name(str): path to a local Python module in the working directory whose __init__.py file contains a get_vectorizer() callable, get_architecture() callable, and a get_state_dict_path() callable.
- param labels(list of str): the classification labels, e.g. ["POSITIVE", "NEGATIVE"].
"""
from importlib import import_module

model_module = import_module(model_module_name)

# Load the model by retrieving the inp-to-vec function, architecture, and state dict
model = cls(
model_module.get_vectorizer(),
model_module.get_architecture(),
model_module.get_state_dict_path(),
labels,
)

return model

def save(self, save_path=None):
"""
Save the current state dict with authorization to overwrite.
Expand Down

0 comments on commit 8fcba7a

Please sign in to comment.