Package: scikit-nni
scikit-nniHyper parameters search for scikit-learn components using Microsoft NNIFree software: Apache Software License 2.0Documentation:https://scikit-nni.readthedocs.io.IntroductionMicrosoft NNI (Neural Network Intelligence) is a toolkit to help users run automated machine learning (AutoML) experiments.
The tool dispatches and runs trial jobs generated by tuning algorithms to search the best neural architecture and/or hyper-parameters in different environments like local machine, remote servers and cloud.Read and explore more about Microsoft NNI here -https://github.com/microsoft/nniscikit-nni is a helper tool (and a package) that :generates the configuration (config.yaml & search-space.json) required for NNIautomatically builds the scikit-learn pipelines based on your specification and becomes an experiment/trial code for Microsoft NNI to run.What value does this tool add to Microsoft NNI ?First note that this tool is specifically written to only help with scikit-learn pipelines and to tune classification algorithms. In near future,
I would add the support for regression algorithms as well.Now when you use Microsoft NNI you need to specify (at minimum) 3 files :A search space (json) file that contains the parameters that you want to search/tune.Your code/experiment. In your experiment code, you perform these tasks in sequence :Request for the parameters from NNI serverCreate your model using these parametersFit your modelScore your modelReport the score to NNI server.a configuration file where you specify the tuner, which mode to use to run, path to your code file and search space file.scikit-nnieliminiates the second step i.e. it builds the scikit pipelines, request NNI server for parameters and also report back the score of your model.  It also simplifies (in IMHO) the input
specification by only requiring one file instead of 3.Sounds interesting ? Then read the documentation below, install scikit-nni, and more importantly provide feedback if it does not work for you and/or you think it can be improved.FeaturesHyperparameters search for scikit-learn pipelines using Microsoft NNINo code required to define the pipelinesBuilt-in datasource reader for reading npz files for classificationSupport for using custom datasource readerSingle configuration file to define NNI configuration and search spaceI plan to add more datasource readers (e.g. CSV, libSVM format files etc). Contributions are always welcome !UsageStep 1 - Write a specification fileThe specification file is essentially a YAML file but with extension.nni.ymlThere are 4 parts (sections) in the configuration file.Datasource SectionThis is where you will specify the (python) callable thatsknniwould be invoking to get the training
and test dataset.The callablemustreturn two values where each value is atupleof two items. The first tuple
consists of training data(X_train, y_train)and the second tuple consists of test data(X_test, y_test).An example callable would look like this:import numpy as np

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

class ACustomDataSource(object):
    def __init__(self):
        pass

    def __call__(self, test_size:float=0.25):
        digits = load_digits()
        X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, random_state=99, test_size=test_size)

        return (X_train, y_train), (X_test, y_test)In the above example, the callable generates the train and test dataset. The callable can even have paramaters for e.g. in this
