“TypeError: 'Session' object is not callable” error running sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 17:37:20

问题


I'm trying to set seeds and configure keras settings to ensure my experiments are reproducible. When I run the following (based on code in an answer to this question):

# Import libraries
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.regularizers import l2

# for setting seeds and configuring keras so that experiments are reproducible
from numpy.random import seed
import random as rn
import os
from tensorflow.keras import backend as K

seed_num = 1

os.environ['PYTHONHASHSEED'] = '0'
np.random.seed(seed_num)
rn.seed(seed_num)

session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

tf.random.set_seed(seed_num)

sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)
K.set_session(sess)

...an error occurs:

TypeError: 'Session' object is not callable

What do I need to change to get this to run successfully and ensure that my experiments are reproducible?

I'm running tensorflow version 2.1.0 in a Jupyter Notebook on a Mac.


回答1:


On the line second to last, you probably want to construct a Session object using the arguments graph and config, not call a Session.

Change this:

sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)

to this:

sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)

You will also need to change the code in the fourteenth line:

from tensorflow.compat.v1.keras import backend as K

Since Tensorflow 2.0, Keras does not expose sessions in the backend like before. You can get around that by using the compat.v1 API, but this is soon to be deprecated.



来源:https://stackoverflow.com/questions/61290368/typeerror-session-object-is-not-callable-error-running-sess-tf-compat-v1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!