-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlp-example.yaml
455 lines (431 loc) · 18.7 KB
/
mlp-example.yaml
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# PIPELINE DEFINITION
# Name: mlp-example-pipeline
# Inputs:
# message: str [Default: 'message']
components:
comp-data-preparation:
executorLabel: exec-data-preparation
inputDefinitions:
artifacts:
dataset:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
outputDefinitions:
artifacts:
dataset_transformed:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
comp-data-validation:
executorLabel: exec-data-validation
inputDefinitions:
artifacts:
dataset:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
outputDefinitions:
artifacts:
reports:
artifactType:
schemaTitle: system.HTML
schemaVersion: 0.0.1
tests:
artifactType:
schemaTitle: system.HTML
schemaVersion: 0.0.1
comp-exctract-data:
executorLabel: exec-exctract-data
outputDefinitions:
artifacts:
dataset:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
comp-split-data:
executorLabel: exec-split-data
inputDefinitions:
artifacts:
dataset:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
outputDefinitions:
artifacts:
dataset_test:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
dataset_train:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
dataset_validation:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
comp-train-model:
executorLabel: exec-train-model
inputDefinitions:
artifacts:
dataset_train:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
dataset_validation:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
parameters:
epochs:
defaultValue: 10.0
isOptional: true
parameterType: NUMBER_INTEGER
outputDefinitions:
artifacts:
model_artifact:
artifactType:
schemaTitle: system.Model
schemaVersion: 0.0.1
comp-validate-model:
executorLabel: exec-validate-model
inputDefinitions:
artifacts:
dataset_test:
artifactType:
schemaTitle: system.Dataset
schemaVersion: 0.0.1
model_artifact:
artifactType:
schemaTitle: system.Model
schemaVersion: 0.0.1
deploymentSpec:
executors:
exec-data-preparation:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- data_preparation
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'scikit-learn'\
\ 'pandas' && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef data_preparation(\n dataset: Input[Dataset],\n dataset_transformed:\
\ Output[Dataset],\n):\n import pandas as pd\n from sklearn.preprocessing\
\ import MinMaxScaler, OneHotEncoder \n\n NUMERIC_FEATURE_KEYS = ['sepal\
\ length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']\n\
\ LABEL_KEY = 'target'\n\n def preprocessing_fn(inputs):\n \
\ \"\"\"Preprocess input columns into transformed columns.\"\"\"\n \
\ outputs = inputs.copy()\n\n # Scale numeric features to range\
\ [0, 1]\n scaler = MinMaxScaler()\n outputs[NUMERIC_FEATURE_KEYS]\
\ = scaler.fit_transform(outputs[NUMERIC_FEATURE_KEYS])\n\n # One-hot\
\ encode the label column\n encoder = OneHotEncoder()\n outputs[LABEL_KEY]\
\ = encoder.fit_transform(outputs[LABEL_KEY].values.reshape(-1, 1)).toarray()\n\
\n return outputs\n\n with open(dataset.path, 'r') as input_file:\n\
\ data = pd.read_csv(input_file)\n\n data_transformed = preprocessing_fn(data)\n\
\n with open(dataset_transformed.path, 'w') as f:\n f.write(data_transformed.to_csv(index=False))\n\
\n"
image: python:3.9
exec-data-validation:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- data_validation
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'evidently'\
\ && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef data_validation(\n dataset: Input[Dataset],\n tests: Output[HTML],\n\
\ reports: Output[HTML],\n):\n from evidently.test_suite import TestSuite\n\
\ from evidently.test_preset import DataQualityTestPreset\n from evidently.test_preset\
\ import DataStabilityTestPreset\n from evidently.report import Report\n\
\ from evidently.metric_preset import DataQualityPreset\n from evidently.metric_preset\
\ import DataDriftPreset\n import pandas as pd\n\n with open(dataset.path,\
\ 'r') as input_file:\n data = pd.read_csv(input_file)\n\n tests_suite=\
\ TestSuite(tests=[\n DataStabilityTestPreset(),\n DataQualityTestPreset()\n\
\ ])\n tests_suite.run(current_data=data.iloc[:60], reference_data=data.iloc[60:],\
\ column_mapping=None)\n tests_suite.save_html(tests.path)\n\n\n reports_suite\
\ = Report(metrics=[\n DataQualityPreset(),\n DataDriftPreset()\n\
\ ])\n\n reports_suite.run(current_data=data.iloc[:60], reference_data=data.iloc[60:],\
\ column_mapping=None)\n reports_suite.save_html(reports.path)\n\n"
image: python:3.10
exec-exctract-data:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- exctract_data
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'scikit-learn'\
\ 'pandas' && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef exctract_data(\n dataset: Output[Dataset],\n):\n from sklearn\
\ import datasets\n data = datasets.load_iris(as_frame=True).frame\n\
\ with open(dataset.path, 'w') as f:\n f.write(data.to_csv(index=False))\n\
\n"
image: python:3.7
exec-split-data:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- split_data
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'pandas' 'scikit-learn'\
\ && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef split_data(dataset: Input[Dataset], dataset_train: Output[Dataset],\
\ dataset_validation: Output[Dataset], dataset_test: Output[Dataset]):\n\
\ import pandas as pd\n from sklearn.model_selection import train_test_split\n\
\ with open(dataset.path, 'r') as input_file:\n data = pd.read_csv(input_file)\n\
\n train, test = train_test_split(data, test_size=0.2)\n train, validation\
\ = train_test_split(train, test_size=0.2)\n\n with open(dataset_train.path,\
\ 'w') as f:\n f.write(train.to_csv(index=False))\n with open(dataset_validation.path,\
\ 'w') as f:\n f.write(validation.to_csv(index=False))\n with\
\ open(dataset_test.path, 'w') as f:\n f.write(test.to_csv(index=False))\n\
\n"
image: python:3.9
exec-train-model:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- train_model
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'tensorflow'\
\ 'pandas' 'joblib' && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef train_model(\n dataset_train: Input[Dataset],\n dataset_validation:\
\ Input[Dataset],\n model_artifact: Output[Model],\n epochs: int =\
\ 10,\n):\n import pandas as pd\n import tensorflow as tf\n from\
\ tensorflow import keras\n from tensorflow.keras import layers\n \
\ def create_model():\n tf_model = keras.Sequential([\n \
\ layers.Dense(64, activation='relu', input_shape=(4,)),\n \
\ layers.Dense(64, activation='relu'),\n layers.Dense(3, activation='softmax')\n\
\ ])\n return tf_model\n\n\n with open(dataset_train.path,\
\ 'r') as train_file:\n train_data = pd.read_csv(train_file)\n \
\ with open(dataset_validation.path, 'r') as validation_file:\n \
\ validation_data = pd.read_csv(validation_file)\n\n # Preprocess the\
\ data\n train_features = train_data.drop('target', axis=1)\n train_labels\
\ = train_data['target']\n validation_features = validation_data.drop('target',\
\ axis=1)\n validation_labels = validation_data['target']\n\n # Create\
\ the model\n tf_model = create_model()\n\n # Compile the model\n\
\ tf_model.compile(optimizer='adam',\n loss='sparse_categorical_crossentropy',\n\
\ metrics=['accuracy'])\n\n # Train the model\n \
\ tf_model.fit(train_features, train_labels, epochs=epochs, validation_data=(validation_features,\
\ validation_labels))\n\n # Evaluate the model\n r = tf_model.evaluate(validation_features,\
\ validation_labels)\n print(\"Result:\",r)\n\n # Save the model\n\
\ tf_model.save( \"model_artifact.keras\")\n\n # Copy the model to\
\ the output path\n import shutil\n shutil.move(\"model_artifact.keras\"\
, model_artifact.path)\n\n"
image: python:3.9
exec-validate-model:
container:
args:
- --executor_input
- '{{$}}'
- --function_to_execute
- validate_model
command:
- sh
- -c
- "\nif ! [ -x \"$(command -v pip)\" ]; then\n python3 -m ensurepip ||\
\ python3 -m ensurepip --user || apt-get install python3-pip\nfi\n\nPIP_DISABLE_PIP_VERSION_CHECK=1\
\ python3 -m pip install --quiet --no-warn-script-location 'kfp==2.2.0'\
\ '--no-deps' 'typing-extensions>=3.7.4,<5; python_version<\"3.9\"' &&\
\ python3 -m pip install --quiet --no-warn-script-location 'tensorflow'\
\ 'pandas' && \"$0\" \"$@\"\n"
- sh
- -ec
- 'program_path=$(mktemp -d)
printf "%s" "$0" > "$program_path/ephemeral_component.py"
_KFP_RUNTIME=true python3 -m kfp.dsl.executor_main --component_module_path "$program_path/ephemeral_component.py" "$@"
'
- "\nimport kfp\nfrom kfp import dsl\nfrom kfp.dsl import *\nfrom typing import\
\ *\n\ndef validate_model(model_artifact: Input[Model], dataset_test: Input[Dataset]):\n\
\ import pandas as pd\n import tensorflow as tf\n\n # Copy the\
\ model to the current directory\n import shutil\n shutil.copy(model_artifact.path,\
\ \"model_artifact.keras\")\n\n # Load the model\n tf_model = tf.keras.models.load_model(\"\
model_artifact.keras\")\n\n with open(dataset_test.path, 'r') as test_file:\n\
\ test_data = pd.read_csv(test_file)\n\n # Preprocess the data\n\
\ test_features = test_data.drop('target', axis=1)\n test_labels =\
\ test_data['target']\n\n # Evaluate the model\n r = tf_model.evaluate(test_features,\
\ test_labels)\n print(r)\n\n"
image: python:3.9
pipelineInfo:
name: mlp-example-pipeline
root:
dag:
tasks:
data-preparation:
cachingOptions:
enableCache: true
componentRef:
name: comp-data-preparation
dependentTasks:
- data-validation
- exctract-data
inputs:
artifacts:
dataset:
taskOutputArtifact:
outputArtifactKey: dataset
producerTask: exctract-data
taskInfo:
name: data-preparation
data-validation:
cachingOptions:
enableCache: true
componentRef:
name: comp-data-validation
dependentTasks:
- exctract-data
inputs:
artifacts:
dataset:
taskOutputArtifact:
outputArtifactKey: dataset
producerTask: exctract-data
taskInfo:
name: data-validation
exctract-data:
cachingOptions:
enableCache: true
componentRef:
name: comp-exctract-data
taskInfo:
name: exctract-data
split-data:
cachingOptions:
enableCache: true
componentRef:
name: comp-split-data
dependentTasks:
- data-preparation
inputs:
artifacts:
dataset:
taskOutputArtifact:
outputArtifactKey: dataset_transformed
producerTask: data-preparation
taskInfo:
name: split-data
train-model:
cachingOptions:
enableCache: true
componentRef:
name: comp-train-model
dependentTasks:
- split-data
inputs:
artifacts:
dataset_train:
taskOutputArtifact:
outputArtifactKey: dataset_train
producerTask: split-data
dataset_validation:
taskOutputArtifact:
outputArtifactKey: dataset_validation
producerTask: split-data
parameters:
epochs:
runtimeValue:
constant: 30.0
taskInfo:
name: train-model
validate-model:
cachingOptions:
enableCache: true
componentRef:
name: comp-validate-model
dependentTasks:
- split-data
- train-model
inputs:
artifacts:
dataset_test:
taskOutputArtifact:
outputArtifactKey: dataset_test
producerTask: split-data
model_artifact:
taskOutputArtifact:
outputArtifactKey: model_artifact
producerTask: train-model
taskInfo:
name: validate-model
inputDefinitions:
parameters:
message:
defaultValue: message
isOptional: true
parameterType: STRING
schemaVersion: 2.1.0
sdkVersion: kfp-2.2.0