example you could optionally pass the fraction of data to be used for testing.Now let’s see how you would add it in the specification file.# Datasource is how you specify which callable# sknni will invoke to get the datadataSource:reader:yourmodule.ACustomDataSourceparams:test_size:0.30Make sure that during the exeuction of the experiment your datasource (i.e. in this caseyourmodule.ACustomDataSource)
is available in the PYTHONPATH.Here is an additional example showing the usage of a built-in datasource readerdataSource:reader:sknni.datasource.NpzClassificationSourceparams:dir_path:/Users/ksachdeva/Desktop/Dev/myoss/scikit-nni/examples/data/multiclass-classificationNpzClassificationSourceexpects that atdir_pathyou have two folders - train and test. In each folder are the files
named as 0.npz, 1.npz etc. Every file contains that features for that corresponding class.The repository contains two such datasources to do binary and multiclass classifications.Pipline definition SectionBelow is an example of this type of section. You simply specify the list of steps of your scikit-learn Pipeline.Note - The sequence of steps is very important.What youMUSTensure is that the full qualified name of your scikit-learn preprocessors, transformers and
estimators is correctly specified & spelled.sknniuses reflection and introspection to create the instances of these components
so if you have a typo in the names and/or they are not available in your PYTHONPATH you will get an error at experiment execution time.sklearnPipeline:name:normalizer_svcsteps:normalizer:type:sklearn.preprocessing.NormalizerclassArgs:norm:l2svc:type:sklearn.svm.SVCIn above example, there are 2 steps. The first step is to normalize the data and the second step is train a classifier using Support
Vector Machine.Search Space SectionThis section corresponds to the search space for your hyperparameters. When you use`nnictrl`this is typically
specified in search-space.json file.Seehttps://nni.readthedocs.io/en/latest/Tutorial/SearchSpaceSpec.htmlto learn more about the search space syntax.Here are the important things to note about this section -The syntax is the same (except we are using YAML here instead of JSON) for specifiying parameter types and ranges.YouMUSTspecifiy the parameters corresponding to the step in your scikit pipeline.YouMUSTuse the names of the parameters that aresame asthe ones accepted by the constructors of scikit-learn components (i.e. preprocessors, estimators etc).Below is an example of this type of section.nniConfigSearchSpace:-normalizer:norm:_type:choice_value:[l2,l1]-svc:C:_type:uniform_value:[0.1,0.0]kernel:_type:choice_value:[linear,rbf,poly,sigmoid]degree:_type:choice_value:[1,2,3,4]gamma:_type:uniform_value:[0.01,0.1]coef0:_type:uniform_value:[0.01,0.1]Note thatsklearn.svm.SVCtakes C, kernel, degree, gamman and coef0 is the paramaters and hence we have used here
the same names (keys) in the search space specification. You can add as many or as little parameters to search for.NNI Config SectionThis is the simplest of all sections as there is nothing new here from sknni perspective. You just copy-paste
here your NNI’s config.yaml here. You do not have to specifycodedirandcommandfield in thetrialsubsection as
this is added by the sknni in the generated configuration files.Seehttps://nni.readthedocs.io/en/latest/Tutorial/ExperimentConfig.htmlHere is an example of this type of section.# This is exactly same as the one that of NNI# except that you do not have to specify the command# and code fields. They are automatically added by the sknni generatornniConfig:authorName:defaultexperimentName:example_sklearn-classificationtrialConcurrency:1maxExecDuration:1hmaxTrialNum:100trainingServicePlatform:localuseAnnotation:falsetuner:builtinTunerName:TPEclassArgs:optimize_mode:maximizetrial:gpuNum:0You can look at the various examples in the repository to learn how to define your own specification file.Step 2 - Generate your experimentsknnigenerate-experiment--specexample/basic_svc.nni.yml--output-direxperimentsAbove command will create a directory experiments/svc-classification with the following filesThe original specification file i.e. basic_svc.nni.yml (used during experiment run as well)Generated Microsoft NNI’s config.ymlGenerated Microsoft NNI’s search-space.jsonNote - there is no python file as typically shown in the examples of Microsoft NNI as the command
in ends up invokingsknnientry point when the experiment is run.Step 3 - Run your experimentThis is same as runningnnitctlnnictlcreate--configexperiments/svc-classification/config.ymlTroubleshootingMy trials are failing what is wrong ?Your trial could fail for many reasons -Bug in your DataSource code resulting the exception/errorWrong inputs to your (or built-in) DataSources resulting in exception/errorYour DataSource (python callable) could not be foundHere is what I would recommend -Test your DataSource codeThe webui does not always display all the errors/logs so look at the log of your trials and more specifically stderr filecat$HOME/nni/experiments/<YOUR_EXPERIMENT_ID>/trials/<TRIAL_ID>/stderrcat$HOME/nni/experiments/<YOUR_EXPERIMENT_ID>/trials/<TRIAL_ID>/trial.logCreditsThis package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.History0.1.1 (2019-10-20)First release on PyPI.