当前位置:网站首页>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
边栏推荐
- 5 minutes, online from 0 to 1!
- The company is worried about customer frustration and brand damage caused by DDoS Attacks
- Risc-v assembly language programming (2) assembly program ASM_ run_ led
- What does it mean that the domain name is being walled? How to solve the problem of domain name by wall?
- CLB unable to access / access timeout troubleshooting
- The basic concept of network is the relationship among services, protocols, processes and ports.
- Why storage?
- Continuously evolving cloud native application delivery
- Enterprise management background user manual
- What is the reason why the list of channels on the left side of easycvr video Plaza displays garbled codes?
猜你喜欢
随机推荐
Differences between JSON objects and JSON strings
Basic concepts of complex networks
Adobe international certification wants to design! Understanding the style guide is your best introduction design
How do fixed assets intensive enterprises manage fixed assets effectively?
Figure 1 understand Tencent reassurance platform
MySQL series tutorial (I) getting to know MySQL
Several relations to be clarified in the process of digital transformation: stock and increment
Introduction of frequency standard comparison measurement system
Is the prospect of cloud computing in the security industry worth being optimistic about?
Container lifecycle
Clickhouse alter table execution process
Semantic web, semantic web, linked data and knowledge map
How to record the domain name reliably? What are the consequences of not filing a domain name?
CLB unable to access / access timeout troubleshooting
How do I view the IP address of a domain name? What is the relationship between domain name and IP?
How to resolve the domain name? How to choose a domain name?
Flutter layout Basics - page navigation and return
Material production tool manual
The influence of TLS protocol and cipher on remote RDP
Tesseract-OCR helloworld



