首页 > 解决方案 > How to use K.get_session in Tensorflow 2.0 or how to migrate it?

问题描述

def __init__(self, **kwargs):
    self.__dict__.update(self._defaults) # set up default values
    self.__dict__.update(kwargs) # and update with user overrides
    self.class_names = self._get_class()
    self.anchors = self._get_anchors()
    self.sess = K.get_session()

RuntimeError: get_session is not available when using TensorFlow 2.0.

标签: machine-learningkerasdeep-learningreal-timetensorflow2.0

解决方案


Tensorflow 2.0 不再直接公开 backend.get_session,但代码仍然存在并为 tf1 公开。

https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/keras/backend.py#L465

您可以将其与 tf1 兼容接口一起使用:

sess = tf.compat.v1.keras.backend.get_session()

或者使用内部路径导入 tenforflow 后端:

import tensorflow.python.keras.backend as K
sess = K.get_session()

推荐阅读