mirror of
https://github.com/explosion/spaCy.git
synced 2025-11-25 20:36:02 +03:00
130 lines
4.5 KiB
Plaintext
130 lines
4.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'cells': [{'cell_type': 'markdown',\n",
|
|
" 'metadata': {},\n",
|
|
" 'source': ['# Pure Logistic Regression Text Categorizer\\n',\n",
|
|
" 'This tutorial demonstrates how to use the custom logistic regression text categorizer.']},\n",
|
|
" {'cell_type': 'code',\n",
|
|
" 'execution_count': None,\n",
|
|
" 'metadata': {},\n",
|
|
" 'source': ['import spacy\\n',\n",
|
|
" 'from spacy.training import Example\\n',\n",
|
|
" '\\n',\n",
|
|
" '# Load spaCy model\\n',\n",
|
|
" 'nlp = spacy.load(\"en_core_web_lg\")\\n',\n",
|
|
" 'nlp.add_pipe(\"pure_logistic_textcat\")\\n',\n",
|
|
" '\\n',\n",
|
|
" '# Example training data\\n',\n",
|
|
" 'TRAIN_DATA = [\\n',\n",
|
|
" ' (\"This is amazing!\", {\"cats\": {\"positive\": 1.0, \"negative\": 0.0}}),\\n',\n",
|
|
" ' (\"This is terrible!\", {\"cats\": {\"positive\": 0.0, \"negative\": 1.0}})\\n',\n",
|
|
" ']\\n',\n",
|
|
" '\\n',\n",
|
|
" '# Create training examples\\n',\n",
|
|
" 'examples = []\\n',\n",
|
|
" 'for text, annotations in TRAIN_DATA:\\n',\n",
|
|
" ' doc = nlp.make_doc(text)\\n',\n",
|
|
" ' example = Example.from_dict(doc, annotations)\\n',\n",
|
|
" ' examples.append(example)\\n',\n",
|
|
" '\\n',\n",
|
|
" '# Train the model\\n',\n",
|
|
" 'textcat = nlp.get_pipe(\"pure_logistic_textcat\")\\n',\n",
|
|
" 'losses = textcat.update(examples)\\n',\n",
|
|
" 'print(f\"Losses: {losses}\")\\n',\n",
|
|
" '\\n',\n",
|
|
" '# Test the model\\n',\n",
|
|
" 'test_text = \"This product is fantastic!\"\\n',\n",
|
|
" 'doc = nlp(test_text)\\n',\n",
|
|
" 'print(f\"\\\\nText: {test_text}\")\\n',\n",
|
|
" 'print(f\"Predictions: {doc.cats}\")']}]}"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"{\n",
|
|
" \"cells\": [\n",
|
|
" {\n",
|
|
" \"cell_type\": \"markdown\",\n",
|
|
" \"metadata\": {},\n",
|
|
" \"source\": [\n",
|
|
" \"# Pure Logistic Regression Text Categorizer\\n\",\n",
|
|
" \"This tutorial demonstrates how to use the custom logistic regression text categorizer.\"\n",
|
|
" ]\n",
|
|
" },\n",
|
|
" {\n",
|
|
" \"cell_type\": \"code\",\n",
|
|
" \"execution_count\": None,\n",
|
|
" \"metadata\": {},\n",
|
|
" \"source\": [\n",
|
|
" \"import spacy\\n\",\n",
|
|
" \"from spacy.training import Example\\n\",\n",
|
|
" \"\\n\",\n",
|
|
" \"# Load spaCy model\\n\",\n",
|
|
" \"nlp = spacy.load(\\\"en_core_web_lg\\\")\\n\",\n",
|
|
" \"nlp.add_pipe(\\\"pure_logistic_textcat\\\")\\n\",\n",
|
|
" \"\\n\",\n",
|
|
" \"# Example training data\\n\",\n",
|
|
" \"TRAIN_DATA = [\\n\",\n",
|
|
" \" (\\\"This is amazing!\\\", {\\\"cats\\\": {\\\"positive\\\": 1.0, \\\"negative\\\": 0.0}}),\\n\",\n",
|
|
" \" (\\\"This is terrible!\\\", {\\\"cats\\\": {\\\"positive\\\": 0.0, \\\"negative\\\": 1.0}})\\n\",\n",
|
|
" \"]\\n\",\n",
|
|
" \"\\n\",\n",
|
|
" \"# Create training examples\\n\",\n",
|
|
" \"examples = []\\n\",\n",
|
|
" \"for text, annotations in TRAIN_DATA:\\n\",\n",
|
|
" \" doc = nlp.make_doc(text)\\n\",\n",
|
|
" \" example = Example.from_dict(doc, annotations)\\n\",\n",
|
|
" \" examples.append(example)\\n\",\n",
|
|
" \"\\n\",\n",
|
|
" \"# Train the model\\n\",\n",
|
|
" \"textcat = nlp.get_pipe(\\\"pure_logistic_textcat\\\")\\n\",\n",
|
|
" \"losses = textcat.update(examples)\\n\",\n",
|
|
" \"print(f\\\"Losses: {losses}\\\")\\n\",\n",
|
|
" \"\\n\",\n",
|
|
" \"# Test the model\\n\",\n",
|
|
" \"test_text = \\\"This product is fantastic!\\\"\\n\",\n",
|
|
" \"doc = nlp(test_text)\\n\",\n",
|
|
" \"print(f\\\"\\\\nText: {test_text}\\\")\\n\",\n",
|
|
" \"print(f\\\"Predictions: {doc.cats}\\\")\"\n",
|
|
" ]\n",
|
|
" }\n",
|
|
" ]\n",
|
|
"}"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|