使用LSTM网络实现文本情感分析

一、实验目的:

理解循环神经网络的基本概念和原理;了解循环神经网络处理文本数据的基本方法;掌握循环神经网络处理文本数据的实践方法,并实现文本情感分析任务。

  • 实验要求:

使用Keras框架定义并训练循环神经网络模型,并进行文本情感分析。

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt

import numpy as np

# 加载 IMDB 数据
imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
print("训练记录数量:{},标签数量:{}".format(len(train_data), len(train_labels)))
print(train_data[0])

# 数据标准化
train_data = keras.preprocessing.sequence.pad_sequences(train_data, padding='post', maxlen=256)
test_data = keras.preprocessing.sequence.pad_sequences(test_data, padding='post', maxlen=256)
print(train_data[0])
# 构建模型
vocab_size = 10000
model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size, 64),
                             tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)), tf.keras.
                            layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1)
                             ])
model.summary()
# 配置并训练模型
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
x_val = train_data[:10000]
partial_x_train = train_data[10000:]
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]
history = model.fit(partial_x_train, partial_y_train, epochs=10, batch_size=512, validation_data=(x_val, y_val),
                    verbose=1)

result = model.evaluate(test_data, test_labels, verbose=2)
print(result)
# 训练过程可视化
history_dict = history.history
print(history_dict.keys())


def plot_graphs(history, string):
    plt.plot(history.history[string])
    plt.plot(history.history['val_' + string])
    plt.xlabel("Epochs")
    plt.ylabel(string)
    plt.legend([string, 'val_' + string])
    plt.show()


plot_graphs(history, "accuracy")


plot_graphs(history, "loss")

运行结果可视化:

相关推荐

  1. 使用Python实现文本分类情感分析模型

    2024-04-26 20:56:01       21 阅读

最近更新

  1. ArduPilot开源飞控之AP_VisualOdom

    2024-04-26 20:56:01       0 阅读
  2. 如何实现跨域

    2024-04-26 20:56:01       0 阅读
  3. centos7yum-mysql-community-server安装流程步骤

    2024-04-26 20:56:01       0 阅读
  4. toFixed 四舍五入问题

    2024-04-26 20:56:01       0 阅读
  5. [C++][CMake][嵌套的CMake]详细讲解

    2024-04-26 20:56:01       1 阅读
  6. 65.指针函数和函数指针

    2024-04-26 20:56:01       1 阅读
  7. 网络安全测评技术与标准

    2024-04-26 20:56:01       1 阅读
  8. Qt之多线程编程(QThread)

    2024-04-26 20:56:01       1 阅读
  9. MySQL:left join 后用 on 还是 where?

    2024-04-26 20:56:01       1 阅读
  10. 最短路径算法(算法篇)

    2024-04-26 20:56:01       1 阅读

热门阅读

  1. vue中@click.prevent函数的使用

    2024-04-26 20:56:01       15 阅读
  2. 【汇编】指令系统的寻址方式

    2024-04-26 20:56:01       16 阅读
  3. 前端生成二维码

    2024-04-26 20:56:01       15 阅读
  4. 定时任务cron与crontab

    2024-04-26 20:56:01       14 阅读
  5. 一维字符型数组算法整理

    2024-04-26 20:56:01       16 阅读