当前位置:网站首页>text classification with RNN
text classification with RNN
2022-06-24 06:06:00 【XianxinMao】
The purpose of this tutorial is to lead you to learn to use RNN Classify text
The data set used this time is IMDB, Altogether 50000 Movie reviews , among 25000 It's a training set , in addition 25000 This is the test set
First we need to load the dataset , Can pass TFDS It's easy to download the data set , As shown in the following code
dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
train_dataset.element_specNext we need to create text encoder, Can pass tf.keras.layers.experimental.preprocessing.TextVectorization Realization , As shown in the following code
VOCAB_SIZE = 1000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(
max_tokens=VOCAB_SIZE
)
encoder.adapt(train_dataset.map(lambda text, label: text))Next we need to build a model , The following figure is the model structure diagram
The corresponding code is as follows
model = tf.keras.Sequential([
encoder,
tf.keras.layers.Embedding(
input_dim=len(encoder.get_vocabulary()),
output_dim=64,
# Use masking to handle the variable sequence lengths
mask_zero=True),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1)
])
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam(1e-4),
metrics=['accuracy'])To this step , We can start training , And model evaluation after training
history = model.fit(train_dataset, epochs=10,
validation_data=test_dataset,
validation_steps=30)
test_loss, test_acc = model.evaluate(test_dataset)
print('Test Loss:', test_loss)
print('Test Accuracy:', test_acc)The above is the record of training results
Code address : https://codechina.csdn.net/csdn_codechina/enterprise_technology/-/blob/master/text_classification_rnn.ipynb
边栏推荐
- Why storage?
- Tesseract-OCR helloworld
- PMP | 8 abilities that excellent project managers focus on training
- Project deployment for learning 3D visualization from scratch
- Go concurrency - work pool mode
- Typora software installation
- How to select cloud game platforms? Just pay attention to two points
- You don't have to spend a penny to build a wechat official website in a minute
- How to use ffmpeg one frame H264 to decode yuv420p in audio and video development?
- How to file a personal domain name? What are the benefits of domain name filing?
猜你喜欢
随机推荐
Interpretation of Cocos creator source code: siblingindex and zindex
C51 single chip microcomputer, an entry-level tutorial for lighting up small lights
How do fixed assets intensive enterprises manage fixed assets effectively?
Malicious software packages are found in pypi code base. Tencent security threat intelligence has been included. Experts remind coders to be careful of supply chain attacks
What happened to the JVM locking on Tencent ECS?
A plate processing device of network separator which can adapt to different line port positions
Summary of basic notes of C language (III)
Project deployment for learning 3D visualization from scratch
Solution to the 39th weekly game of acwing
Why storage?
Tencent Youtu presents a number of AI technologies at the 2021 global digital economy conference
Net domain name? Net domain name?
Risc-v assembly language programming (2) assembly program ASM_ run_ led
How does the company domain name come from? What kind of domain name is a good domain name
Material production tool manual
ZABBIX enterprise distributed monitoring
Detailed explanation of IPv6 theory and concept
Container lifecycle
Smart Logistics: with the advent of aiot era, how to get through the "last mile" of logistics?
Inferior administrator and black heart Haikang



