In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to use tf.Module to better build Autograph, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
There are three ways to build computing diagrams: static diagrams, dynamic graphs, and Autograph. TensorFlow 2.0 mainly uses dynamic computing diagrams and Autograph.
The dynamic calculation diagram is easy to debug, and the coding efficiency is high, but the execution efficiency is low.
The static calculation chart is very efficient, but it is difficult to debug. On the other hand, Autograph mechanism can convert dynamic graph into static computing graph, which has the advantages of both execution efficiency and coding efficiency. Of course, the code that the Autograph mechanism can convert is not without any constraints, and there are some coding specifications to follow, otherwise the conversion may fail or fail to meet expectations. Let's take a look at using tf.Module to better build Autograph.
An overview of Autograph and tf.Module
Earlier, when introducing the coding specification of Autograph, it was mentioned that when building Autograph, you should avoid defining tf.Variable. Tf.function inside the function decorated by @ tf.Variable.
However, if you define tf.Variable outside the function, it will appear that the function has external variable dependencies, and the encapsulation is not perfect.
A simple idea is to define a class and place the associated tf.Variable creation in the class's initialization method. Instead, put the logic of the function in other methods.
After such a fierce operation as a tiger, we will feel that everything is as natural as the human law, the local law, the daily law, the way and the law.
Surprisingly, TensorFlow provides a base class tf.Module, by inheriting it to build subclasses, we can not only get the above natural, but also very convenient to manage variables, but also very convenient to manage other Module it references, and most importantly, we can use tf.saved_model to save the model and achieve cross-platform deployment.
In fact, tf.keras.models.Model and tf.keras.layers.Layer are inherited from tf.Module and provide convenient variable management and referenced sub-module management functions.
Therefore, with the encapsulation provided by tf.Module and the rich low-level API of TensoFlow, we can actually develop any machine learning model (not just a neural network model) based on TensorFlow and implement cross-platform deployment.
Second, use tf.Module to encapsulate Autograph
Define a simple function.
Let's use the subclassing of tf.Module to encapsulate it.
If you view the calculation diagram in tensorboard, the module name demo_module will be added to facilitate the hierarchical rendering of the calculation diagram structure.
In addition to using the subclassing of tf.Module to achieve encapsulation, we can also encapsulate it by adding attributes to tf.Module.
Three, tf.Module and tf.keras.Model,tf.keras.layers.Layer
The models and layers in tf.keras are implemented by inheriting tf.Module, and also have variable management and sub-module management functions.
The above is how to use tf.Module to better build Autograph. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.