
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can Tensorflow be used to work with character substring in Python?
Character substrings can be used with Tensorflow using the ‘substr’ method which is present in ‘strings’ module of Tensorflow. It is then converted into a Numpy array and then displayed.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will see how to represent Unicode strings using Python, and manipulate those using Unicode equivalents. First, separate the Unicode strings into tokens based on script detection with the help of the Unicode equivalents of standard string ops.
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.
print("The default unit is byte") print("When len is 1, a single byte is returned") tf.strings.substr(thanks, pos=7, len=1).numpy() print("The unit is specified as UTF8_CHAR") print("It takes up 4 bytes") print(tf.strings.substr(thanks, pos=7, len=1, unit='UTF8_CHAR').numpy())
Code credit: https://www.tensorflow.org/tutorials/load_data/unicode
Output
The default unit is byte When len is 1, a single byte is returned The unit is specified as UTF8_CHAR It takes up 4 bytes b''
Explanation
- The tf.strings.substr operation takes the "unit" parameter.
- It then uses this to determine the kind of offsets the "pos" and "len" paremeters would contain.
- Related Articles
- How can Tensorflow be used to work with tf.data API and tokenizer?
- How can Tensorflow be used with boosted trees in Python?
- How can Tensorflow be used to load the flower dataset and work with it?
- How can Tensorflow text be used with whitespace tokenizer in Python?
- How can Tensorflow text be used to split the strings by character using unicode_split() in Python?
- How can Tensorflow be used with Estimator to compile the model using Python?
- How can Tensorflow be used with Estimators to evaluate the model using Python?
- How can Tensorflow be used with Estimator to predict the output using Python?
- How can Tensorflow be used to define feature columns in Python?
- How can Tensorflow be used to compose layers using Python?
- How can Tensorflow be used with tf.data for finer control using Python?
- How can functional API be used to work with residual connections in Python?
- How can Tensorflow be used to prepare the dataset with stackoverflow questions using Python?
- How can Tensorflow be used with Estimators to inspect the titanic dataset using Python?
- How can TensorFlow be used to preprocess Fashion MNIST data in Python?
