|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | +# - Generated by tools/entrypoint_compiler.py: do not edit by hand |
| 6 | +""" |
| 7 | +LinearSvmBinaryClassifier |
| 8 | +""" |
| 9 | + |
| 10 | +__all__ = ["LinearSvmBinaryClassifier"] |
| 11 | + |
| 12 | + |
| 13 | +from ...entrypoints.trainers_linearsvmbinaryclassifier import \ |
| 14 | + trainers_linearsvmbinaryclassifier |
| 15 | +from ...utils.utils import trace |
| 16 | +from ..base_pipeline_item import BasePipelineItem, DefaultSignatureWithRoles |
| 17 | + |
| 18 | + |
| 19 | +class LinearSvmBinaryClassifier( |
| 20 | + BasePipelineItem, |
| 21 | + DefaultSignatureWithRoles): |
| 22 | + """ |
| 23 | +
|
| 24 | + Linear Support Vector Machine (SVM) Binary Classifier |
| 25 | +
|
| 26 | + .. remarks:: |
| 27 | + Linear SVM implements an algorithm that finds a hyperplane in the |
| 28 | + feature space for binary classification, by solving an SVM problem. |
| 29 | + For instance, with feature values *f_0, f_1,..., f_{D-1}*, the |
| 30 | + prediction is given by determining what side of the hyperplane the |
| 31 | + point falls into. That is the same as the sign of the feautures' |
| 32 | + weighted sum, i.e. *\sum_{i = 0}^{D-1} \left(w_i * f_i \right) + b*, |
| 33 | + where *w_0, w_1,..., w_{D-1}* are the weights computed by the |
| 34 | + algorithm, and *b* is the bias computed by the algorithm. |
| 35 | +
|
| 36 | + This algorithm implemented is the PEGASOS method, which alternates |
| 37 | + between stochastic gradient descent steps and projection steps, |
| 38 | + introduced by Shalev-Shwartz, Singer and Srebro. |
| 39 | +
|
| 40 | +
|
| 41 | + **Reference** |
| 42 | +
|
| 43 | + `Wikipedia entry for Support Vector Machine |
| 44 | + <https://en.wikipedia.org/wiki/Support-vector_machine>`_ |
| 45 | +
|
| 46 | + `Pegasos: Primal Estimated sub-GrAdient SOlver for SVM |
| 47 | + <https://ttic.uchicago.edu/~shai/papers/ShalevSiSr07.pdf>`_ |
| 48 | +
|
| 49 | +
|
| 50 | + :param normalize: Specifies the type of automatic normalization used: |
| 51 | +
|
| 52 | + * ``"Auto"``: if normalization is needed, it is performed |
| 53 | + automatically. This is the default choice. |
| 54 | + * ``"No"``: no normalization is performed. |
| 55 | + * ``"Yes"``: normalization is performed. |
| 56 | + * ``"Warn"``: if normalization is needed, a warning |
| 57 | + message is displayed, but normalization is not performed. |
| 58 | +
|
| 59 | + Normalization rescales disparate data ranges to a standard scale. |
| 60 | + Feature |
| 61 | + scaling ensures the distances between data points are proportional |
| 62 | + and |
| 63 | + enables various optimization methods such as gradient descent to |
| 64 | + converge |
| 65 | + much faster. If normalization is performed, a ``MinMax`` normalizer |
| 66 | + is |
| 67 | + used. It normalizes values in an interval [a, b] where ``-1 <= a <= |
| 68 | + 0`` |
| 69 | + and ``0 <= b <= 1`` and ``b - a = 1``. This normalizer preserves |
| 70 | + sparsity by mapping zero to zero. |
| 71 | +
|
| 72 | + :param caching: Whether trainer should cache input training data. |
| 73 | +
|
| 74 | + :param lambda_: Regularizer constant. |
| 75 | +
|
| 76 | + :param perform_projection: Perform projection to unit-ball? Typically used |
| 77 | + with batch size > 1. |
| 78 | +
|
| 79 | + :param number_of_iterations: Number of iterations. |
| 80 | +
|
| 81 | + :param initial_weights_diameter: Sets the initial weights diameter that |
| 82 | + specifies the range from which values are drawn for the initial |
| 83 | + weights. These weights are initialized randomly from within this range. |
| 84 | + For example, if the diameter is specified to be ``d``, then the weights |
| 85 | + are uniformly distributed between ``-d/2`` and ``d/2``. The default |
| 86 | + value is ``0``, which specifies that all the weights are set to zero. |
| 87 | +
|
| 88 | + :param no_bias: No bias. |
| 89 | +
|
| 90 | + :param initial_weights: Initial Weights and bias, comma-separated. |
| 91 | +
|
| 92 | + :param shuffle: Whether to shuffle for each training iteration. |
| 93 | +
|
| 94 | + :param batch_size: Batch size. |
| 95 | +
|
| 96 | + :param params: Additional arguments sent to compute engine. |
| 97 | +
|
| 98 | + .. index:: models, classification, svm |
| 99 | +
|
| 100 | + Example: |
| 101 | + .. literalinclude:: /../nimbusml/examples/LinearSvmBinaryClassifier.py |
| 102 | + :language: python |
| 103 | + """ |
| 104 | + |
| 105 | + @trace |
| 106 | + def __init__( |
| 107 | + self, |
| 108 | + normalize='Auto', |
| 109 | + caching='Auto', |
| 110 | + lambda_=0.001, |
| 111 | + perform_projection=False, |
| 112 | + number_of_iterations=1, |
| 113 | + initial_weights_diameter=0.0, |
| 114 | + no_bias=False, |
| 115 | + initial_weights=None, |
| 116 | + shuffle=True, |
| 117 | + batch_size=1, |
| 118 | + **params): |
| 119 | + BasePipelineItem.__init__( |
| 120 | + self, type='classifier', **params) |
| 121 | + |
| 122 | + self.normalize = normalize |
| 123 | + self.caching = caching |
| 124 | + self.lambda_ = lambda_ |
| 125 | + self.perform_projection = perform_projection |
| 126 | + self.number_of_iterations = number_of_iterations |
| 127 | + self.initial_weights_diameter = initial_weights_diameter |
| 128 | + self.no_bias = no_bias |
| 129 | + self.initial_weights = initial_weights |
| 130 | + self.shuffle = shuffle |
| 131 | + self.batch_size = batch_size |
| 132 | + |
| 133 | + @property |
| 134 | + def _entrypoint(self): |
| 135 | + return trainers_linearsvmbinaryclassifier |
| 136 | + |
| 137 | + @trace |
| 138 | + def _get_node(self, **all_args): |
| 139 | + algo_args = dict( |
| 140 | + feature_column_name=self._getattr_role( |
| 141 | + 'feature_column_name', |
| 142 | + all_args), |
| 143 | + label_column_name=self._getattr_role( |
| 144 | + 'label_column_name', |
| 145 | + all_args), |
| 146 | + example_weight_column_name=self._getattr_role( |
| 147 | + 'example_weight_column_name', |
| 148 | + all_args), |
| 149 | + normalize_features=self.normalize, |
| 150 | + caching=self.caching, |
| 151 | + lambda_=self.lambda_, |
| 152 | + perform_projection=self.perform_projection, |
| 153 | + number_of_iterations=self.number_of_iterations, |
| 154 | + initial_weights_diameter=self.initial_weights_diameter, |
| 155 | + no_bias=self.no_bias, |
| 156 | + initial_weights=self.initial_weights, |
| 157 | + shuffle=self.shuffle, |
| 158 | + batch_size=self.batch_size) |
| 159 | + |
| 160 | + all_args.update(algo_args) |
| 161 | + return self._entrypoint(**all_args) |
0 commit comments