如何使用TensorFlow Text预处理文本数据?
TensorFlow Text 是一个可以与 TensorFlow 库一起使用的包。在使用它之前,必须显式安装它。它可以用于预处理基于文本的模型的数据。
阅读更多: 什么是TensorFlow以及Keras如何与TensorFlow一起创建神经网络?
我们将使用 Keras Sequential API,它有助于构建一个顺序模型,该模型用于处理简单的层堆栈,其中每一层只有一个输入张量和一个输出张量。
包含至少一层卷积层的神经网络称为卷积神经网络。我们可以使用卷积神经网络来构建学习模型。
TensorFlow Text 包含一系列与文本相关的类和操作,可用于 TensorFlow 2.0。TensorFlow Text 可用于预处理序列建模。
我们使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置,并且可以免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。
示例
import tensorflow as tf
import tensorflow_text as text
print("Converting to UTF-8 encoding")
docs = tf.constant([u'Everything not saved will be lost.'.encode('UTF-16-BE'), u'Sad☹'.encode('UTF-16-BE')])
utf8_docs = tf.strings.unicode_transcode(docs, input_encoding='UTF-16-BE', output_encoding='UTF-8')代码来源 −https://tensorflowcn.cn/tutorials/tensorflow_text/intro
输出
Converting to UTF-8 encoding
解释
可以使用“encode”方法将字符串转换为UTF-8编码。
完成此操作后,字符串将被转码为UTF-8编码。
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP