-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeddings.py
223 lines (180 loc) · 7.29 KB
/
embeddings.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from keras.layers import Dense, Input
from keras.preprocessing.sequence import pad_sequences
from keras.layers.embeddings import Embedding
import numpy as np
from keras.layers import BatchNormalization
from keras.utils.vis_utils import plot_model
from keras.layers import Flatten
from keras.models import Model
from keras.layers.merge import concatenate
class One_Hot_Encoder(object):
"""A one-hot encoder-decoder for categorical features"""
def __init__(self, df, categorical_columns):
"""Create the dictionaries corresponding to this dataframe and the
categorical columns"""
self.num_to_name = {}
self.name_to_num = {}
for c in categorical_columns:
self.num_to_name[c] = dict(enumerate(df[c].unique()))
self.name_to_num[c] = {v: k for k, v in self.num_to_name[c].items()}
def encode(self, df, categorical_columns, max_length):
""" create one-hot representations for the categorical features
pad the representations to a desired length"""
padded_categorical_data = []
for c in categorical_columns:
ohe = [[self.name_to_num[c][i]] for i in df[c]]
padded_categorical_data.append(
pad_sequences(ohe, maxlen=max_length, padding="post")
)
# merge categorical and ordinal feature inputs
return padded_categorical_data
def retrieve_names(self, categorical_column, num_list):
"""Converts a list of numbers to the corresponding
categorical variable names. Returns the list of names"""
return [self.num_to_name[categorical_column][i] for i in num_list]
class embeddings_models(object):
def __init__(
self,
vocabulary_sizes,
max_length,
categorical_columns,
num_ordinal_features,
# _visualize,
):
"""Initialize functor:
All models trained will contain the same properties listed below"""
self.vocabulary_sizes = vocabulary_sizes
self.max_length = max_length
self.categorical_columns = categorical_columns
self.num_ordinal_features = num_ordinal_features
# self.visualize = _visualize
self.num_models = 1
def __call__(
self,
_input_data_train,
_labels_train,
_input_data_test,
_labels_test,
_epochs,
_batch_size,
_layers,
_embs_list=[],
):
"""Train a model, print the test accuracy, and return the trained model"""
model = model_with_embeddings(
self.vocabulary_sizes,
self.max_length,
self.categorical_columns,
self.num_ordinal_features,
_layers,
_embs_list,
)
train_history = model.train(
_input_data_train, _labels_train, _epochs, _batch_size
)
# (self.visualize).plot_training_history(
# train_history, "train_history_" + str(self.num_models) + ".png"
# )
test_acc = model.test_accuracy(_input_data_test, _labels_test)
print("Testing Accuracy = %f" % (test_acc * 100))
self.num_models += 1
return model
class model_with_embeddings(object):
"""This is a class that implements a basic model with embeddings"""
def __init__(
self,
vocabulary_sizes,
max_length,
_categorecal_features,
num_ordinal_features,
dense_nodes,
pretrained_embeddings,
quiet=False,
):
"""Setup the model based on the sizes of the feature arrays"""
# Note that the vocabulary size will have to accomm
nodes_in_embedding_layer = [
max(2, int(np.ceil(np.sqrt(np.sqrt(v))))) for v in vocabulary_sizes
]
# Create embeddings for the categorical inputs
embedding_inputs = []
flat_embeddings = []
models = []
self.emb_names = [
(c.replace(" ", "_") + "_embedding") for c in _categorecal_features
]
for i, vocab_size in enumerate(vocabulary_sizes):
embedding_inputs.append(Input(shape=(max_length,)))
if len(pretrained_embeddings) == 0:
embedding_i = Embedding(
vocab_size,
nodes_in_embedding_layer[i],
name=self.emb_names[i],
input_length=max_length, # weights=[word_weight_matrix],
trainable=True,
)(embedding_inputs[i])
else:
embedding_i = Embedding(
vocab_size,
nodes_in_embedding_layer[i],
name=self.emb_names[i],
input_length=max_length,
weights=[pretrained_embeddings[i]],
trainable=True,
)(embedding_inputs[i])
flat_embeddings.append(Flatten()(embedding_i))
models.append(Model(inputs=embedding_inputs[i], outputs=flat_embeddings[i]))
# Merge embeddings with ordinal inputs
ordinal_inputs = [Input(shape=(1,)) for i in range(num_ordinal_features)]
concatenated = concatenate(flat_embeddings + ordinal_inputs)
# Deep network after all inputs have been incorporated
hidden_layers = [concatenated]
for i in range(len(dense_nodes)):
hidden_layer = Dense(dense_nodes[i], activation="relu")(
BatchNormalization()(hidden_layers[i])
)
hidden_layers.append(hidden_layer)
output = Dense(1, activation="sigmoid")(hidden_layers[-1])
self.merged_model = Model(
inputs=embedding_inputs + ordinal_inputs, outputs=output
)
def train(self, train_input_data, train_labels, _epochs, _batch_size):
"""compiles the model, fits the _input_data to the _labels, and evaluates the accuracy
of the merged_model"""
# compile the model
(self.merged_model).compile(
optimizer="adam", loss="binary_crossentropy", metrics=["acc"]
)
# fit the model
history = (self.merged_model).fit(
train_input_data,
train_labels,
batch_size=_batch_size,
epochs=_epochs,
verbose=1,
)
return history
def save(self,name):
self.merged_model.save(name)
def test_accuracy(self, _input_data, _labels, quiet=False):
"""evaluate the accuracy of the model"""
test_loss, test_accuracy = (self.merged_model).evaluate(
_input_data, _labels, verbose=0
)
return test_accuracy
def predict_prob(self, _input_data):
"""predict probabilities for test set"""
yhat_probs = (self.merged_model).predict(_input_data, verbose=0)
return yhat_probs[:, 0]
def predict(self, _input_data):
"""predict classes for test set"""
yhat_probs = (self.merged_model).predict(_input_data, verbose=0)
return (yhat_probs[:, 0] > 0.5).astype(int)
def embeddings_names(self):
return self.emb_names
def extract_weights(self, name):
"""Extract weights from a neural network model"""
# Extract weights
weight_layer = (self.merged_model).get_layer(name)
weights = weight_layer.get_weights()[0]
return weights