Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append ECG neural network tune example #199

Merged
merged 10 commits into from
Dec 4, 2024
Prev Previous commit
Next Next commit
Append two test examples
  • Loading branch information
Audice committed Nov 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit a9d24badeb183a4f99773ce42c72c705f8c37997
133 changes: 41 additions & 92 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
@@ -485,49 +485,58 @@ The blue dots on the graph represent the points of exploratory trials,
the red dot marks the found optimum corresponding to the hyperparameters
at which the f1-score reaches its maximum.

Пример работы с фреймворком при варьировании двух непрерывных и одного дискретного параметра
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Ранее, при поиске оптимального набора параметров, был рассмотрен пример с варьированием двух непрерывных параметров метода SVC.
В качестве целевого дискретного параметра выберем тип ядра алгоритма: kernel. Данный категориальный параметр принимает одно из 5 значений: linear, poly, rbf, sigmoid, precomputed.
Однако, в случае рассмотрения параметров C и gamma, доступны лишь три из них: poly, rbf, sigmoid.
An example of working with the framework when varying two continuous and one discrete parameters
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Previously, when searching for the optimal set of parameters, an example was considered
with varying two continuous parameters of the SVC method.
As a target discrete parameter, we will select the type of algorithm kernel: **kernel**.
This categorical parameter takes one of 5 values: linear, poly, rbf, sigmoid, precomputed.
However, when considering the parameters C and gamma, only three of them are available:
poly, rbf, sigmoid.

Let's consider the behavior of the quality metric f1-score on each specified core separately.
Let's set the following areas for parameters:

Рассмотрим поведение метрики качества F1 score на каждом указанном ядре в отдельности. Установим следующие области для параметров:

* Параметр регуляризации **C**: [10\ :sup:`1`, 10\ :sup:`10`]
* Коэффициент ядра **gamma**: [10\ :sup:`-9`, 10\ :sup:`-6.7`]
#. regularization parameter **C**: [10\ :sup:`1`, 10\ :sup:`10`];
#. kernel coefficient **gamma**: [10\ :sup:`-9`, 10\ :sup:`-6.7`].

При запуске на равномерной сетке размера 40 на 40 точек с типом ядра rbf, видно, что график имеет несколько локальных минимумов.
При таком поиске найдено оптимальное значение метрики, равное -0.949421.
When running on a uniform grid of 40 by 40 points with the rbf kernel type,
it is clear that the graph has several local minima.
With this search, the optimal metric value was found equal to -0.949421.

.. figure:: images/rbf_kernel.JPG
:width: 500
:align: center

График значений метрики F1 score на указанной области с ядром kernel = rbf
Graph of f1-score metric values on a specified area with kernel = rbf

При смене типа ядра с rbf на sigmoid график претерпел изменения, однако по прежнему имеет многоэкстримальную природу.
Минимальное значение метрики на указанной сетке равно -0.93832.
When changing the kernel type from rbf to sigmoid, the graph changed,
but still has a multi-extreme nature.
The minimum metric value on the specified grid is -0.93832.

.. figure:: images/sigmoid_kernel.JPG
:width: 500
:align: center

График значений метрики F1 score на указанной области с ядром kernel = sigmoid
Graph of f1-score metric values on a specified area with kernel = sigmoid

При исследовании поведения графика метрики на равномерной сетке с ядром poly, наблюдается слабая многоэкстримальность.
При этом минимум значения метрики найден не был, и метод остановился на значении -0.9337763.
When studying the behavior of the metric graph on a uniform grid with a poly kernel,
weak multiextremality is observed.
However, the minimum value of the metric was not found, and the method stopped at the value -0.9337763.

.. figure:: images/poly_kernel.JPG
:width: 500
:align: center

График значений метрики F1 score на указанной области с ядром kernel = poly
Graph of f1-score metric values on a specified area with kernel = poly

При варьировании типа ядра для алгоритма SVC, наблюдается разное значение метрики. Таким образом, появляется возможность исследовать исследовать поведение метрики срадствами фреймворка iOpt,
варьируя параметры C, gamma и kernel в указанной области.
When varying the kernel type for the SVC algorithm, different metric values are observed.
Thus, it becomes possible to explore the behavior of the metric using the iOpt framework,
varying the parameters **C**, **gamma** and **kernel** in the specified area.

Подготовим проблему для решения поставленной задачи. Для этого, как и в двумерном случае необходимо объявить класс,
являющийся наследником класса **Problem** с абстрактным методом **Calculate**. Код данного класса представлен ниже:
Let's prepare a problem to solve. To do this, as in the two-dimensional case,
it is necessary to declare a class that is a descendant of the **Problem** class
with the abstract **Calculate** method. The code for this class is presented below:

.. code-block::

@@ -571,81 +580,21 @@ The SVC_3D class accepts the following constructor parameters:
#. **kernel_coefficient_bound** – maximum and minimum values for **gamma** as a dictionary.
#. **kernel_type** – the kernel type used in the SVC algorithm.

