utils.py
4.34 KB
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
from __future__ import print_function
import codecs
import sys
import javaobj
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Dropout, Activation, BatchNormalization, Lambda
from keras import backend as K
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def initialize_neural_model(architecture, number_of_features, path_to_model):
model = None
if architecture == 'simple':
model = initialize_simple_model(number_of_features, path_to_model)
elif architecture == 'siamese':
model = initialize_siamese_model(number_of_features, path_to_model)
return model
def initialize_simple_model(number_of_features, path_to_model):
inputs = Input(shape=(number_of_features,))
output_from_1st_layer = Dense(500)(inputs)
output_from_1st_layer = BatchNormalization()(output_from_1st_layer)
output_from_1st_layer = Activation('relu')(output_from_1st_layer)
output_from_1st_layer = Dropout(0.2)(output_from_1st_layer)
output_from_2nd_layer = Dense(200)(output_from_1st_layer)
output_from_2nd_layer = BatchNormalization()(output_from_2nd_layer)
output_from_2nd_layer = Activation('relu')(output_from_2nd_layer)
output_from_2nd_layer = Dropout(0.2)(output_from_2nd_layer)
output_from_3rd_layer = Dense(100)(output_from_2nd_layer)
output_from_3rd_layer = BatchNormalization()(output_from_3rd_layer)
output_from_3rd_layer = Activation('relu')(output_from_3rd_layer)
output_from_3rd_layer = Dropout(0.2)(output_from_3rd_layer)
output = Dense(1, activation='sigmoid')(output_from_3rd_layer)
model = Model(inputs, output)
model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])
model.load_weights(path_to_model)
return model
def initialize_siamese_model(number_of_features, path_to_model):
input_dim = number_of_features
base_network = create_base_network(input_dim)
input_a = Input(shape=(input_dim,))
input_b = Input(shape=(input_dim,))
processed_a = base_network(input_a)
processed_b = base_network(input_b)
distance = Lambda(euclidean_distance, output_shape=eucl_dist_output_shape)([processed_a, processed_b])
model = Model([input_a, input_b], distance)
model.compile(loss=contrastive_loss, optimizer='Adam')
model.load_weights(path_to_model)
return model
def create_base_network(input_dim):
seq = Sequential()
seq.add(Dense(1000, input_shape=(input_dim,), activation='relu'))
seq.add(Dropout(0.2))
seq.add(BatchNormalization())
seq.add(Dense(500, activation='relu'))
seq.add(Dropout(0.2))
seq.add(BatchNormalization())
seq.add(Dense(300, activation='relu'))
return seq
def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
def eucl_dist_output_shape(shapes):
shape1, shape2 = shapes
return shape1[0], 1
def contrastive_loss(y_true, y_pred):
margin = 1
return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
def load_freq_list(freq_path):
freq_list = {}
with codecs.open(freq_path, 'r', 'utf-8') as freq_file:
lines = freq_file.readlines()
for line in lines:
line_parts = line.split()
freq = int(line_parts[0])
base = line_parts[1]
if base not in freq_list:
freq_list[base] = freq
return freq_list
def load_one2many_map(map_path):
this_map = {}
marshaller = javaobj.JavaObjectUnmarshaller(open(map_path, 'rb'))
pobj = marshaller.readObject()
jmap_annotations = pobj.__dict__['annotations']
jmap_annotations_count = len(jmap_annotations)
for i in range(jmap_annotations_count):
if i % 2 == 1:
mapped_elements = set(jmap_annotations[i+1].__dict__['annotations'])
this_map[jmap_annotations[i]] = mapped_elements
return this_map
def load_one2one_map(map_path):
this_map = {}
marshaller = javaobj.JavaObjectUnmarshaller(open(map_path, 'rb'))
pobj = marshaller.readObject()
jmap_annotations = pobj.__dict__['annotations']
jmap_annotations_count = len(jmap_annotations)
for i in range(jmap_annotations_count):
if i % 2 == 1:
element = jmap_annotations[i+1]
this_map[jmap_annotations[i]] = element
return this_map