首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用TF2.2执行TF1.X代码会导致错误

使用TF2.2执行TF1.X代码会导致错误
EN

Stack Overflow用户
提问于 2020-12-29 19:42:38
回答 1查看 402关注 0票数 0

如何在Tensorflow 2.2中修复以下错误?我更喜欢将代码转换为与TF2.2兼容的代码,如果可能的话,不要使用紧凑的版本。

代码语言:javascript
复制
AttributeError: module 'tensorflow' has no attribute 'placeholder'

代码语言:javascript
复制
[3306:3298 0:1022] 01:57:24 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ python train.py 
Traceback (most recent call last):
  File "train.py", line 260, in <module>
    train()
  File "train.py", line 96, in train
    pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
  File "/home/mona/research/code/DJ-RN/pointnet/models/pointnet_cls.py", line 13, in placeholder_inputs
    pointclouds_pl = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
AttributeError: module 'tensorflow' has no attribute 'placeholder'

也是

代码语言:javascript
复制
[3306:3298 0:1023] 01:57:31 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'2.2.0'
>>> quit()
12149/31772MB
[3306:3298 0:1024] 01:59:05 Tue Dec 29 [mona@goku:pts/0 +1] ~/research/code/DJ-RN/pointnet
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
12149/31772MB

$ lsb_release -a
LSB Version:    core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.1 LTS
Release:    20.04
Codename:   focal

如下所示,占位符不是一种方法:

代码语言:javascript
复制
>>> tf.compat.v1.summary.
tf.compat.v1.summary.Event(                    tf.compat.v1.summary.get_summary_description(
tf.compat.v1.summary.FileWriter(               tf.compat.v1.summary.histogram(
tf.compat.v1.summary.FileWriterCache(          tf.compat.v1.summary.image(
tf.compat.v1.summary.SessionLog(               tf.compat.v1.summary.initialize(
tf.compat.v1.summary.Summary(                  tf.compat.v1.summary.merge(
tf.compat.v1.summary.SummaryDescription(       tf.compat.v1.summary.merge_all(
tf.compat.v1.summary.TaggedRunMetadata(        tf.compat.v1.summary.scalar(
tf.compat.v1.summary.all_v2_summary_ops(       tf.compat.v1.summary.tensor_summary(
tf.compat.v1.summary.audio(                    tf.compat.v1.summary.text(

正如论坛和git问题中提到的,我也尝试过以下导入,但是它不起作用(它也在用于代码迁移的官方tensorflow文档:https://www.tensorflow.org/guide/migrate中):

代码语言:javascript
复制
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

这是我得到的错误:

代码语言:javascript
复制
$ python train.py 
WARNING:tensorflow:From /home/mona/anaconda3/lib/python3.8/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
Traceback (most recent call last):
  File "train.py", line 260, in <module>
    train()
  File "train.py", line 96, in train
    pointclouds_pl, labels_pl = MODEL.placeholder_inputs(BATCH_SIZE, NUM_POINT)
  File "/home/mona/research/code/DJ-RN/pointnet/models/pointnet_cls.py", line 15, in placeholder_inputs
    pointclouds_pl = tf.compact.v1.placeholder(tf.float32, shape=(batch_size, num_point, 3))
AttributeError: module 'tensorflow.compat.v1' has no attribute 'compact'

代码驻留在这个repo:https://github.com/charlesq34/pointnet/issues/265中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-29 21:12:01

代码语言:javascript
复制
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

而不是

代码语言:javascript
复制
import tensorflow as tf

然后用它就像:

代码语言:javascript
复制
tf.placeholder()

例如,

代码语言:javascript
复制
pointclouds_ph = tf.placeholder(tf.float32, shape=(batch_size, point_num, 3))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65497808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档