-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sparse lr/sklearn intent classifier
- Loading branch information
Showing
13 changed files
with
621 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# SparseLogisticRegressionIntentClassifier | ||
|
||
This intent classifier is based on the Logistic Regression Classifier from | ||
[sklearn](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html). | ||
This classifier only looks at sparse features extracted from the Rasa NLU | ||
feature pipeline and is a faster alternative to neural models like | ||
[DIET](https://rasa.com/docs/rasa/components#dietclassifier-2). This model | ||
requires that there be some sparse featurizers in your pipeleine. If you config | ||
only has dense features it will throw an exception. | ||
|
||
## Configurable Variables | ||
|
||
We classifier supports the same parameters as those that are listed in the [sklearn documentation](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html). The only difference is: | ||
- there is no `warm_start option` | ||
- the default `class_weight` is "balanced" | ||
|
||
## Base Usage | ||
|
||
The configuration file below demonstrates how you might use the this component. | ||
In this example we are extracting sparse features with two | ||
CountVectorsFeaturizer instances, the first of which produces sparse | ||
bag-of-words features, and the second which produces sparse | ||
bags-of-character-ngram features. | ||
|
||
Note that in the following example, setting the `class_weight` parameter to None | ||
explicitly does have an effect because our default value for this paramter is "balanced". | ||
|
||
```yaml | ||
language: en | ||
|
||
pipeline: | ||
- name: WhitespaceTokenizer | ||
- name: CountVectorsFeaturizer | ||
- name: CountVectorsFeaturizer | ||
analyzer: char_wb | ||
min_ngram: 1 | ||
max_ngram: 4 | ||
- name: rasa_nlu_examples.classifiers.SparseLogisticRegressionIntentClassifier | ||
class_weight: None | ||
``` | ||
Unlike [DIET](https://rasa.com/docs/rasa/components#dietclassifier-2), this | ||
classifier only predicts intents. If you also need entity extraction, you will | ||
have to add a separate entity extractor to your config. Below is an example | ||
where we have included the CRFEntityExtractor to extract entities. | ||
```yaml | ||
language: en | ||
|
||
pipeline: | ||
- name: WhitespaceTokenizer | ||
- name: LexicalSyntacticFeaturizer | ||
- name: CountVectorsFeaturizer | ||
- name: CountVectorsFeaturizer | ||
analyzer: char_wb | ||
min_ngram: 1 | ||
max_ngram: 4 | ||
- name: rasa_nlu_examples.classifiers.SparseLogisticRegressionIntentClassifier | ||
- name: CRFEntityExtractor | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
from .sparse_naive_bayes_intent_classifier import SparseNaiveBayesIntentClassifier | ||
from .sparse_logistic_regression_intent_classifier import ( | ||
SparseLogisticRegressionIntentClassifier, | ||
) | ||
|
||
__all__ = ["SparseNaiveBayesIntentClassifier"] | ||
__all__ = [ | ||
"SparseNaiveBayesIntentClassifier", | ||
"SparseLogisticRegressionIntentClassifier", | ||
] |
42 changes: 42 additions & 0 deletions
42
rasa_nlu_examples/classifiers/sparse_logistic_regression_intent_classifier.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any | ||
|
||
import sklearn | ||
from sklearn.linear_model import LogisticRegression | ||
|
||
from rasa_nlu_examples.classifiers.sparse_sklearn_intent_classifier import ( | ||
SparseSklearnIntentClassifier, | ||
) | ||
|
||
|
||
class SparseLogisticRegressionIntentClassifier(SparseSklearnIntentClassifier): | ||
r"""A logistic regression classifier using the sklearn framework with sparse features.""" | ||
|
||
defaults = { | ||
# The following parameters and defaults are the same as the ones used by the | ||
# current scikit-learn version (0.24.2). For some nice explanations on what | ||
# these parameters and their defaults so, have a look at the scikit-learn docs: | ||
# https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html | ||
"C": 1.0, | ||
"class_weight": "balanced", | ||
"dual": False, | ||
"fit_intercept": True, | ||
"intercept_scaling": 1, | ||
"l1_ratio": None, | ||
"max_iter": 100, | ||
"multi_class": "auto", | ||
"n_jobs": None, | ||
"penalty": "l2", | ||
"random_state": None, | ||
"solver": "lbfgs", | ||
"tol": 0.0001, | ||
"verbose": 0, | ||
} | ||
|
||
def create_sklearn_classifier(self, **kwargs: Any) -> sklearn.base.ClassifierMixin: | ||
r"""Lazily imports the required sklearn classifier class and creates and | ||
instantiates the sklearn classifier using all the given keyword arguments. | ||
:param **kwargs: see defaults dictionary | ||
""" | ||
|
||
return LogisticRegression(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.