Ad

AttributeError: 'module' Object Has No Attribute 'placeholder', How Can I Resolve This Problem?

- 1 answer

When I run the code, that error message raise.

I'm using python 2.7.5, keras 2.2.4 and tensorflow 2.0.0-beta1. Somebody told me 'don't make file name tensorflow', but I didn't make like that. I think it might be occurred by changed items that is in upgraded tensorflow version, https://www.tensorflow.org/beta/guide/effective_tf2 , but the lower version also can't run with error that is "ValueError:bad marshal data". All the processes are running on virtual environment.

Error message is

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'

I don't know if this is a problem line, but this line is only my code in the error message. (last line)

# 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})

How can I run the program without error code?

Ad

Answer

Tensorflow 2 is does not use placeholders by default so it is not available without using compatibility mode.

You could do

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

but, really, you probably want to migrate your code to best utelise v2. Check out the official guide here: https://www.tensorflow.org/beta/guide/migration_guide

Ad
source: stackoverflow.com
Ad