table of Contents
2.1 Eager mode is convenient for learning and model debugging
2.2 Operating characteristics of Eager mode
2.3 Calculation of gradient in Eager mode
4 Examples of variables and automatic differential operations
Note: The test data does not need to be out of order
tf.keras.metrics summary calculation module
1 Introduction:
We learn to use the keras provided api implement neural networks, but because of his great package for the self-defined circulation and custom training is unfriendly
We can use Eage, use Eager in recycling
2 Introduction:
TensorFlow's eager mode is an imperative programming environment that allows us to immediately evaluate the results of operations without having to build a calculation graph .
Eager and graph operation mode:
Simply put: Figure operator is equivalent to every step of drawing out, Eager you can direct the outcome
2.1 Eager mode is convenient for learning and model debugging
Eager mode greatly facilitates us to use TensorFlow to debug models, increases the flexibility of network debugging and the friendliness of tensorflow for beginners. Here we can call it the interactive mode of tensorflow
- Intuitive interface-build code naturally and use Python data structures.
- Quickly iterate small models and small data.
Easier to debug-directly inspect, run models, and test changes in an interactive environment. During this process, the code will report errors immediately .
Natural control flow-eager mode uses Python control flow instead of graph control flow, which simplifies the creation of dynamic models.
Eager execution supports most TensorFlow operations and GPU acceleration.
2.2 Operating characteristics of Eager mode
- In eager mode, the TensorFlow operation will be executed immediately and its value will be returned to Python (see the example later).
- The tf.Tensor object refers to the specific value instead of the symbol handle of the node in the calculation graph (can be modified).
- Tensorflow can work well with NumPy in Eager mode . TensorFlow mathematical operations can combine Python objects and NumPy arrays
- Converted to tf.Tensor object. The tf.Tensor.numpy method returns the value of the object as a NumPy ndarray .
2.3 Calculation of gradient in Eager mode
In Eager mode, use tf.GradientTape to track the operation of calculating the gradient
Since different operations may occur during each execution, all forward pass operations are recorded on Tape . To calculate the gradient, the tape will play back and then discarded particular tf.GradientTape only one gradient calculation ; subsequent calls will lead error (RuntimeError) issued runtime. You can also set up repeatable recall
3. Demo code of Eager module
- tf.multiply() Multiply the corresponding elements in the two matrices
- tf.matmul() multiplies matrix a by matrix b to generate a * b
4 Examples of variables and automatic differential operations
5 Custom training
When not training
After training:
Note: The test data does not need to be out of order
tf.keras.metrics summary calculation module
You can find the mean m = tf.keras.metrics.Mean('acc')
The enumerate() function is used to combine a traversable data object (such as a list , tuple, or string) into an index sequence, and list data and data subscripts at the same time. It is generally used in a for loop.