1、Tensorflow2.x版本与Tensorflow1.x版本代码转换
flags = tf.flags 改为 flags = tf.compat.v1.flags
tf.logging.info 改为 tf.compat.v1.logging.info
tf.contrib 改为 tf.compat.v1.estimator
tf.contrib.data.map_and_batch 改为 tf.data.experimental.map_and_batch
tf.python_io 改为 tf.io
tf.FixedLenFeature 改为 tf.io.FixedLenFeature
tf.gfile 改为 tf.compat.v1.gfile
tf.parse_single_example 改为 tf.io.parse_single_example
tf.to_int32(my_tensor) 改为 tf.cast(my_tensor, tf.int32)
tf.variable_scope() 改为 tf.compat.v1.variable_scope()
tf.get_variable() 改为 tf.compat.v1.get_variable()
tf.truncated_normal_initializer() 改为 tf.compat.v1.truncated_normal_initializer()
tf.assert_less_equal() 改为 tf.compat.v1.assert_less_equal()
tf.contrib.layers.xxx 改为 tf.keras.layers.xxx
如
原代码为
tf.contrib.layers.layer_norm(
inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name)
修改为
layer_norma = tf.keras.layers.LayerNormalization(axis=-1)
layer_norma(input_tensor)
tf.layers 改为 tf.compat.v1.layers
2、
原代码为
hidden_size = output_layer.shape[-1].value
修改为
hidden_size = output_layer.shape[-1]
3、
原代码为
output_layer = tf.nn.dropout(output_layer, keep_prob=0.9)
修改为
output_layer = tf.nn.dropout(output_layer, rate=0.9)
4、
原代码为
tf.train.init_from_checkpoint(init_checkpoint, assignment_map)
修改为
tf.compat.v1.train.init_from_checkpoint
5、
原代码为
global_step = tf.train.get_or_create_global_step()
修改为
global_step = tf.compat.v1.train.get_or_create_global_step()