Exemplo n.º 1
0
    def test_metalearner_nearest_neighbor(self):

        meta_train_data = {}
        metadata_indices = [1, 2, 3]
        for ix, dataset_id in enumerate(metadata_indices):
            hparams = standard_artifacts.HyperParameters()
            hparams.uri = os.path.join(self._input_data_dir,
                                       f'Tuner.train_mockdata_{dataset_id}',
                                       'best_hyperparameters')
            meta_train_data[f'hparams_train_{ix}'] = [hparams]

            metafeature = artifacts.MetaFeatures()
            metafeature.uri = os.path.join(
                self._input_data_dir,
                f'MetaFeatureGen.train_mockdata_{dataset_id}', 'metafeatures')
            meta_train_data[f'meta_train_features_{ix}'] = [metafeature]

        input_dict = {
            **meta_train_data,
        }
        output_dict = {
            executor.OUTPUT_HYPERPARAMS: [self._hparams_out],
            executor.OUTPUT_MODEL: [self._model_out],
        }

        exec_properties = self._exec_properties.copy()
        exec_properties['algorithm'] = executor.NEAREST_NEIGHBOR
        ex = executor.MetaLearnerExecutor()
        ex.Do(input_dict, output_dict, exec_properties)

        self._verify_hparams_outputs(executor.NEAREST_NEIGHBOR)
        self._verify_model_export()
Exemplo n.º 2
0
  def setUp(self):
    super(ComponentTest, self).setUp()

    num_train = 5
    self.meta_train_data = {}
    for ix in range(num_train):
      self.meta_train_data[f'hparams_train_{ix}'] = channel_utils.as_channel(
          [standard_artifacts.HyperParameters()])
      self.meta_train_data[
          f'meta_train_features_{ix}'] = channel_utils.as_channel(
              [artifacts.MetaFeatures()])

    self.custom_config = {'some': 'thing', 'some other': 1, 'thing': 2}
Exemplo n.º 3
0
    def testDoWithNearestNeighbor(self):

        exec_properties = self._exec_properties.copy()
        exec_properties['tuner_fn'] = '%s.%s' % (
            tuner_module.tuner_fn.__module__, tuner_module.tuner_fn.__name__)
        exec_properties['metalearning_algorithm'] = 'nearest_neighbor'

        input_dict = self._input_dict.copy()

        ps_type = ps_pb2.Type(
            binary_classification=ps_pb2.BinaryClassification(label='class'))
        ps = ps_pb2.ProblemStatement(
            owner=['nitroml'],
            tasks=[ps_pb2.Task(
                name='mockdata_1',
                type=ps_type,
            )])

        exec_properties['custom_config'] = json_utils.dumps({
            'problem_statement':
            text_format.MessageToString(message=ps, as_utf8=True),
        })

        hps_artifact = artifacts.KCandidateHyperParameters()
        hps_artifact.uri = os.path.join(self._testdata_dir,
                                        'MetaLearner.nearest_neighbor',
                                        'hparams_out')
        input_dict['warmup_hyperparameters'] = [hps_artifact]

        model_artifact = standard_artifacts.Model()
        model_artifact.uri = os.path.join(self._testdata_dir,
                                          'MetaLearner.nearest_neighbor',
                                          'model')
        input_dict['metamodel'] = [model_artifact]

        metafeature = artifacts.MetaFeatures()
        metafeature.uri = os.path.join(self._testdata_dir,
                                       'MetaFeatureGen.test_mockdata',
                                       'metafeatures')
        input_dict['metafeature'] = [metafeature]

        tuner = executor.Executor(self._context)
        tuner.Do(input_dict=input_dict,
                 output_dict=self._output_dict,
                 exec_properties=exec_properties)
        self._verify_output()
Exemplo n.º 4
0
    def setUp(self):
        super(ExecutorTest, self).setUp()

        source_data_dir = os.path.dirname(os.path.dirname(__file__))
        input_data_dir = os.path.join(source_data_dir, 'testdata')

        statistics = standard_artifacts.ExampleStatistics()
        statistics.uri = os.path.join(input_data_dir,
                                      'StatisticsGen.train_mockdata_1',
                                      'statistics', '5')
        statistics.split_names = artifact_utils.encode_split_names(
            ['train', 'eval'])
        transformed_examples = standard_artifacts.Examples()
        transformed_examples.uri = os.path.join(input_data_dir,
                                                'Transform.train_mockdata_1',
                                                'transformed_examples', '10')
        transformed_examples.split_names = artifact_utils.encode_split_names(
            ['train', 'eval'])
        self._input_dict = {
            executor.EXAMPLES_KEY: [transformed_examples],
            executor.STATISTICS_KEY: [statistics],
        }

        output_data_dir = os.path.join(
            os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR',
                           tempfile.mkdtemp(dir=flags.FLAGS.test_tmpdir)),
            self._testMethodName)
        self._metafeatures = artifacts.MetaFeatures()
        self._metafeatures.uri = output_data_dir
        self._output_dict = {
            executor.METAFEATURES_KEY: [self._metafeatures],
        }

        self._exec_properties = {
            'custom_config': {
                'problem_statement_path': '/some/fake/path'
            }
        }
Exemplo n.º 5
0
  def __init__(self,
               statistics: types.Channel = None,
               transformed_examples: Optional[types.Channel] = None,
               custom_config: Optional[Dict[str, Any]] = None,
               instance_name: Optional[str] = None):
    """Construct a MetaFeatureGen component.

    Args:
      statistics: Output channel from StatisticsGen.
      transformed_examples: Optional channel from tfx Transform component.
      custom_config: Optional dict which contains addtional parameters.
      instance_name: Optional unique instance name. Necessary if multiple
        MetaFeatureGen components are declared in the same pipeline.
    """

    metafeatures = types.Channel(
        type=artifacts.MetaFeatures, artifacts=[artifacts.MetaFeatures()])
    spec = MetaFeatureGenSpec(
        metafeatures=metafeatures,
        transformed_examples=transformed_examples,
        statistics=statistics,
        custom_config=custom_config)
    super(MetaFeatureGen, self).__init__(spec=spec, instance_name=instance_name)