1
1
class AblationStudy (object ):
2
- def __init__ (self , training_dataset_name , training_dataset_version ,
3
- label_name , ** kwargs ):
2
+ """The `AblationStudy` object is the entry point to define an ablation
3
+ study with maggy. This object can subsequently be passed as an argument
4
+ when the experiment is launched with `experiment.lagom()`.
5
+
6
+ Sample usage:
7
+
8
+ >>> from maggy.ablation import AblationStudy
9
+ >>> ablation_study = AblationStudy('titanic_train_dataset',
10
+ >>> label_name='survived')
11
+
12
+ Define your study by including layers and features, which should be
13
+ ablated:
14
+
15
+ >>> ablation_study.features.include('pclass', 'fare')
16
+ >>> ablation_study.model.layers.include('my_dense_two',
17
+ >>> 'my_dense_three')
18
+
19
+ You can also add a layer group using a list:
20
+
21
+ >>> ablation_study.model.layers.include_groups(['my_dense_two',
22
+ >>> 'my_dense_four'])
23
+
24
+ Or add a layer group using a prefix:
25
+
26
+ >>> ablation_study.model.layers.include_groups(prefix='my_dense')
27
+
28
+ Next you should define a base model function using the layer and feature
29
+ names you previously specified:
30
+
31
+ >>> # you only need to add the `name` parameter to layer initializers
32
+ >>> def base_model_generator():
33
+ >>> model = tf.keras.Sequential()
34
+ >>> model.add(tf.keras.layers.Dense(64, activation='relu'))
35
+ >>> model.add(tf.keras.layers.Dense(..., name='my_dense_two', ...)
36
+ >>> model.add(tf.keras.layers.Dense(32, activation='relu'))
37
+ >>> model.add(tf.keras.layers.Dense(..., name='my_dense_sigmoid', ...)
38
+ >>> # output layer
39
+ >>> model.add(tf.keras.layers.Dense(1, activation='linear'))
40
+ >>> return model
41
+
42
+ Make sure to include the generator function in the study:
43
+
44
+ >>> ablation_study.model.set_base_model_generator(base_model_generator)
45
+
46
+ Last but not least you can define your actual training function:
47
+
48
+ >>> from maggy import experiment
49
+ >>> from maggy.callbacks import KerasBatchEnd
50
+
51
+ >>> def training_function(dataset_function, model_function, reporter):
52
+ >>> import tensorflow as tf
53
+ >>> epochs = 5
54
+ >>> batch_size = 10
55
+ >>> tf_dataset = dataset_function(epochs, batch_size)
56
+ >>> model = model_function()
57
+ >>> model.compile(optimizer=tf.train.AdamOptimizer(0.001),
58
+ >>> loss='binary_crossentropy',
59
+ >>> metrics=['accuracy'])
60
+ >>> ### Maggy REPORTER
61
+ >>> callbacks = [KerasBatchEnd(reporter, metric='acc')]
62
+ >>> history = model.fit(tf_dataset, epochs=5, steps_per_epoch=30)
63
+ >>> return float(history.history['acc'][-1])
64
+
65
+ Lagom the experiment:
66
+
67
+ >>> result = experiment.lagom(map_fun=training_function,
68
+ >>> experiment_type='ablation',
69
+ >>> ablation_study=ablation_study,
70
+ >>> ablator='loco',
71
+ >>> name='Titanic-LOCO',
72
+ >>> hb_interval=5)
73
+ """
74
+
75
+ def __init__ (
76
+ self , training_dataset_name , training_dataset_version , label_name ,
77
+ ** kwargs ):
78
+ """Initializes the ablation study.
79
+
80
+ :param training_dataset_name: Name of the training dataset in the
81
+ featurestore.
82
+ :type training_dataset_name: str
83
+ :param training_dataset_version: Version of the training dataset to be
84
+ used.
85
+ :type training_dataset_version: int
86
+ :param label_name: Name of the target prediction label.
87
+ :type label_name: str
88
+ """
4
89
self .features = Features ()
5
90
self .model = Model ()
6
91
self .hops_training_dataset_name = training_dataset_name
@@ -11,8 +96,9 @@ def __init__(self, training_dataset_name, training_dataset_version,
11
96
def to_dict (self ):
12
97
"""
13
98
Returns the ablation study configuration as a Python dictionary.
14
- :return: A dictionary with ablation study configuration parameters as keys
15
- (i.e. 'training_dataset_name', 'included_features', etc.)
99
+
100
+ :return: A dictionary with ablation study configuration parameters as
101
+ keys (i.e. 'training_dataset_name', 'included_features', etc.)
16
102
:rtype: dict
17
103
"""
18
104
ablation_dict = {
0 commit comments