module 'tensorflow' has no attribute 'absolute_import'

本文详细解析了在使用TensorFlow过程中遇到的版本不兼容问题,提供了具体的修改代码示例,帮助读者理解并解决类似错误,确保代码在不同版本间顺利运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image
这个error猝不及防,在查看了tensorflow API的版本后,做了如下更改

# from tensorflow import *
from tensorflow.python import *

module ‘XXX’ has no attribute ‘XXX’
解决方案:(1)module未导入成功;(2)版本容错问题。

error分析原因
本人环境是tensorflow 1.10
因为有朋友在tensorflow1.3版本上并无此错误,故直接查看了1.3版本与出错的init.py之间的区别,即做出了上述尝试,成功。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# pylint: disable=wildcard-import
from tensorflow.python import *  # 出错参考点
# pylint: enable=wildcard-import

from tensorflow.python.util.lazy_loader import LazyLoader
contrib = LazyLoader('contrib', globals(), 'tensorflow.contrib')
del LazyLoader

del absolute_import
del division
del print_function

# These symbols appear because we import the python package which
# in turn imports from tensorflow.core and tensorflow.python. They
# must come from this module. So python adds these symbols for the
# resolution to succeed.
# pylint: disable=undefined-variable
del python
del core

对比出错init

from __future__ import absolute_import
from tensorflow import *  # 与上面版本的init有出入

from .sg_util import sg_opt
from .sg_main import *
from .sg_logging import *
from .sg_train import *
from .sg_queue import sg_producer_func
from .sg_layer import sg_emb

from . import sg_optimize
from . import sg_data
from . import sg_initializer

from ._version import __version__


__author__ = 'namju.kim@kakaocorp.com'


#
# augmenting tensorflow Variable and Tensor type by injecting custom methods
#

modules = ['sg_activation', 'sg_metric', 'sg_layer', 'sg_loss', 'sg_transform', 'sg_net']
for mod in modules:
    sg_inject(__path__[0], mod)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值