-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding_training.py
More file actions
200 lines (164 loc) · 7.28 KB
/
embedding_training.py
File metadata and controls
200 lines (164 loc) · 7.28 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
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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 21:23:55 2018
@author: Manuel Camargo
"""
import os
import random
import itertools
import math
import pandas as pd
import numpy as np
from keras.models import Model
from keras.layers import Input, Embedding, Dot, Reshape
from support_modules.readers import log_reader as lr
from support_modules import role_discovery as rl
from support_modules import support as sup
from support_modules import nn_support as nsup
def training_model(parameters, timeformat, no_loops=False):
"""Main method of the embedding training module.
Args:
parameters (dict): parameters for training the embeddeding network.
timeformat (str): event-log date-time format.
no_loops (boolean): remove loops fom the event-log (optional).
"""
log = lr.LogReader(os.path.join('input_files', parameters['file_name']),
timeformat, timeformat, one_timestamp=True)
# Pre-processing tasks
_, resource_table = rl.read_resource_pool(log, sim_percentage=0.50)
# Role discovery
log_df_resources = pd.DataFrame.from_records(resource_table)
log_df_resources = log_df_resources.rename(index=str, columns={"resource": "user"})
# Dataframe creation
log_df = pd.DataFrame.from_records(log.data)
log_df = log_df.merge(log_df_resources, on='user', how='left')
log_df = log_df[log_df.task != 'Start']
log_df = log_df[log_df.task != 'End']
log_df = log_df.reset_index(drop=True)
if no_loops:
log_df = nsup.reduce_loops(log_df)
# Index creation
ac_index = create_index(log_df, 'task')
ac_index['start'] = 0
ac_index['end'] = len(ac_index)
index_ac = {v: k for k, v in ac_index.items()}
rl_index = create_index(log_df, 'role')
rl_index['start'] = 0
rl_index['end'] = len(rl_index)
index_rl = {v: k for k, v in rl_index.items()}
# Define the number of dimensions as the 4th root of the number of categories
dim_number = math.ceil(len(list(itertools.product(*[list(ac_index.items()),
list(rl_index.items())])))**0.25)
ac_weights, rl_weights = train_embedded(log_df, ac_index, rl_index, dim_number)
sup.create_file_from_list(reformat_matrix(index_ac, ac_weights),
os.path.join(os.path.join('input_files', 'embedded_matix'),
'ac_'+ parameters['file_name'].split('.')[0]+'.emb'))
sup.create_file_from_list(reformat_matrix(index_rl, rl_weights),
os.path.join(os.path.join('input_files', 'embedded_matix'),
'rl_'+ parameters['file_name'].split('.')[0]+'.emb'))
# =============================================================================
# Pre-processing: embedded dimension
# =============================================================================
def train_embedded(log_df, ac_index, rl_index, dim_number):
"""Carry out the training of the embeddings"""
# Iterate through each book
pairs = list()
for i in range(0, len(log_df)):
# Iterate through the links in the book
pairs.append((ac_index[log_df.iloc[i]['task']], rl_index[log_df.iloc[i]['role']]))
model = ac_rl_embedding_model(ac_index, rl_index, dim_number)
model.summary()
n_positive = 1024
gen = generate_batch(pairs, ac_index, rl_index, n_positive, negative_ratio=2)
# Train
model.fit_generator(gen, epochs=100,
steps_per_epoch=len(pairs) // n_positive,
verbose=2)
# Extract embeddings
ac_layer = model.get_layer('activity_embedding')
rl_layer = model.get_layer('role_embedding')
ac_weights = ac_layer.get_weights()[0]
rl_weights = rl_layer.get_weights()[0]
return ac_weights, rl_weights
def generate_batch(pairs, ac_index, rl_index, n_positive=50,
negative_ratio=1.0):
"""Generate batches of samples for training"""
batch_size = n_positive * (1 + negative_ratio)
batch = np.zeros((batch_size, 3))
pairs_set = set(pairs)
activities = list(ac_index.keys())
roles = list(rl_index.keys())
# This creates a generator
while True:
# randomly choose positive examples
idx = 0
for idx, (activity, role) in enumerate(random.sample(pairs, n_positive)):
batch[idx, :] = (activity, role, 1)
# Increment idx by 1
idx += 1
# Add negative examples until reach batch size
while idx < batch_size:
# random selection
random_ac = random.randrange(len(activities))
random_rl = random.randrange(len(roles))
# Check to make sure this is not a positive example
if (random_ac, random_rl) not in pairs_set:
# Add to batch and increment index, label 0 due classification task
batch[idx, :] = (random_ac, random_rl, 0)
idx += 1
# Make sure to shuffle order
np.random.shuffle(batch)
yield {'activity': batch[:, 0], 'role': batch[:, 1]}, batch[:, 2]
def ac_rl_embedding_model(ac_index, rl_index, embedding_size):
"""Model to embed activities and roles using the functional API"""
# Both inputs are 1-dimensional
activity = Input(name='activity', shape=[1])
role = Input(name='role', shape=[1])
# Embedding the activity (shape will be (None, 1, embedding_size))
activity_embedding = Embedding(name='activity_embedding',
input_dim=len(ac_index),
output_dim=embedding_size)(activity)
# Embedding the role (shape will be (None, 1, embedding_size))
role_embedding = Embedding(name='role_embedding',
input_dim=len(rl_index),
output_dim=embedding_size)(role)
# Merge the layers with a dot product along the second axis (shape will be (None, 1, 1))
merged = Dot(name='dot_product', normalize=True, axes=2)([activity_embedding, role_embedding])
# Reshape to be a single number (shape will be (None, 1))
merged = Reshape(target_shape=[1])(merged)
# Loss function is mean squared error
model = Model(inputs=[activity, role], outputs=merged)
model.compile(optimizer='Adam', loss='mse')
return model
# =============================================================================
# Support
# =============================================================================
def reformat_matrix(index, weigths):
"""Reformating of the embedded matrix for exporting.
Args:
index: index of activities or roles.
weigths: matrix of calculated coordinates.
Returns:
matrix with indexes.
"""
matrix = list()
for i, _ in enumerate(index):
data = [i, index[i]]
data.extend(weigths[i])
matrix.append(data)
return matrix
def create_index(log_df, column):
"""Creates an idx for a categorical attribute.
Args:
log_df: dataframe.
column: column name.
Returns:
index of a categorical attribute pairs.
"""
temp_list = log_df[[column]].values.tolist()
subsec_set = {(x[0]) for x in temp_list}
subsec_set = sorted(list(subsec_set))
alias = dict()
for i, _ in enumerate(subsec_set):
alias[subsec_set[i]] = i + 1
return alias