开始使用免费开始使用

新闻文章分类

在本练习中,您将创建一个多分类模型。

数据集已作为 news_novel 加载到环境中。训练数据的所有预处理也都已完成,tokenizer 同样可用。

一个 RNN 模型已按以下结构进行了预训练:使用 Embedding 层、一个 LSTM 层,以及输出的 Dense 层,目标为 3 个类别:sci.spacealt.atheismsoc.religion.christian。该已训练模型的权重保存在 classify_news_weights.h5 文件中。

您将对新的数据进行预处理,并在新的数据集 news_novel 上进行评估。

本练习是课程的一部分

使用 Keras 构建语言建模的循环神经网络(RNN)

查看课程

练习说明

  • 使用已加载的 tokenizer 转换 news_novel.data 中的数据。
  • 对得到的数值索引序列进行填充(pad)。
  • news_novel.target 中的标签转换为 one-hot 表示。
  • 使用 .evaluate() 方法评估模型,并打印得到的损失和准确率。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Change text for numerical ids and pad
X_novel = tokenizer.texts_to_sequences(____)
X_novel = pad_sequences(____, maxlen=400)

# One-hot encode the labels
Y_novel = to_categorical(____)

# Load the model pre-trained weights
model.load_weights('classify_news_weights.h5')

# Evaluate the model on the new dataset
loss, acc = model.____(X_novel, Y_novel, batch_size=64)

# Print the loss and accuracy obtained
print("Loss:\t{0}\nAccuracy:\t{1}".format(____, ____))
编辑并运行代码