Search code examples
python-3.xnlpspacyspacy-3

Email Classifier using Spacy , throwing the below error due to version issue when tried to implement BOW


I'm trying to Create the TextCategorizer with exclusive classes and "bow" architecture but its throwing the below error due to version issue and my python version is 3.8 ,also my spacy version is 3.2.3 , please some one help me in resolving this

######## Main method ########

def main():

    # Load dataset
    data = pd.read_csv(data_path, sep='\t')
    observations = len(data.index)
    # print("Dataset Size: {}".format(observations))

    # Create an empty spacy model
    nlp = spacy.blank("en")

    # Create the TextCategorizer with exclusive classes and "bow" architecture
    text_cat = nlp.create_pipe(
                  "textcat",
                  config={
                    "exclusive_classes": True,
                    "architecture": "bow"})

    # Adding the TextCategorizer to the created empty model
    nlp.add_pipe(text_cat)

    # Add labels to text classifier
    text_cat.add_label("ham")
    text_cat.add_label("spam")

    # Split data into train and test datasets
    x_train, x_test, y_train, y_test = train_test_split(
        data['text'], data['label'], test_size=0.33, random_state=7)

    # Create the train and test data for the spacy model
    train_lables = [{'cats': {'ham': label == 'ham',
                              'spam': label == 'spam'}}  for label in y_train]
    test_lables = [{'cats': {'ham': label == 'ham',
                          'spam': label == 'spam'}}  for label in y_test]

    # Spacy model data
    train_data = list(zip(x_train, train_lables))
    test_data = list(zip(x_test, test_lables))

    # Model configurations
    optimizer = nlp.begin_training()
    batch_size = 5
    epochs = 10

    # Training the model
    train_model(nlp, train_data, optimizer, batch_size, epochs)

    # Sample predictions
    # print(train_data[0])
    # sample_test = nlp(train_data[0][0])
    # print(sample_test.cats)

    # Train and test accuracy
    train_predictions = get_predictions(nlp, x_train)
    test_predictions = get_predictions(nlp, x_test)
    train_accuracy = accuracy_score(y_train, train_predictions)
    test_accuracy = accuracy_score(y_test, test_predictions)

    print("Train accuracy: {}".format(train_accuracy))
    print("Test accuracy: {}".format(test_accuracy))

    # Creating the confusion matrix graphs
    cf_train_matrix = confusion_matrix(y_train, train_predictions)
    plt.figure(figsize=(10,8))
    sns.heatmap(cf_train_matrix, annot=True, fmt='d')

    cf_test_matrix = confusion_matrix(y_test, test_predictions)
    plt.figure(figsize=(10,8))
    sns.heatmap(cf_test_matrix, annot=True, fmt='d')


if __name__ == "__main__":
    main()

Below is the error

---------------------------------------------------------------------------
ConfigValidationError                     Traceback (most recent call last)
<ipython-input-6-a77bb5692b25> in <module>
     72 
     73 if __name__ == "__main__":
---> 74     main()

<ipython-input-6-a77bb5692b25> in main()
     12 
     13     # Create the TextCategorizer with exclusive classes and "bow" architecture