Чтобы запустить процесс оптимизации, необходимо создать объект класса **SVC_3D**, а также объект класса **Solver**
с переданным объектом целевой функции.
The **Calculate** method implements the logic for calculating the objective function
at the **Point** point. It is worth noting that the **Point** contains two real parameters
and one discrete one, which are used to train the SVC classifier.
To obtain the value of the optimized function, the average f1-score value is calculated
based on cross-validation.

To start the optimization process, you need to create an object of the **SVC_3D** class,
as well as an object of the **Solver** class with the passed target function object.

* Параметр регуляризации **C**: [10\ :sup:`1`, 10\ :sup:`10`]
* Коэффициент ядра **gamma**: [10\ :sup:`-9`, 10\ :sup:`-6.7`]
* Тип ядра **kernel**: [rbf, sigmoid, poly]

Для выполнения подготовленной проблемы разработан скрипт, в котором устанавливается указанная ранее область для поиска
оптимального сочетания гиперпараметр. В рамках скрипта производится загрузка данных, на которых
анализируется работа алгоритма SVC. В скрипт добавлены слушатели (Listener), которые предоставляют дополнительную
информацию о процессе поиска. Так, **ConsoleOutputListener** позволяет отслеживать процесс
поиска оптимального набора гиперпараметров, визуализируя в консоле точки испытания и значения метрики в данной точке.
**StaticDiscreteListener**, с флагом mode='analysis' предоставляет в графическом виде суммарную статистику
по исследованию, в которой отображено:

* График зависимости значения целевой функции с ростом числа испытаний
* Значение метрики на каждой итерации в зависимости от выбранного значения дискретного параметра
* График минимальных значений целевой функции на каждой итерации
* Значения целевой функции в при использовании конкретного значения дискретного параметра

.. figure:: images/statictic.JPG
:width: 500
:align: center

Статистика по поиску оптимального сочетания параметров средствами iOpt.

**StaticDiscreteListener**, с флагом mode='bestcombination' визуализирует линии уровня, которые соответствуют графику с тем значением
дискретного параметра, на котором был найден оптимум для метрики. Синими точками на графике обозначены точки испытаний решателя для лучшего
значения дискретного параметра, а серым цветом - все остальные. Красная точка - найденный минимум значения целевой функции.

.. figure:: images/best_solve.JPG
:width: 500
:align: center

Линии уровня графика, соответствующего функции с "лучшим" значением дискретного параметра

.. code-block::

from iOpt.output_system.listeners.console_outputers import ConsoleOutputListener
from iOpt.output_system.listeners.static_painters import StaticDiscreteListener
from sklearn.datasets import load_breast_cancer
from iOpt.solver import Solver
from iOpt.solver_parametrs import SolverParameters
from examples.Machine_learning.SVC._3D.Problem import SVC_3D
from sklearn.utils import shuffle

def load_breast_cancer_data():
dataset = load_breast_cancer()
x_raw, y_raw = dataset['data'], dataset['target']
inputs, outputs = shuffle(x_raw, y_raw ^ 1, random_state=42)
return inputs, outputs

if __name__ == "__main__":
x, y = load_breast_cancer_data()
regularization_value_bound = {'low': 1, 'up': 10}
kernel_coefficient_bound = {'low': -9, 'up': -6.7}
kernel_type = {'kernel': ['rbf', 'sigmoid', 'poly']}
problem = SVC_3D.SVC_3D(x, y, regularization_value_bound, kernel_coefficient_bound, kernel_type)
method_params = SolverParameters(itersLimit=400)
solver = Solver(problem, parameters=method_params)
apl = StaticDiscreteListener("experiment1.png", mode='analysis')
solver.AddListener(apl)
apl = StaticDiscreteListener("experiment2.png", mode='bestcombination', calc='interpolation', mrkrs=4)
solver.AddListener(apl)
cfol = ConsoleOutputListener(mode='full')
solver.AddListener(cfol)
solver_info = solver.Solve()

Код скрипта для запуска процедуры поиска оптимального сочетания гиперпараметров.
When searching for the optimal combination of discrete and continuous parameters,
the following areas were considered:

В ходе работы фреймворка был найден минимум показателя f1-score, равный **-0.95157487**. Количество
итераций решателя **iterLimits**\=400.
#. regularization parameter **C**: [10\ :sup:`1`, 10\ :sup:`10`];
#. kernel coefficient **gamma**: [10\ :sup:`-9`, 10\ :sup:`-6.7`];
#. kernel type **kernel**: [rbf, sigmoid, poly].

To perform the prepared problem, a script has been developed in which the previously specified area
is set to search for the optimal combination of hyperparameters. As part of the script, data is loaded

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from iOpt.output_system.listeners.static_painters import StaticPainterNDListener
from iOpt.output_system.listeners.animate_painters import AnimatePainterNDListener
from iOpt.output_system.listeners.console_outputers import ConsoleOutputListener

