python - 使用 TF2.2 执行 TF1.X 代码会导致错误
问题描述
如何修复 Tensorflow 2.2 中的以下错误?如果可能的话,我更喜欢将代码转换为与 TF2.2 兼容的代码,而不是使用紧凑版本。
AttributeError: module 'tensorflow' has no attribute 'placeholder'
[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'
还
[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
如下所示,占位符不是一种方法:
>>> 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 ):
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这是我得到的错误:
$ 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
解决方案
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
代替
import tensorflow as tf
然后像这样使用它:
tf.placeholder()
例如,
pointclouds_ph = tf.placeholder(tf.float32, shape=(batch_size, point_num, 3))
推荐阅读
- node.js - 如何根据multer中的请求有条件地使用memoryStorage和multer-S3?
- python - 将列标题转换为顶行
- c# - 如何在 .net 表单应用程序中放置命令行界面?
- sql - SQL 字符串列 - 查找字符串中括号内的任何整数
- java - 子图的克隆
- game-engine - 如何在godot引擎中添加到Line2d的各个点上添加计时器?
- c++ - gcc/g++ 错误“隐式声明”构造函数被“弃用”是什么意思?
- scala - Scala中的无限惰性列表
- bash - 自动输入要求的密码
- flutter - dart或flutter中如何将请求参数作为formdata传递