1、to_float,to_int32
# has_min = tf.to_float(tf.reduce_any(pos_num_neg_mask))
has_min = tf.cast(tf.reduce_any(pos_num_neg_mask), tf.float32)
 
2、tf.log
# softmax_loss = -tf.reduce_sum(y_true * tf.log(y_pred),
#                               axis=-1)
softmax_loss = -tf.reduce_sum(y_true * tf.math.log(y_pred),
                              axis=-1)
 
3、get_session
import tensorflow.keras.backend as K
self.sess           = K.get_session()
 
会出现问题(AttributeError: module 'tensorflow.keras.backend' has no attribute 'get_session'),因为tensorflow2 中的keras.backend没有这个,改成以下:
import tensorflow.keras.backend as K
import tensorflow as tf
tf.compat.v1.disable_v2_behavior() #这个要加,字面意思就是禁用v2的一些性能,不然用tf.compat.v1中的部分函数时会出问题(如TypeError: Tensors are unhashable. (KerasTensor(type_spec=TensorSpec(shape=(None, 4), dtype=tf.float32, name=None), description="created by layer 'input_1'"))Instead, use tensor.ref() as the key.)
self.sess = tf.compat.v1.keras.backend.get_session()
# self.sess           = K.get_session()