from iOpt.solver import Solver
from iOpt.solver_parametrs import SolverParameters
from examples.Machine_learning.SVC._2D.Problems import SVC_2D_Transformators_State
from sklearn.utils import shuffle
import numpy as np
import pandas as pd
import csv

def factory_dataset():
x = []
y = []
with open(r"../Datasets/transformator_state.csv") as rrrr_file:
file_reader = csv.reader(rrrr_file, delimiter=",")
for row in file_reader:
x_row = []
for i in range(len(row)-1):
x_row.append(row[i])
x.append(x_row)
y.append(row[len(row)-1])
return shuffle(np.array(x), np.array(y), random_state=42)


if __name__ == "__main__":
X, Y = factory_dataset()
regularization_value_bound = {'low': 5, 'up': 9}
kernel_coefficient_bound = {'low': -3, 'up': 1}
problem = SVC_2D_Transformators_State.SVC_2D_Transformators_State(X, Y, regularization_value_bound, kernel_coefficient_bound)
method_params = SolverParameters(r=np.double(2.0), iters_limit=100)
solver = Solver(problem, parameters=method_params)
#apl = AnimatePainterNDListener("svc2d_transformator_state_anim.png", "output", vars_indxs=[0, 1])
#solver.add_listener(apl)
#spl = StaticPainterNDListener("svc2d_transformator_state_stat.png", "output", vars_indxs=[0, 1], mode="surface", calc="interpolation")
#solver.add_listener(spl)
cfol = ConsoleOutputListener(mode='full')
solver.add_listener(cfol)
solver_info = solver.solve()
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
from iOpt.trial import Point
from iOpt.trial import FunctionValue
from iOpt.problem import Problem
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
from typing import Dict
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

class SVC_2D_Transformators_State(Problem):
"""
Класс SVC_2D представляет возможность поиска оптимального набора гиперпараметров алгоритма
C-Support Vector Classification.
Найденные параметры являются оптимальными при варьировании параматра регуляризации
(Regularization parameter С) значения коэфицента ядра (gamma)
"""

def __init__(self, x_dataset: np.ndarray, y_dataset: np.ndarray,
regularization_bound: Dict[str, float],
kernel_coefficient_bound: Dict[str, float]):
"""
Конструктор класса SVC_2D

:param x_dataset: входные данные обучающе выборки метода SVC
:param y_dataset: выходные данные обучающе выборки метода SVC
:param kernel_coefficient_bound: Значение параметра регуляризации
:param regularization_bound: Границы изменения значений коэфицента ядра (low - нижняя граница, up - верхняя)
"""
super(SVC_2D_Transformators_State, self).__init__()
self.dimension = 2
self.number_of_float_variables = 2
self.number_of_discrete_variables = 0
self.number_of_objectives = 1
self.number_of_constraints = 0
if x_dataset.shape[0] != y_dataset.shape[0]:
raise ValueError('The input and output sample sizes do not match.')
self.x = x_dataset
self.y = y_dataset
self.float_variable_names = np.array(["Regularization parameter", "Kernel coefficient"], dtype=str)
self.lower_bound_of_float_variables = np.array([regularization_bound['low'], kernel_coefficient_bound['low']],
dtype=np.double)
self.upper_bound_of_float_variables = np.array([regularization_bound['up'], kernel_coefficient_bound['up']],
dtype=np.double)



def calculate(self, point: Point, function_value: FunctionValue) -> FunctionValue:
"""
Метод расчёта значения целевой функции в точке

:param point: Точка испытания
:param function_value: объект хранения значения целевой функции в точке
"""
cs, gammas = point.float_variables[0], point.float_variables[1]
clf = SVC(C=10 ** cs, gamma=10 ** gammas)
function_value.value = -cross_val_score(clf, self.x, self.y, scoring='accuracy').mean()
return function_value
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
from examples.Machine_learning.SVC._3D.Problem import SVC_3D
from sklearn.utils import shuffle


def load_breast_cancer_data():
dataset = load_breast_cancer()
x_raw, y_raw = dataset['data'], dataset['target']
@@ -18,12 +19,13 @@ def load_breast_cancer_data():
kernel_coefficient_bound = {'low': -9, 'up': -6.7}
kernel_type = {'kernel': ['rbf', 'sigmoid', 'poly']}
problem = SVC_3D.SVC_3D(x, y, regularization_value_bound, kernel_coefficient_bound, kernel_type)
method_params = SolverParameters(itersLimit=400)
method_params = SolverParameters(iters_limit=400)
solver = Solver(problem, parameters=method_params)
apl = StaticDiscreteListener("experiment1.png", mode='analysis')
solver.add_listener(apl)
apl = StaticDiscreteListener("experiment2.png", mode='bestcombination', calc='interpolation', mrkrs=4)
solver.add_listener(apl)
cfol = ConsoleOutputListener(mode='full')
solver.AddListener(cfol)
solver_info = solver.Solve()
solver.add_listener(cfol)
solver_info = solver.solve()

Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.