随笔 - 93  文章 - 0  评论 - 3  阅读 - 46174

tensorflow 2 相对于 tensorflow 1 修改部分记录

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()

 

posted on   WenJXUST  阅读(243)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示