예제 #1
0
    def test_fit_predict(self, resource_loader):
        """Tests prediction after a fit"""
        config = ModelConfig(
            **{
                "model_type": "text",
                "example_type": QUERY_EXAMPLE_TYPE,
                "label_type": CLASS_LABEL_TYPE,
                "model_settings": {
                    "classifier_type": "logreg"
                },
                "params": {
                    "fit_intercept": True,
                    "C": 100
                },
                "features": {
                    "bag-of-words": {
                        "lengths": [1]
                    },
                    "freq": {
                        "bins": 5
                    },
                    "length": {},
                },
            })
        model = TextModel(config)
        examples = self.labeled_data.queries()
        labels = self.labeled_data.intents()
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model.predict([markup.load_query("hi").query]) == "greet"
        assert model.predict([markup.load_query("bye").query]) == "exit"
예제 #2
0
    def test_fit_predict(self, resource_loader):
        """Tests prediction after a fit"""
        config = ModelConfig(
            **{
                'model_type': 'text',
                'example_type': QUERY_EXAMPLE_TYPE,
                'label_type': CLASS_LABEL_TYPE,
                'model_settings': {
                    'classifier_type': 'logreg'
                },
                'params': {
                    'fit_intercept': True,
                    'C': 100
                },
                'features': {
                    'bag-of-words': {
                        'lengths': [1]
                    },
                    'freq': {
                        'bins': 5
                    },
                    'length': {}
                }
            })
        model = TextModel(config)
        examples = [q.query for q in self.labeled_data]
        labels = [q.intent for q in self.labeled_data]
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model.predict([markup.load_query('hi').query]) == 'greet'
        assert model.predict([markup.load_query('bye').query]) == 'exit'
예제 #3
0
    def test_extract_features(self, resource_loader):
        """Tests extracted features after a fit"""
        config = ModelConfig(
            **{
                "model_type": "text",
                "example_type": QUERY_EXAMPLE_TYPE,
                "label_type": CLASS_LABEL_TYPE,
                "model_settings": {"classifier_type": "logreg"},
                "params": {"fit_intercept": True, "C": 100},
                "features": {
                    "bag-of-words": {"lengths": [1]},
                },
            }
        )
        model = TextModel(config)
        examples = self.labeled_data.queries()
        labels = self.labeled_data.intents()
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        expected_features = {
            "bag_of_words|length:1|ngram:hi": 1,
            "bag_of_words|length:1|ngram:OOV": 1,
        }
        extracted_features = model.view_extracted_features(
            markup.load_query("hi there").query
        )
        assert extracted_features == expected_features
예제 #4
0
    def test_extract_features(self, resource_loader):
        """Tests extracted features after a fit"""
        config = ModelConfig(
            **{
                'model_type': 'text',
                'example_type': QUERY_EXAMPLE_TYPE,
                'label_type': CLASS_LABEL_TYPE,
                'model_settings': {
                    'classifier_type': 'logreg'
                },
                'params': {
                    'fit_intercept': True,
                    'C': 100
                },
                'features': {
                    'bag-of-words': {
                        'lengths': [1]
                    },
                }
            })
        model = TextModel(config)
        examples = [q.query for q in self.labeled_data]
        labels = [q.intent for q in self.labeled_data]
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        expected_features = {
            'bag_of_words|length:1|ngram:hi': 1,
            'bag_of_words|length:1|ngram:there': 1
        }
        extracted_features = model.view_extracted_features(
            markup.load_query('hi there').query)
        assert extracted_features == expected_features
예제 #5
0
    def test_fit_cv(self, resource_loader):
        """Tests fitting with param selection"""
        config = ModelConfig(
            **{
                "model_type": "text",
                "example_type": QUERY_EXAMPLE_TYPE,
                "label_type": CLASS_LABEL_TYPE,
                "model_settings": {"classifier_type": "logreg"},
                "param_selection": {
                    "type": "k-fold",
                    "k": 10,
                    "grid": {"C": [10, 100, 1000], "fit_intercept": [True, False]},
                },
                "features": {
                    "bag-of-words": {"lengths": [1]},
                    "freq": {"bins": 5},
                    "length": {},
                },
            }
        )
        model = TextModel(config)
        examples = self.labeled_data.queries()
        labels = self.labeled_data.intents()
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model._current_params
예제 #6
0
    def test_fit(self, resource_loader):
        """Tests that a basic fit succeeds"""
        config = ModelConfig(
            **{
                "model_type": "text",
                "example_type": QUERY_EXAMPLE_TYPE,
                "label_type": CLASS_LABEL_TYPE,
                "model_settings": {
                    "classifier_type": "logreg"
                },
                "params": {
                    "fit_intercept": True,
                    "C": 100
                },
                "features": {
                    "bag-of-words": {
                        "lengths": [1]
                    },
                    "freq": {
                        "bins": 5
                    },
                    "length": {},
                },
            })
        model = TextModel(config)
        examples = self.labeled_data.queries()
        labels = self.labeled_data.intents()
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model._current_params == {"fit_intercept": True, "C": 100}
예제 #7
0
    def test_fit(self, resource_loader):
        """Tests that a basic fit succeeds"""
        config = ModelConfig(
            **{
                'model_type': 'text',
                'example_type': QUERY_EXAMPLE_TYPE,
                'label_type': CLASS_LABEL_TYPE,
                'model_settings': {
                    'classifier_type': 'logreg'
                },
                'params': {
                    'fit_intercept': True,
                    'C': 100
                },
                'features': {
                    'bag-of-words': {
                        'lengths': [1]
                    },
                    'freq': {
                        'bins': 5
                    },
                    'length': {}
                }
            })
        model = TextModel(config)
        examples = [q.query for q in self.labeled_data]
        labels = [q.intent for q in self.labeled_data]
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model._current_params == {'fit_intercept': True, 'C': 100}
예제 #8
0
    def test_fit_cv(self, resource_loader):
        """Tests fitting with param selection"""
        config = ModelConfig(
            **{
                'model_type': 'text',
                'example_type': QUERY_EXAMPLE_TYPE,
                'label_type': CLASS_LABEL_TYPE,
                'model_settings': {
                    'classifier_type': 'logreg'
                },
                'param_selection': {
                    'type': 'k-fold',
                    'k': 10,
                    'grid': {
                        'C': [10, 100, 1000],
                        'fit_intercept': [True, False]
                    },
                },
                'features': {
                    'bag-of-words': {
                        'lengths': [1]
                    },
                    'freq': {
                        'bins': 5
                    },
                    'length': {}
                }
            })
        model = TextModel(config)
        examples = [q.query for q in self.labeled_data]
        labels = [q.intent for q in self.labeled_data]
        model.initialize_resources(resource_loader, examples, labels)
        model.fit(examples, labels)

        assert model._current_params