首页 > 解决方案 > ImportError:无法从“tensorflow”导入名称“Session”

问题描述

我正在努力运行模块。

from tensorflow import Session, ConfigProto, GPUOptions
gpuoptions = GPUOptions(allow_growth=True)
session = Session(config=ConfigProto(gpu_options=gpuoptions))
K.set_session(session)
classifier = Sequential()

我不知道为什么它不起作用。

它只是告诉我:

ImportError: cannot import name 'Session' from 'tensorflow' (C:\Users\hayou\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py)

标签: pythontensorflow

解决方案


我猜你使用的是 TensorFlow 2.x。在这种情况下,请改用 tf.compat.v1.--function--() 。

import tensorflow as tf
gpuoptions = tf.compat.v1.GPUOptions(allow_growth=True)
session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpuoptions))
K.set_session(session)
classifier = Sequential()

推荐阅读