Skip to content

Commit d5a8a37

Browse files
committed
added text classification tutorial
1 parent ec0f854 commit d5a8a37

File tree

15 files changed

+100691
-0
lines changed

15 files changed

+100691
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
3131
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
3232
- [How to Build a Spam Classifier using Keras in Python](https://www.thepythoncode.com/article/build-spam-classifier-keras-python). ([code](machine-learning/nlp/spam-classifier))
3333
- [How to Build a Text Generator using Keras in Python](https://www.thepythoncode.com/article/text-generation-keras-python). ([code](machine-learning/nlp/text-generator))
34+
- [How to Perform Text Classification in Python using Tensorflow 2 and Keras](https://www.thepythoncode.com/article/text-classification-using-tensorflow-2-and-keras-in-python). ([code](machine-learning/nlp/text-classification))
3435
- ### [Computer Vision](https://www.thepythoncode.com/topic/computer-vision)
3536
- [How to Detect Human Faces in Python using OpenCV](https://www.thepythoncode.com/article/detect-faces-opencv-python). ([code](machine-learning/face_detection))
3637
- [How to Make an Image Classifier in Python using Keras](https://www.thepythoncode.com/article/image-classification-keras-python). ([code](machine-learning/image-classifier))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from tensorflow.keras.callbacks import TensorBoard
2+
3+
import os
4+
5+
from parameters import *
6+
from utils import create_model, load_20_newsgroup_data
7+
8+
# create these folders if they does not exist
9+
if not os.path.isdir("results"):
10+
os.mkdir("results")
11+
12+
if not os.path.isdir("logs"):
13+
os.mkdir("logs")
14+
15+
if not os.path.isdir("data"):
16+
os.mkdir("data")
17+
18+
# dataset name, IMDB movie reviews dataset
19+
dataset_name = "20_news_group"
20+
# get the unique model name based on hyper parameters on parameters.py
21+
model_name = get_model_name(dataset_name)
22+
23+
# load the data
24+
data = load_20_newsgroup_data(N_WORDS, SEQUENCE_LENGTH, TEST_SIZE, oov_token=OOV_TOKEN)
25+
26+
model = create_model(data["tokenizer"].word_index, units=UNITS, n_layers=N_LAYERS,
27+
cell=RNN_CELL, bidirectional=IS_BIDIRECTIONAL, embedding_size=EMBEDDING_SIZE,
28+
sequence_length=SEQUENCE_LENGTH, dropout=DROPOUT,
29+
loss=LOSS, optimizer=OPTIMIZER, output_length=data["y_train"][0].shape[0])
30+
31+
model.summary()
32+
33+
tensorboard = TensorBoard(log_dir=os.path.join("logs", model_name))
34+
35+
history = model.fit(data["X_train"], data["y_train"],
36+
batch_size=BATCH_SIZE,
37+
epochs=EPOCHS,
38+
validation_data=(data["X_test"], data["y_test"]),
39+
callbacks=[tensorboard],
40+
verbose=1)
41+
42+
43+
model.save(os.path.join("results", model_name) + ".h5")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# [How to Perform Text Classification in Python using Tensorflow 2 and Keras](https://www.thepythoncode.com/article/text-classification-using-tensorflow-2-and-keras-in-python)
2+
To use this:
3+
- `pip3 install -r requirements.txt`
4+
- Please read [this tutorial](https://www.thepythoncode.com/article/text-classification-using-tensorflow-2-and-keras-in-python) before using this.
5+
- Tweak the hyperparameters in `parameters.py` and train the model.
6+
- For testing, consider using `test.py`

0 commit comments

Comments
 (0)