[TF. Keras] API changes from tensorflow 1. X to 2.0

Time:2020-1-14

Tensorflow version 2.0 uses keras as a high-level API, which is very friendly for keras boy / girl. Tf.keras is migrating from version 1. X to version 2.0. Several changes are needed.

1. Set random seeds

import tensorflow as tf

# TF 1.x
tf.set_random_seed(args.seed)
# TF 2.0
tf.random.set_seed(args.seed)

2. Set the number of parallel threads and dynamic memory allocation

import tensorflow as tf
from tensorflow.python.keras import backend as K

# TF 1.x
config = tf.ConfigProto(intra_op_parallelism_threads=1,
                         inter_op_parallelism_threads=1)
Config.gpu ﹣ options.allow ﹣ growth = true ﹣ not all of the display memory is full, allocated on demand
K.set_session(tf.Session(config=config))

# TF 2.0
config = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,
                                  inter_op_parallelism_threads=1)
Config.gpu ﹣ options.allow ﹣ growth = true ﹣ not all of the display memory is full, allocated on demand
K.set_session(tf.compat.v1.Session(config=config))

3. In the log generated by model. Fit(), the name of ACC is changed to accuracy, and the name of Val UU ACC is changed to val uuccuracy. Therefore, it needs to be modified in callbacks.modelcheckpoint:

from tensorflow.python.keras import callbacks

# TF 1.x
ck_callback = callbacks.ModelCheckpoint('./model.h5', monitor='val_acc', mode='max',
                                            verbose=1, save_best_only=True, save_weights_only=True)

# TF 2.0
ck_callback = callbacks.ModelCheckpoint('./model.h5', monitor='val_accuracy', mode='max',
                                            verbose=1, save_best_only=True, save_weights_only=True)