<jupyter_start><jupyter_text>Keras: Classify Multi-Label ![iris](../images/iris.png) Reference this great blog for machine learning cookbooks: [MachineLearningMastery.com "Multi-Label Classification"](https://machinelearningmastery.com/multi-label-classification-with-deep-learning/).<jupyter_code>import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import History

from sklearn.preprocessing import OneHotEncoder, PowerTransformer

import aiqc
from aiqc import datum<jupyter_output><empty_output><jupyter_text>---  Example Data Reference [Example Datasets](example_datasets.ipynb) for more information.<jupyter_code>df = datum.to_pandas('iris.tsv')
df.head()
df.dtypes<jupyter_output><empty_output><jupyter_text>---  a) High-Level API Reference [High-Level API Docs](api_high_level.ipynb) for more information including how to work with non-tabular data.<jupyter_code>splitset = aiqc.Pipeline.Tabular.make(
    dataFrame_or_filePath = df
    , label_column = 'species'
    , size_test = 0.22
    , size_validation = 0.12
    , label_encoder = OneHotEncoder(sparse=False)
    , feature_encoders = [{
        "sklearn_preprocess": PowerTransformer(method='box-cox', copy=False)
        , "dtypes": ['float64']
    }]
    
    , dtype = None
    , features_excluded = None
    , fold_count = None
    , bin_count = None
)
def fn_build(features_shape, label_shape, **hp):
    model = Sequential()
    model.add(Dense(units=features_shape[0], activation='relu', kernel_initializer='he_uniform'))
    model.add(Dropout(0.2))
    model.add(Dense(units=hp['neuron_count'], activation='relu', kernel_initializer='he_uniform'))
    model.add(Dense(units=label_shape[0], activation='softmax'))
    return model
def fn_train(model, loser, optimizer, samples_train, samples_evaluate, **hp):
    model.compile(
        loss = loser
        , optimizer = optimizer
        , metrics = ['accuracy']
    )
    model.fit(
        samples_train["features"]
        , samples_train["labels"]
        , validation_data = (
            samples_evaluate["features"]
            , samples_evaluate["labels"]
        )
        , verbose = 0
        , batch_size = hp['batch_size']
        , epochs = hp['epoch_count']
        , callbacks=[History()]
    )
    return model
hyperparameters = {
    "neuron_count": [9, 12]
    , "batch_size": [3]
    , "learning_rate": [0.03, 0.05]
    , "epoch_count": [30, 60]
}
queue = aiqc.Experiment.make(
    library = "keras"
    , analysis_type = "classification_multi"
    , fn_build = fn_build
    , fn_train = fn_train
    , splitset_id = splitset.id
    , repeat_count = 1
    , hide_test = False
    , hyperparameters = hyperparameters
    
    , fn_lose = None #automated
    , fn_optimize = None #automated
    , fn_predict = None #automated
    , foldset_id = None
)
queue.run_jobs()<jupyter_output>🔮 Training Models 🔮: 100%|██████████████████████████████████████████| 8/8 [00:37<00:00,  4.68s/it]<jupyter_text>For more information on visualization of performance metrics, reference the [Visualization & Metrics](visualization.html) documentation. ---  b) Low-Level API Reference [Low-Level API Docs](api_high_level.ipynb) for more information including how to work with non-tabular data, and defining an optimizer.<jupyter_code>dataset = aiqc.Dataset.Tabular.from_pandas(df)
label_column = 'species'
label = dataset.make_label(columns=[label_column])
labelcoder = label.make_labelcoder(
    sklearn_preprocess = OneHotEncoder(sparse=False)
)
feature = dataset.make_feature(exclude_columns=[label_column])
encoderset = feature.make_encoderset()
featurecoder_0 = encoderset.make_featurecoder(
    sklearn_preprocess = PowerTransformer(method='yeo-johnson', copy=False)
    , dtypes = ['float64']
)
splitset = aiqc.Splitset.make(
    feature_ids = [feature.id]
    , label_id = label.id
    , size_test = 0.22
    , size_validation = 0.12
)
def fn_build(features_shape, label_shape, **hp):
    model = Sequential()
    model.add(Dense(units=features_shape[0], activation='relu', kernel_initializer='he_uniform'))
    model.add(Dropout(0.2))
    model.add(Dense(units=hp['neuron_count'], activation='relu', kernel_initializer='he_uniform'))
    model.add(Dense(units=label_shape[0], activation='softmax'))
    return model<jupyter_output><empty_output><jupyter_text>Optional, just included to show users how to specify their own optimizer.<jupyter_code>def fn_optimize(**hp):
    optimizer = keras.optimizers.Adamax()
    return optimizer
def fn_train(model, loser, optimizer, samples_train, samples_evaluate, **hp):
    model.compile(
        loss = loser
        , optimizer = optimizer
        , metrics = ['accuracy']
    )
    model.fit(
        samples_train["features"]
        , samples_train["labels"]
        , validation_data = (
            samples_evaluate["features"]
            , samples_evaluate["labels"]
        )
        , verbose = 0
        , batch_size = hp['batch_size']
        , epochs = hp['epoch_count']
        , callbacks=[History()]
    )
    return model
algorithm = aiqc.Algorithm.make(
    library = "keras"
    , analysis_type = "classification_multi"
    , fn_build = fn_build
    , fn_train = fn_train
)
hyperparameters = {
    "neuron_count": [9, 12]
    , "batch_size": [3]
    , "learning_rate": [0.03, 0.05]
    , "epoch_count": [30, 60]
}
hyperparamset = algorithm.make_hyperparamset(
    hyperparameters = hyperparameters
)
queue = algorithm.make_queue(
    splitset_id = splitset.id
    , hyperparamset_id = hyperparamset.id
    , repeat_count = 1
)
queue.run_jobs()<jupyter_output>🔮 Training Models 🔮: 100%|██████████████████████████████████████████| 8/8 [00:35<00:00,  4.49s/it]
StarcoderdataJupyterStructuredCleanDedup