Porting with Keras
If the original TensorFlow network is constructed based on the Keras API, see this section to understand the manual porting process.
About Keras
Similar to Estimator, Keras is another high-level API of TensorFlow. It constructs graphs efficiently, and provides APIs for training, evaluation, validation, and export. Develop your training script with the Keras API as follows:
- Preprocess data.
- Construct your model.
- Compile your model.
- Train your model.
Currently, only training scripts compiled using TensorFlow Keras APIs are supported. Native Keras APIs are not supported.
The following describes how to port the Keras training scripts for training on the Ascend AI Processor:
Header File
To import NPU-related libraries, add this header file reference in related Python files as follows.
1 | from npu_bridge.npu_init import * |
After the preceding header file is imported, the training script is executed on the Ascend AI Processor by default.
Porting Configuration
To train your model on the Ascend AI Processor, create a TensorFlow session, register Keras, and add related configurations. When the training ends, close the session.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import tensorflow as tf import tensorflow.python.keras as keras from tensorflow.python.keras import backend as K from npu_bridge.npu_init import * # Add allow_soft_placement=True for the session configurations to allow TensorFlow to automatically allocate devices. sess_config = tf.ConfigProto(allow_soft_placement=True) # Add an NPU optimizer named NpuOptimizer. During network compilation, the NPU traverses only the session configurations under NpuOptimizer. custom_op = sess_config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" # Explicitly disable the remapping and memory_optimization functions of TensorFlow to avoid conflicts with the functions of the NPU. sess_config.graph_options.rewrite_options.remapping = RewriterConfig.OFF sess_config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF sess = tf.Session(config=sess_config) K.set_session(sess) # Preprocess data... # Construct a model... # Compile the model... # Train the model... sess.close() |
With the ported training script, the number of training iterations performed by the Ascend AI Processor is fixed to 1 in every sess.run call. To reduce data transfers between the host and device and shorten the training duration, use the model_to_npu_estimator API to convert the model constructed by Keras to an NPUEstimator object and set iterations_per_loop (number of training iterations performed by the Ascend AI Processor every sess.run call) in NPURunConfig to a value that suits your needs. For details, see Enabling Iteration Offload In Keras Mode.