“类型错误:‘会话’对象不可调用";错误运行 sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf) [英] "TypeError: 'Session' object is not callable" error running sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)

查看:31
本文介绍了“类型错误:‘会话’对象不可调用";错误运行 sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置种子并配置 keras 设置,以确保我的实验可重现.当我运行以下命令时(基于 这个问题):

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)

...发生错误:

TypeError: 'Session' 对象不可调用

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?

我在 Mac 上的 Jupyter Notebook 中运行 tensorflow 2.1.0 版.

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

推荐答案

在倒数第二行,您可能希望使用参数 graph 构造一个 Session 对象和config,而不是调用一个Session.

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

改变这个:

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

为此:

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

从 Tensorflow 2.0 开始,Keras 不再像以前那样在后端公开会话.您可以通过使用 compat.v1 API 来解决这个问题,但这很快就会被弃用.

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.

这篇关于“类型错误:‘会话’对象不可调用";错误运行 sess = tf.compat.v1.Session()(graph=tf.compat.v1.get_default_graph(), config=session_conf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