当前位置:网站首页>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
边栏推荐
- Working principle and type selection of signal generator
- How to record the purchased domain name? Why should the purchased domain name be filed?
- What happened to the JVM locking on Tencent ECS?
- Analysis of official template of wechat personnel recruitment management system (II)
- Intelligent monitoring era - the way of monitoring construction
- At the beginning of the school season, use this template to improve the management level
- Test development knowledge map
- NoClassDefFoundError and classnotfoundexception exceptions
- What if the domain name is blocked? What can I do to quickly unseal?
- How to buy a domain name? How to do a good job in website construction?
猜你喜欢
随机推荐
Working principle and type selection of signal generator
MySQL series tutorial (I) getting to know MySQL
Is the prospect of cloud computing in the security industry worth being optimistic about?
How to solve the enterprise network security problem in the mixed and multi cloud era?
Summary of basic notes of C language (III)
Flutter - date of birth calculation age tool class
Go concurrency - work pool mode
Tencent Youtu presents a number of AI technologies at the 2021 global digital economy conference
Brief introduction to the working principle of high frequency signal generator
Oceanus practice - develop MySQL CDC to es SQL jobs from 0 to 1
How do fixed assets intensive enterprises manage fixed assets effectively?
How does the company domain name come from? What kind of domain name is a good domain name
Differences between JSON objects and JSON strings
How to renew the domain name when it expires
[in depth sharing] Devops evolution path -- Realizing R & D digital transformation based on four vertical and four horizontal Devops system
Micro build low code supports Excel to import data source
How to file a personal domain name? What are the benefits of domain name filing?
How to register a domain name? What are the benefits of building a website?
Talk about the story behind search engines
Understand the classification and summary of cross chain related technologies



