-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
executable file
·89 lines (78 loc) · 2.88 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
import pandas as pd
import itertools
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.backend import set_session
import numpy as np
import random as rn
SEED = 20190222
np.random.seed(SEED)
rn.seed(SEED)
tf.set_random_seed(SEED)
def set_allow_growth(device="1"):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
config.gpu_options.visible_device_list=device
sess = tf.Session(config=config)
set_session(sess) # set this TensorFlow session as the default session for Keras
def load_data(dataset):
texts = []
labels = []
partition_to_n_row = {}
for partition in ['train', 'valid', 'test']:
with open("data/" + dataset + "/" + partition + ".seq.in") as fp:
lines = fp.read().splitlines()
texts.extend(lines)
partition_to_n_row[partition] = len(lines)
with open("data/" + dataset + "/" + partition + ".label") as fp:
labels.extend(fp.read().splitlines())
df = pd.DataFrame([texts, labels]).T
df.columns = ['text', 'label']
return df, partition_to_n_row
def get_score(cm):
fs = []
n_class = cm.shape[0]
for idx in range(n_class):
TP = cm[idx][idx]
r = TP / cm[idx].sum() if cm[idx].sum()!=0 else 0
p = TP / cm[:, idx].sum() if cm[:, idx].sum()!=0 else 0
f = 2*r*p/(r+p) if (r+p)!=0 else 0
fs.append(f*100)
f = np.mean(fs).round(2)
f_seen = np.mean(fs[:-1]).round(2)
f_unseen = round(fs[-1], 2)
print("Overall(macro): ", f)
print("Seen(macro): ", f_seen)
print("=====> Uneen(Experiment) <=====: ", f_unseen)
return f, f_seen, f_unseen
def plot_confusion_matrix(cm, classes, normalize=False,
title='Confusion matrix', figsize=(12,10),
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
# Compute confusion matrix
np.set_printoptions(precision=2)
plt.figure(figsize=figsize)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
plt.savefig('img/mat.png')