forked from Bassem-Makni/NMT4RDFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_utils.py
147 lines (126 loc) · 5.21 KB
/
nn_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import csv
import os
import time
from collections import Iterable
from collections import OrderedDict
import keras.backend as K
import numpy as np
import six
from keras.callbacks import Callback
from sklearn.model_selection import train_test_split
def train_validate_test_split(df, train_percent=0.6, validate_percent=0.2, stratify=None, seed=1):
val_test_percent = 1 - train_percent
test_percent = (1 - (train_percent + validate_percent))
test_percent = test_percent / (test_percent + validate_percent)
if stratify:
df_train, df_val_test = train_test_split(df, test_size=val_test_percent, random_state=seed,
stratify=df[stratify])
df_val, df_test = train_test_split(df_val_test, test_size=test_percent, random_state=seed,
stratify=df_val_test[stratify])
else:
df_train, df_val_test = train_test_split(df, test_size=val_test_percent, random_state=seed)
df_val, df_test = train_test_split(df_val_test, test_size=test_percent, random_state=seed)
return df_train, df_val, df_test
def true_acc2(y_true, y_pred):
return K.mean(K.all(K.equal(y_true, K.round(y_pred)), axis=-1))
def true_acc(y_true, y_pred):
"""
All Accuracy
https://github.com/rasmusbergpalm/normalization/blob/master/train.py#L10
"""
return K.mean(
K.all(
K.equal(
K.argmax(y_true, axis=-1),
K.argmax(y_pred, axis=-1)
),
axis=1)
)
def to_categorical(y, num_classes=None):
"""Converts a class vector (integers) to binary class matrix.
E.g. for use with categorical_crossentropy.
# Arguments
y: class vector to be converted into a matrix
(integers from 0 to num_classes).
num_classes: total number of classes.
# Returns
A binary matrix representation of the input.
"""
y = np.array(y, dtype=np.int)
input_shape = y.shape
if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
input_shape = tuple(input_shape[:-1])
y = y.ravel()
if not num_classes:
num_classes = np.max(y) + 1
n = y.shape[0]
categorical = np.zeros((n, num_classes), dtype=np.bool)
categorical[np.arange(n), y] = 1
output_shape = input_shape + (num_classes,)
categorical = np.reshape(categorical, output_shape)
return categorical
class CSVLoggerTimed(Callback):
"""Callback that streams epoch results to a csv file.
Supports all values that can be represented as a string,
including 1D iterables such as np.ndarray.
# Example
```python
csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])
```
# Arguments
filename: filename of the csv file, e.g. 'run/log.csv'.
separator: string used to separate elements in the csv file.
append: True: append if file exists (useful for continuing
training). False: overwrite existing file,
"""
def __init__(self, filename, separator=',', append=False):
self.sep = separator
self.filename = filename
self.append = append
self.writer = None
self.keys = None
self.append_header = True
self.file_flags = 'b' if six.PY2 and os.name == 'nt' else ''
self.starttime = time.time()
super(CSVLoggerTimed, self).__init__()
def on_train_begin(self, logs=None):
self.starttime = time.time()
if self.append:
if os.path.exists(self.filename):
with open(self.filename, 'r' + self.file_flags) as f:
self.append_header = not bool(len(f.readline()))
self.csv_file = open(self.filename, 'a' + self.file_flags)
else:
self.csv_file = open(self.filename, 'w' + self.file_flags)
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
logs['time'] = time.time()
logs['time_delta'] = time.time() - self.starttime
def handle_value(k):
is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
if isinstance(k, six.string_types):
return k
elif isinstance(k, Iterable) and not is_zero_dim_ndarray:
return '"[%s]"' % (', '.join(map(str, k)))
else:
return k
if self.keys is None:
self.keys = sorted(logs.keys())
if self.model.stop_training:
# We set NA so that csv parsers do not fail for this last epoch.
logs = dict([(k, logs[k]) if k in logs else (k, 'NA') for k in self.keys])
if not self.writer:
class CustomDialect(csv.excel):
delimiter = self.sep
self.writer = csv.DictWriter(self.csv_file,
fieldnames=['epoch'] + self.keys, dialect=CustomDialect)
if self.append_header:
self.writer.writeheader()
row_dict = OrderedDict({'epoch': epoch})
row_dict.update((key, handle_value(logs[key])) for key in self.keys)
self.writer.writerow(row_dict)
self.csv_file.flush()
def on_train_end(self, logs=None):
self.csv_file.close()
self.writer = None