In addition to the general purpose container datatypes: dict, list, set and tuple; Python also implements specialized container datatypes. One of the commonly used specialized container datatype in Python is the 'named tuple'.
A 'named tuple' can be created in Python using the collections module factory function namedtuple(). This function returns a tuple like object whose elements can be accessed using the corresponding attribute name. Hence the name of this tuple like object: 'named tuple'. The attribute names or field_names for the 'named tuple' can be either a sequence of strings (Eg. ['x', 'y']) or a single string with each fieldname separated by whitespace and/or commas (Eg. 'x y' or 'x, y'). Of note here, while assigning a fieldname; they should not start with a digit or an underscore. However, both digits and underscores are allowed elsewhere in the fieldname. A common application for the 'named tuple' is to create more readable vectors such as those representing the Cartesian coordinate space or for representing the latitude-longitude data from a GPS receiver.
Creating and working with 'named tuples' in Python
Import libraries
Create a 'named tuple'
Python 'for-loop' to manipulate the 'named tuple'
Tensorflow 'while_loop' to manipulate the 'named tuple'
The same operations performed in the Python 'for-loop' is replicated using Tensorflow 'while_loop'. The slicing of tuple elements is applicable for the 'named tuples'. Therefore, a 'named tuple' provides the convenience of accessing its contents using both field_names as well as their position index.
Performance optimization of the 'named tuple' operations in Tensorflow 'while_loop'
Described below is the a quick code-snippet on performance optimization for the 'named tuple' operations. In this example, a 'named tuple' is updated using the Tensorflow 'while_loop' by wrapping the operation as a decorated function.
References
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
OverviewMoad Computer is an actionable insights firm. We provide enterprises with end-to-end artificial intelligence solutions. Actionable Insights blog is a quick overview of things we are most excited about. Archives
November 2022
Categories |