I think the following should work since I am not finding any examples of how else to do this. Maybe the variable needs to be created in some kind of session or tf.function?
In [68]: keras.models.Model(inputs=[], outputs=K.variable(1.0, dtype=tf.float32))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-68-133bb7e0b07b> in <module>
----> 1 keras.models.Model(inputs=[], outputs=K.variable(1.0, dtype=tf.float32))
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in __init__(self, *args, **kwargs)
120
121 def __init__(self, *args, **kwargs):
--> 122 super(Model, self).__init__(*args, **kwargs)
123 # initializing _distribution_strategy here since it is possible to call
124 # predict on a model without compiling it.
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py in __init__(self, *args, **kwargs)
136 'inputs' in kwargs and 'outputs' in kwargs):
137 # Graph network
--> 138 self._init_graph_network(*args, **kwargs)
139 else:
140 # Subclassed network
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
454 self._setattr_tracking = False # pylint: disable=protected-access
455 try:
--> 456 result = method(self, *args, **kwargs)
457 finally:
458 self._setattr_tracking = previous_value # pylint: disable=protected-access
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
232
233 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs):
--> 234 base_layer_utils.create_keras_history(self._nested_outputs)
235
236 self._base_init(name=name)
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
249 operations and need to have Keras metadata assigned to them.
250 """
--> 251 _create_keras_history_helper(tensors, set())
252
253
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops)
272 if getattr(tensor, '_keras_history', None) is not None:
273 continue
--> 274 op = tensor.op # The Op that created this Tensor.
275 if op not in processed_ops:
276 # Recursively set `_keras_history`.
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py in op(self)
810 def op(self):
811 """The op for this variable."""
--> 812 return self._handle.op
813
814 @property
~/anaconda3/envs/37/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in op(self)
932 def op(self):
933 raise AttributeError(
--> 934 "Tensor.op is meaningless when eager execution is enabled.")
935
936 @property
AttributeError: Tensor.op is meaningless when eager execution is enabled.
Are you able to execute your code in TF 1.X ? Also can you share a standalone code snippet to reproduce the error reported? Thanks!
Right now only tf.keras supports eager execution.
The above is stand alone. I think the issue is that the variable needs to be created inside a custom layer and then added using add_weight. Even then it is a bit of a hack to get the dimensions right as I think you always need to provide an input.
Closing this issue since @robieta 's comment addresses the issue reported. Feel free to reopen if have any further problems. Thanks!
Right now only
tf.kerassupports eager execution.
@robieta I'm getting the same error even when using tf.keras
`
tf.keras.models.Model(inputs=[], outputs=tf.Variable(1.0, dtype=tf.float32))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2835a379fbff> in <module>
----> 1 tf.keras.models.Model(inputs=[], outputs=tf.Variable(1.0, dtype=tf.float32))
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\keras\engine\training.py in __init__(self, *args, **kwargs)
128
129 def __init__(self, *args, **kwargs):
--> 130 super(Model, self).__init__(*args, **kwargs)
131 # initializing _distribution_strategy here since it is possible to call
132 # predict on a model without compiling it.
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\keras\engine\network.py in __init__(self, *args, **kwargs)
160 'inputs' in kwargs and 'outputs' in kwargs):
161 # Graph network
--> 162 self._init_graph_network(*args, **kwargs)
163 else:
164 # Subclassed network
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\keras\engine\network.py in _init_graph_network(self, inputs, outputs, name, **kwargs)
267
268 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs):
--> 269 base_layer_utils.create_keras_history(self._nested_outputs)
270
271 self._base_init(name=name, **kwargs)
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py in create_keras_history(tensors)
198 keras_tensors: The Tensors found that came from a Keras Layer.
199 """
--> 200 _, created_layers = _create_keras_history_helper(tensors, set(), [])
201 return created_layers
202
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\keras\engine\base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
224 if getattr(tensor, '_keras_history', None) is not None:
225 continue
--> 226 op = tensor.op # The Op that created this Tensor.
227 if op not in processed_ops:
228 # Recursively set `_keras_history`.
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py in op(self)
884 def op(self):
885 """The op for this variable."""
--> 886 return self._handle.op
887
888 @property
~\AppData\Local\Continuum\anaconda3\envs\tf_daily\lib\site-packages\tensorflow\python\framework\ops.py in op(self)
977 def op(self):
978 raise AttributeError(
--> 979 "Tensor.op is meaningless when eager execution is enabled.")
980
981 @property
AttributeError: Tensor.op is meaningless when eager execution is enabled.
I am getting the same error ...
That error still exists in 2.0 version.
I met the same error when using keras.models.Model()
tensorflow 2.0.0
keras 2.3.1
tf.keras 2.2.4-tf
Most helpful comment
@robieta I'm getting the same error even when using
tf.keras`