-
Notifications
You must be signed in to change notification settings - Fork 19
/
custom_config_example.py
65 lines (49 loc) · 2.2 KB
/
custom_config_example.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
from typing import List
import numpy as np
import pandas as pd
from keras.engine import Layer
from keras.layers import Dense, Activation, Concatenate
from sklearn.preprocessing import MinMaxScaler
from entity_embeddings import Config, Embedder
from entity_embeddings.network import ModelAssembler
from entity_embeddings.processor import TargetProcessor
from entity_embeddings.util import visualization_utils
class CustomProcessor(TargetProcessor):
def process_target(self, y: List) -> np.ndarray:
# just for example purposes, let's use a MinMaxScaler
return MinMaxScaler().fit_transform(pd.DataFrame(y))
class CustomAssembler(ModelAssembler):
def make_final_layer(self, previous_layer: Layer):
output_model = Dense(1)(previous_layer)
output_model = Activation('sigmoid')(output_model)
return output_model
def compile_model(self, model):
model.compile(loss='mean_absolute_error', optimizer='adam')
return model
"""
You can aso customize the hidden layers of the network
"""
def make_hidden_layers(self, outputs: List[Layer]):
output_model = Concatenate()(outputs)
output_model = Dense(5000, kernel_initializer="uniform")(output_model)
output_model = Activation('relu')(output_model)
output_model = Dense(1000, kernel_initializer="uniform")(output_model)
output_model = Activation('relu')(output_model)
return output_model
def main():
custom_processor = CustomProcessor()
custom_assembler = CustomAssembler()
data_path = "../ross_short.csv"
config = Config.make_custom_config(csv_path=data_path,
target_name='Sales',
train_ratio=0.9,
target_processor=custom_processor,
model_assembler=custom_assembler,
epochs=1,
verbose=True,
artifacts_path='artifacts')
embedder = Embedder(config)
embedder.perform_embedding()
visualization_utils.make_visualizations_from_config(config)
if __name__ == '__main__':
main()