-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_utils.py
76 lines (61 loc) · 2.48 KB
/
model_utils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import data_loader as dl
import matplotlib.pyplot as plt
import numpy as np
import keras.backend as K
def ignore_class_accuracy(to_ignore=0):
def ignore_accuracy(y_true, y_pred):
y_true_class = K.argmax(y_true, axis=-1)
y_pred_class = K.argmax(y_pred, axis=-1)
ignore_mask = K.cast(K.not_equal(y_pred_class, to_ignore), 'int32')
matches = K.cast(K.equal(y_true_class, y_pred_class), 'int32') * ignore_mask
accuracy = K.sum(matches) / K.maximum(K.sum(ignore_mask), 1)
return accuracy
return ignore_accuracy
################################################################################
def predict(model,sentence,MAX_LENGTH,id2tag,emb):
sentence = dl.preprocess_for_model_testing(sentence,MAX_LENGTH,emb)
pred = model.predict(sentence)
pred = pred[0]
output = []
for tag in pred:
id = np.argmax(tag)
if(id==9): break
output.append(id2tag[id])
return output
################################################################################
def plot_model_history(model,history):
# summarize history for accuracy
plt.figure(figsize=(10,8))
plt.plot(history.history['ignore_accuracy'])
plt.plot(history.history['val_ignore_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.figure(figsize=(10,8))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
################################################################################
# ~~~~~~~~~~~~~~~~~~ Following function is for model3 ~~~~~~~~~~~~~~~~~~~~~~ #
def predict_v2(model,sentence,MAX_LENGTH,emb,id2tag):
padded_sentence = dl.preprocess_for_model_testing(sentence,MAX_LENGTH,emb,number_format=0)
f_input = dl.get_features(padded_sentence[0])
f_input = f_input.reshape(-1,f_input.shape[0],f_input.shape[1])
sentence = dl.preprocess_for_model_testing(sentence,MAX_LENGTH,emb)
pred = model.predict([sentence,f_input])
pred = pred[0]
output = []
for tag in pred:
id = np.argmax(tag)
if(id==9): break
output.append(id2tag[id])
return output
################################################################################
# Code provided by Shreyansh Chordia