{
   "components":{
      "comp-getbesthyperparametersop":{
         "executorLabel":"exec-getbesthyperparametersop",
         "inputDefinitions":{
            "parameters":{
               "study_spec_metrics":{
                  "parameterType":"LIST"
               },
               "trials":{
                  "parameterType":"LIST"
               }
            }
         },
         "outputDefinitions":{
            "parameters":{
               "Output":{
                  "parameterType":"LIST"
               }
            }
         }
      }
   },
   "deploymentSpec":{
      "executors":{
         "exec-getbesthyperparametersop":{
            "container":{
               "args":[
                  "--executor_input",
                  "{{$}}",
                  "--function_to_execute",
                  "GetBestHyperparametersOp"
               ],
               "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 'google-cloud-aiplatform' 'kfp==2.0.0-alpha.0' && \"$0\" \"$@\"\n",
                  "sh",
                  "-ec",
                  "program_path=$(mktemp -d)\nprintf \"%s\" \"$0\" > \"$program_path/ephemeral_component.py\"\npython3 -m kfp.v2.components.executor_main                         --component_module_path                         \"$program_path/ephemeral_component.py\"                         \"$@\"\n",
                  "\nimport kfp\nfrom kfp.v2 import dsl\nfrom kfp.v2.dsl import *\nfrom typing import *\n\ndef GetBestHyperparametersOp(trials: list, study_spec_metrics: list) -> list:\n  \"\"\"Retrieves the best hyperparameters based on the trials.\n\n  Args:\n      trials (list):\n        Required. List representing the intermediate\n        JSON representation of the trials from the hyperparameter tuning job.\n      study_spec_metrics (list):\n        Required. List serialized from dictionary representing the metrics to optimize.\n        The dictionary key is the metric_id, which is reported by your training\n        job, and the dictionary value is the optimization goal of the metric\n        ('minimize' or 'maximize'). example:\n        metrics = hyperparameter_tuning_job.serialize_metrics({'loss': 'minimize', 'accuracy': 'maximize'})\n\n  Returns:\n      List representing the intermediate JSON representation of the best\n      hyperparameters from the best trial in the list of trials.\n\n  Raises:\n      RuntimeError: If there are multiple metrics.\n  \"\"\"\n  from google.cloud.aiplatform.aiplatform_v1.types import study\n\n  if len(study_spec_metrics) > 1:\n    raise RuntimeError('Unable to determine best parameters for multi-objective'\n                       ' hyperparameter tuning.')\n  trials_list = [study.Trial.from_json(trial) for trial in trials]\n  best_trial = None\n  goal = study_spec_metrics[0]['goal']\n  best_fn = None\n  if goal == study.StudySpec.MetricSpec.GoalType.MAXIMIZE:\n    best_fn = max\n  elif goal == study.StudySpec.MetricSpec.GoalType.MINIMIZE:\n    best_fn = min\n  best_trial = best_fn(\n      trials_list, key=lambda trial: trial.final_measurement.metrics[0].value)\n\n  return [\n      study.Trial.Parameter.to_json(param) for param in best_trial.parameters\n  ]\n\n"
               ],
               "image":"python:3.7"
            }
         }
      }
   },
   "pipelineInfo":{
      "name":"get-best-hyperparameters-op-test"
   },
   "root":{
      "dag":{
         "tasks":{
            "getbesthyperparametersop":{
               "cachingOptions":{
                  "enableCache":true
               },
               "componentRef":{
                  "name":"comp-getbesthyperparametersop"
               },
               "inputs":{
                  "parameters":{
                     "study_spec_metrics":{
                        "runtimeValue":{
                           "constant":[
                              {
                                 "goal":1.0,
                                 "metric_id":"accuracy"
                              }
                           ]
                        }
                     },
                     "trials":{
                        "runtimeValue":{
                           "constant":[
                              "{\n \"id\": \"1\",\n \"state\": 4,\n \"parameters\": [\n {\n \"parameterId\": \"learning_rate\",\n \"value\": 0.03162277660168379\n },\n {\n \"parameterId\": \"momentum\",\n \"value\": 0.5\n },\n {\n \"parameterId\": \"num_neurons\",\n \"value\": 128.0\n }\n ],\n \"finalMeasurement\": {\n \"stepCount\": \"10\",\n \"metrics\": [\n {\n \"metricId\": \"accuracy\",\n \"value\": 0.734375\n }\n ]\n },\n \"startTime\": \"2021-12-10T00:41:57.675086142Z\",\n \"endTime\": \"2021-12-10T00:52:35Z\",\n \"name\": \"\",\n \"measurements\": [],\n \"clientId\": \"\",\n \"infeasibleReason\": \"\",\n \"customJob\": \"\"\n}"
                           ]
                        }
                     }
                  }
               },
               "taskInfo":{
                  "name":"getbesthyperparametersop"
               }
            }
         }
      }
   }
}
