Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt text classification from scratch #2056

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions examples/nlp/ipynb/text_classification_from_scratch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"\n",
"**Authors:** Mark Omernick, Francois Chollet<br>\n",
"**Date created:** 2019/11/06<br>\n",
"**Last modified:** 2020/05/17<br>\n",
"**Last modified:** 2025/03/05<br>\n",
"**Description:** Text sentiment classification starting from raw text files."
]
},
Expand Down Expand Up @@ -47,10 +47,9 @@
"source": [
"import os\n",
"\n",
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n",
"os.environ[\"KERAS_BACKEND\"] = \"torch\" # or jax, or tensorflow\n",
"\n",
"import keras\n",
"import tensorflow as tf\n",
"import numpy as np\n",
"from keras import layers"
]
Expand Down Expand Up @@ -269,6 +268,7 @@
"source": [
"import string\n",
"import re\n",
"import tensorflow as tf\n",
"\n",
"\n",
"# Having looked at our data above, we see that the raw text contains HTML break\n",
Expand Down Expand Up @@ -440,7 +440,7 @@
},
"outputs": [],
"source": [
"epochs = 3\n",
"epochs = 5\n",
"\n",
"# Fit the model using the train and test datasets.\n",
"model.fit(train_ds, validation_data=val_ds, epochs=epochs)"
Expand Down Expand Up @@ -486,21 +486,36 @@
},
"outputs": [],
"source": [
"# A string input\n",
"inputs = keras.Input(shape=(1,), dtype=\"string\")\n",
"# Turn strings into vocab indices\n",
"indices = vectorize_layer(inputs)\n",
"# Turn vocab indices into predictions\n",
"outputs = model(indices)\n",
"\n",
"# Our end to end model\n",
"end_to_end_model = keras.Model(inputs, outputs)\n",
"end_to_end_model.compile(\n",
" loss=\"binary_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"]\n",
")\n",
"\n",
"# Test it with `raw_test_ds`, which yields raw strings\n",
"end_to_end_model.evaluate(raw_test_ds)"
"# We create a custom Model to override the evaluate method so\n",
"# that it first pre-process text data\n",
"class ModelEndtoEnd(keras.Model):\n",
"\n",
" def evaluate(self, inputs):\n",
"\n",
" # Turn strings into vocab indices\n",
" test_ds = inputs.map(vectorize_text)\n",
" indices = test_ds.cache().prefetch(buffer_size=10)\n",
" return super().evaluate(indices)\n",
"\n",
" # Build the model\n",
" def build(self, input_shape):\n",
" self.built = True\n",
"\n",
"\n",
"def get_end_to_end(model):\n",
" inputs = model.inputs[0]\n",
" outputs = model.outputs\n",
" end_to_end_model = ModelEndtoEnd(inputs, outputs, name=\"end_to_end_model\")\n",
" end_to_end_model.compile(\n",
" optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"]\n",
" )\n",
" return end_to_end_model\n",
"\n",
"\n",
"end_to_end_classification_model = get_end_to_end(model)\n",
"# Pass raw text dataframe to the model\n",
"end_to_end_classification_model.evaluate(raw_test_ds)"
]
}
],
Expand Down
Loading