AttributeError:“模块”对象没有属性“占位符”,如何解决此问题?

时间:2019-06-19 08:11:48

标签: python python-2.7 tensorflow keras

当我运行代码时,该错误消息就会出现。

我正在使用python 2.7.5,keras 2.2.4和tensorflow 2.0.0-beta1。 有人告诉我“不要创建文件名tensorflow”,但我不是那样做的。 我认为这可能是由升级的tensorflow版本中的更改项引起的, https://www.tensorflow.org/beta/guide/effective_tf2 ,但较低版本也无法运行,错误为“ ValueError:错误的元数据”。 所有进程都在虚拟环境上运行。

错误消息是

Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W0619 16:47:38.715425 139651914356544 deprecation.py:323] From /home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/tensorflow/python/compat/v2_compat.py:65: 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 "./gpd_predict.py", line 165, in <module>
    model = model_from_json(loaded_model_json, custom_objects={'tf':tf})
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/engine/saving.py", line 622, in model_from_json
    return deserialize(config, custom_objects=custom_objects)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/layers/__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/engine/network.py", line 1043, in from_config
    process_layer(layer_data)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/engine/network.py", line 1029, in process_layer
    custom_objects=custom_objects)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/layers/__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 149, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/engine/base_layer.py", line 1103, in from_config
    return cls(**config)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "/home/sea/Programs/generalized-phase-detection/generalized-phase-detection-master/venv/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 529, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
AttributeError: 'module' object has no attribute 'placeholder'

我不知道这是否是问题行,但这行只是我在错误消息中的代码。 (最后一行)

# load json and create model
    json_file = open('model_pol.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    model = model_from_json(loaded_model_json, custom_objects={'tf':tf})

如何在没有错误代码的情况下运行程序?

3 个答案:

答案 0 :(得分:0)

当前的Keras与TensorFlow 2.0不兼容(该版本尚未发布稳定版,仅作为beta版本),因此请不要尝试将tf 2.0与官方keras一起使用(使用像1.13这样的稳定​​tensorflow),或使用{ {1}}来自tensorflow 2.0。

答案 1 :(得分:0)

Tensorflow 2默认情况下不使用占位符,因此如果不使用兼容模式,它将不可用。

可以

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

但是,实际上,您可能希望将代码迁移到最佳utelise v2。在此处查看官方指南:https://www.tensorflow.org/beta/guide/migration_guide

答案 2 :(得分:0)

以下代码片段强制安装tensorflow版本1.x并提供兼容性。

%tensorflow_version 1.x 
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()