---> 14     text_cat = nlp.add_pipe(
     15                   "textcat",
     16                   config={

~\anaconda3\lib\site-packages\spacy\language.py in add_pipe(self, factory_name, name, before, after, first, last, source, config, raw_config, validate)
    790                     lang_code=self.lang,
    791                 )
--> 792             pipe_component = self.create_pipe(
    793                 factory_name,
    794                 name=name,

~\anaconda3\lib\site-packages\spacy\language.py in create_pipe(self, factory_name, name, config, raw_config, validate)
    672         # We're calling the internal _fill here to avoid constructing the
    673         # registered functions twice
--> 674         resolved = registry.resolve(cfg, validate=validate)
    675         filled = registry.fill({"cfg": cfg[factory_name]}, validate=validate)["cfg"]
    676         filled = Config(filled)

~\anaconda3\lib\site-packages\thinc\config.py in resolve(cls, config, schema, overrides, validate)
    727         validate: bool = True,
    728     ) -> Dict[str, Any]:
--> 729         resolved, _ = cls._make(
    730             config, schema=schema, overrides=overrides, validate=validate, resolve=True
    731         )

~\anaconda3\lib\site-packages\thinc\config.py in _make(cls, config, schema, overrides, resolve, validate)
    776         if not is_interpolated:
    777             config = Config(orig_config).interpolate()
--> 778         filled, _, resolved = cls._fill(
    779             config, schema, validate=validate, overrides=overrides, resolve=resolve
    780         )

~\anaconda3\lib\site-packages\thinc\config.py in _fill(cls, config, schema, validate, resolve, parent, overrides)
    831                     schema.__fields__[key] = copy_model_field(field, Any)
    832                 promise_schema = cls.make_promise_schema(value, resolve=resolve)
--> 833                 filled[key], validation[v_key], final[key] = cls._fill(
    834                     value,
    835                     promise_schema,

~\anaconda3\lib\site-packages\thinc\config.py in _fill(cls, config, schema, validate, resolve, parent, overrides)
    897                 result = schema.parse_obj(validation)
    898             except ValidationError as e:
--> 899                 raise ConfigValidationError(
    900                     config=config, errors=e.errors(), parent=parent
    901                 ) from None

ConfigValidationError: 

Config validation error

textcat -> architecture        extra fields not permitted
textcat -> exclusive_classes   extra fields not permitted

{'nlp': <spacy.lang.en.English object at 0x000001B90CD4BF70>, 'name': 'textcat', 'architecture': 'bow', 'exclusive_classes': True, 'model': {'@architectures': 'spacy.TextCatEnsemble.v2', 'linear_model': {'@architectures': 'spacy.TextCatBOW.v2', 'exclusive_classes': True, 'ngram_size': 1, 'no_output_layer': False}, 'tok2vec': {'@architectures': 'spacy.Tok2Vec.v2', 'embed': {'@architectures': 'spacy.MultiHashEmbed.v2', 'width': 64, 'rows': [2000, 2000, 1000, 1000, 1000, 1000], 'attrs': ['ORTH', 'LOWER', 'PREFIX', 'SUFFIX', 'SHAPE', 'ID'], 'include_static_vectors': False}, 'encode': {'@architectures': 'spacy.MaxoutWindowEncoder.v2', 'width': 64, 'window_size': 1, 'maxout_pieces': 3, 'depth': 2}}}, 'scorer': {'@scorers': 'spacy.textcat_scorer.v1'}, 'threshold': 0.5, '@factories': 'textcat'}

My Spacy-Version

print(spacy.__version__)

3.2.3

My Python Version

import sys
print(sys.version)

3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]

Tring to downgrade the Spacy-Version

!conda install -c conda-forge spacy = 2.1.8
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
Solving environment: ...working... 

Building graph of deps:   0%|          | 0/5 [00:00<?, ?it/s]
Examining spacy=2.1.8:   0%|          | 0/5 [00:00<?, ?it/s] 
Examining python=3.8:  20%|##        | 1/5 [00:00<00:00,  4.80it/s]
Examining python=3.8:  40%|####      | 2/5 [00:00<00:00,  9.60it/s]
Examining @/win-64::__cuda==11.6=0:  40%|####      | 2/5 [00:01<00:00,  9.60it/s]
Examining @/win-64::__cuda==11.6=0:  60%|######    | 3/5 [00:01<00:01,  1.97it/s]
Examining @/win-64::__win==0=0:  60%|######    | 3/5 [00:01<00:01,  1.97it/s]    
Examining @/win-64::__archspec==1=x86_64:  80%|########  | 4/5 [00:01<00:00,  1.97it/s]
                                                                                       

Determining conflicts:   0%|          | 0/5 [00:00<?, ?it/s]
Examining conflict for spacy python:   0%|          | 0/5 [00:00<?, ?it/s]
                                                                          

UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:

Specifications:

  - spacy=2.1.8 -> python[version='>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']

Your python: python=3.8

Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
failed


If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

Please feel free to comment or ask . Thank you


Solution

  • Just from the way I would understand that error message it tells you that the spacy version you want to install (2.1.8) is incompatible with the python version you have (3.8.8). It needs Python 3.6 or 3.7.

    So either create an environment with Python 3.6 or 3.7 (its quite easy to specify Python version when creating a new environment in conda) or use a higher version of spacy. Did you already try if the code works if you just use the newest version of spacy?

    Is there a specific reason for why you are using this spacy version? If you are using some methods that are not supported anymore it might make more sense to update your code to the newer spacy methods. Especially if you are doing this to learn about spacy it is counterproductive to learn methods that are not supported anymore. Sadly a lot of tutorials fail to either update their code or at least specify what versions they are using and then leave their code online for years.