forked from danlou/MedLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher_softmax.py
executable file
·35 lines (25 loc) · 1011 Bytes
/
matcher_softmax.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
import joblib
from keras.models import load_model
import numpy as np
np.random.seed(42)
import tensorflow as tf
tf.set_random_seed(42)
class SoftMax_CLF():
def __init__(self, threshold=0.5):
self.label_mapping = None
self.model = None
self.threshold = threshold
def load(self, model_path, mapping_path):
self.model = load_model(model_path)
self.label_mapping = joblib.load(mapping_path)
self.label_mapping = {i:l for l, i in self.label_mapping.items()}
def predict(self, ctx_vec):
preds = self.model.predict_proba(ctx_vec.reshape(1, -1))[0]
matches = {}
for pred_idx, pred in enumerate(preds):
if pred > self.threshold: # skips NaNs too
pred_label = self.label_mapping[pred_idx]
pred_label = pred_label.lstrip('UMLS:')
matches[pred_label] = pred
matches = sorted(matches.items(), key=lambda x: x[1], reverse=True)
return matches