{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiple-Objective Optimization Auto-Tuning" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import paradance as para" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the offline dataset used for Bayesian automatic tuning.\n", "\n", "`FILE_PATH` is the path where the training data is stored. `FILE_NAME` is the filename." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loader = para.CSVLoader(\n", " file_path=FILE_PATH,\n", " file_name=FILE_NAME,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, three different targets can be fused like this:\n", "\n", "```\n", "formula=\"targets[0] - 2 * targets[1] + 100 * max(0, targets[2])\". \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Select the features to be used for hyperparameter search" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "selected_columns=['pxtr_0', 'pxtr_1', 'pxtr_2', 'pxtr_3', 'pxtr_4']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We should construct a `Calculator`, to calculate the composite score based on the selected features. \n", "\n", "The calculation method for the composite score can be chosen as either `sum` or `product`, corresponding to the **additive** and **multiplicative** forms of the formulas.\n", "\n", "We can also use the `free_style` type of equation, which supports any form of `eval()` expression equation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cal = para.Calculator(\n", " df=loader.df, \n", " selected_columns=selected_columns, \n", " equation_type=\"product\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Determine the Bayesian search space: whether to search for the power term; whether to search for the first-order term of the features." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "power = True\n", "first_order = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, construct the optimization target based on the above settings, and determine the direction of optimization, whether to maximize or minimize the fusion target expression.\n", "\n", "The optimization process will be recorded in the `LOG_FILE` file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ob = para.MultipleObjective(\n", " direction=\"minimize\",\n", " weights_num=len(selected_columns),\n", " formula=formula,\n", " power=power,\n", " first_order=first_order,\n", " log_file=LOG_FILE,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add the appropriate optimizers to the optimization targets, such as the \"woauc\"/\"auc\" optimizer in this case. \n", "\n", "`target_column` is the sub-target that this optimizer is used to optimize, such as \"watch_t\"/\"follow\"/\"click\"." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ob.add_calculator(\n", " calculator=cal,\n", " flag='woauc',\n", " target_column='watch_time'\n", ")\n", "\n", "ob.add_calculator(\n", " calculator=cal,\n", " flag='auc',\n", " target_column='follow'\n", ")\n", "\n", "ob.add_calculator(\n", " calculator=cal,\n", " flag='auc',\n", " target_column='click'\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Begin optimization, and save the tuning log. `n_trials` represents the number of rounds for hyperparameter search." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ob.optimize(n_trials=600)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }