-
Notifications
You must be signed in to change notification settings - Fork 4
/
evaluate_model.py
61 lines (53 loc) · 1.89 KB
/
evaluate_model.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
53
54
55
56
57
58
59
60
61
import io
import hydra
import torch
from hydra.core.config_store import ConfigStore
from utils.data_utils import *
from utils.modeling import *
from utils.train_utils import *
def define_and_load_model(conf):
# Define the model
model = Model_Recursive_LSTM_v2(
input_size=conf.model.input_size,
comp_embed_layer_sizes=list(conf.model.comp_embed_layer_sizes),
drops=list(conf.model.drops),
loops_tensor_size=8,
device=conf.testing.gpu,
)
# Load the trained model weights
model.load_state_dict(
torch.load(
conf.testing.testing_model_weights_path,
map_location=conf.testing.gpu,
)
)
model = model.to(conf.testing.gpu)
# Set the model to evaluation mode
model.eval()
return model
def evaluate(conf, model):
print("Loading the dataset...")
val_ds, val_bl, val_indices, _ = load_pickled_repr(
os.path.join(conf.experiment.base_path ,'pickled/pickled_')+Path(conf.data_generation.valid_dataset_file).parts[-1][:-4],
max_batch_size = 1024,
store_device=conf.testing.gpu,
train_device=conf.testing.gpu
)
print("Evaluation...")
val_df = get_results_df(val_ds, val_bl, val_indices, model, train_device = conf.testing.gpu)
val_scores = get_scores(val_df)
return dict(
zip(
["nDCG", "nDCG@5", "nDCG@1", "Spearman_ranking_correlation", "MAPE"],
[item for item in val_scores.describe().iloc[1, 1:6].to_numpy()],
)
)
@hydra.main(config_path="conf", config_name="config")
def main(conf):
print("Defining and loading the model using parameters from the config file")
model = define_and_load_model(conf)
print(f"Validating on the dataset: {conf.data_generation.valid_dataset_file}")
scores = evaluate(conf, model)
print(f"Evaluation scores are:\n{scores}")
if __name__ == "__main__":
main()