- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can Tensorflow text be used to split the strings by character using unicode_split() in Python?
Tensorflow text can be used to split the strings by character using ‘unicode_split’ method, by first encoding the split strings, and then assigning the function call to a variable. This variable holds the result of the function call.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.
A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model.
TensorFlow Text contains collection of text related classes and ops that can be used with TensorFlow 2.0. The TensorFlow Text can be used to preprocess sequence modelling.
We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.
Tokenization is the method of breaking down a string into tokens. These tokens can be words, numbers, or punctuation.
The important interfaces include Tokenizer and TokenizerWithOffsets each of which have a single method tokenize and tokenize_with_offsets respectively. There are multiple tokenizers, each of which implement TokenizerWithOffsets (which extends the Tokenizer class). This includes an option to get byte offsets into the original string. This helps know the bytes in the original string the token was created from.
Example
print("The encoded characters are split") tokens = tf.strings.unicode_split([u"仅今年前".encode('UTF-8')], 'UTF-8') print("The tokenized data is converted to a list") print(tokens.to_list())
Code credit −https://www.tensorflow.org/tutorials/tensorflow_text/intro
Output
The encoded characters are split The tokenized data is converted to a list [[b'\xe4\xbb\x85', b'\xe4\xbb\x8a', b'\xe5\xb9\xb4', b'\xe5\x89\x8d']]
Explanation
All tokenizers return RaggedTensors with the inner-most dimension of tokens mapped to the original individual strings.
The resulting shape's rank increases by one.
When tokenizing languages without using whitespace to segment words, it is common to split by character.
This can be done using the unicode_split op found in Tensorflow core.
Once the unicode_split is called, the tokenized data is added to a list.
- Related Articles
- How can Tensorflow text be used to split the UTF-8 strings in Python?
- How can Tensorflow be used to work with character substring in Python?
- How can Tensorflow text be used to preprocess text data?
- How can Tensorflow text be used with whitespace tokenizer in Python?
- How can Tensorflow be used to decode the predictions using Python?
- How can Tensorflow be used to check the predicrion using Python?
- How can Tensorflow be used to check the predictions using Python?
- How can Tensorflow be used to plot the results using Python?
- How can Tensorflow be used to visualize the data using Python?
- How can Tensorflow be used to standardize the data using Python?
- How can Tensorflow be used to compile the model using Python?
- How can Tensorflow be used to train the model using Python?
- How can Tensorflow be used with Estimators to split the iris dataset?
- How can Tensorflow be used to compose layers using Python?
- How can Tensorflow and Tensorflow text be used to tokenize string data?
