Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5a8a37

Browse files
committed
added text classification tutorial
1 parentec0f854 commitd5a8a37

File tree

15 files changed

+100691
-0
lines changed

15 files changed

+100691
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
fromtensorflow.keras.callbacksimportTensorBoard
2+
3+
importos
4+
5+
fromparametersimport*
6+
fromutilsimportcreate_model,load_20_newsgroup_data
7+
8+
# create these folders if they does not exist
9+
ifnotos.path.isdir("results"):
10+
os.mkdir("results")
11+
12+
ifnotos.path.isdir("logs"):
13+
os.mkdir("logs")
14+
15+
ifnotos.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")
Lines changed: 6 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp