Saving this Keras model blows up - python

Consider the following program:
import tensorflow as tf
from tensorflow.linalg import matmul
import tensorflow.keras as tfk
import tensorflow.keras.backend as K
import numpy as np
class MinimalRNNCell(tfk.layers.Layer):
def __init__(self, units, **kwargs):
self.units = units
self.state_size = units
super(MinimalRNNCell, self).__init__(**kwargs)
def build(self, input_shape):
self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
initializer='uniform',
name='kernel')
self.recurrent_kernel = self.add_weight(
shape=(self.units, self.units),
initializer='uniform',
name='recurrent_kernel')
self.built = True
def call(self, inputs, states=None, constants=None, *args, **kwargs):
prev_output = states[0]
print("constants: ", constants[0].name)
h = matmul(inputs, self.kernel) + constants[0]
output = h + matmul(prev_output, self.recurrent_kernel)
return output, [output]
def get_config(self):
return dict(super().get_config(), **{'units': self.units})
cell = MinimalRNNCell(32)
x = tfk.Input((None, 5), name='x')
z = tfk.Input((1,), name='z')
layer = tfk.layers.RNN(cell, name='rnn')
y = layer(x, constants=[z])
model = tfk.Model(inputs=[x, z], outputs=[y])
model.compile(optimizer='adam', loss='mse')
model.predict([np.array([[[0,0,0,0,0]]]), np.array([[0]])])
model.save('tmp.model')
Everything works until saving, at which point it blows up:
constants: z:0
constants: z:0
constants: constants:0
Traceback (most recent call last):
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1880, in _create_c_op
c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 32 and 5 for '{{node add/add}} = AddV2[T=DT_FLOAT](MatMul, constants)' with input shapes: [?,32], [?,?,5].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1245, in binary_op_wrapper
out = r_op(x)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1267, in r_binary_op_wrapper
return func(x, y, name=name)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1565, in _add_dispatch
return gen_math_ops.add_v2(x, y, name=name)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py", line 532, in add_v2
_, _, _op, _outputs = _op_def_library._apply_op_helper(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 748, in _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 599, in _create_op_internal
return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3557, in _create_op_internal
ret = Operation(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 2041, in __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1883, in _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 32 and 5 for '{{node add/add}} = AddV2[T=DT_FLOAT](MatMul, constants)' with input shapes: [?,32], [?,?,5].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "example.py", line 43, in <module>
model.save('tmp.model')
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 2111, in save
save.save_model(self, filepath, overwrite, include_optimizer, save_format,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/save.py", line 150, in save_model
saved_model_save.save(model, filepath, overwrite, include_optimizer,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save.py", line 89, in save
saved_nodes, node_paths = save_lib.save_and_return_nodes(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/saved_model/save.py", line 1103, in save_and_return_nodes
_build_meta_graph(obj, signatures, options, meta_graph_def,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/saved_model/save.py", line 1290, in _build_meta_graph
return _build_meta_graph_impl(obj, signatures, options, meta_graph_def,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/saved_model/save.py", line 1207, in _build_meta_graph_impl
signatures = signature_serialization.find_function_to_export(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/saved_model/signature_serialization.py", line 99, in find_function_to_export
functions = saveable_view.list_functions(saveable_view.root)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/saved_model/save.py", line 154, in list_functions
obj_functions = obj._list_functions_for_serialization( # pylint: disable=protected-access File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 2713, in _list_functions_for_serialization
functions = super(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 3016, in _list_functions_for_serialization
return (self._trackable_saved_model_saver
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/base_serialization.py", line 92, in list_functions_for_serialization
fns = self.functions_to_serialize(serialization_cache)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 73, in functions_to_serialize
return (self._get_serialized_attributes(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 89, in _get_serialized_attributes
object_dict, function_dict = self._get_serialized_attributes_internal(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/model_serialization.py", line 53, in _get_serialized_attributes_internal
super(ModelSavedModelSaver, self)._get_serialized_attributes_internal(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 99, in _get_serialized_attributes_internal
functions = save_impl.wrap_layer_functions(self.obj, serialization_cache)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 154, in wrap_layer_functions
original_fns = _replace_child_layer_functions(layer, serialization_cache)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 284, in _replace_child_layer_functions
child_layer._trackable_saved_model_saver._get_serialized_attributes(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 89, in _get_serialized_attributes
object_dict, function_dict = self._get_serialized_attributes_internal(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 151, in _get_serialized_attributes_internal
super(RNNSavedModelSaver, self)._get_serialized_attributes_internal(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/layer_serialization.py", line 99, in _get_serialized_attributes_internal
functions = save_impl.wrap_layer_functions(self.obj, serialization_cache)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 204, in wrap_layer_functions
fn.get_concrete_function()
File "/usr/lib64/python3.8/contextlib.py", line 120, in __exit__
next(self.gen)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 367, in tracing_scope
fn.get_concrete_function(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 1367, in get_concrete_function
concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 1273, in _get_concrete_function_garbage_collected
self._initialize(args, kwargs, add_initializers_to=initializers)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 763, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3279, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 599, in wrapper
ret = method(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 165, in wrap_with_training_arg
return control_flow_util.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py", line 109, in smart_cond
return smart_module.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/smart_cond.py", line 56, in smart_cond
return false_fn()
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 167, in <lambda>
lambda: replace_training_and_call(False))
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 163, in replace_training_and_call
return wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 681, in call
return call_and_return_conditional_losses(inputs, *args, **kwargs)[0]
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 639, in __call__
return self.wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 763, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3279, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 599, in wrapper
ret = method(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 165, in wrap_with_training_arg
return control_flow_util.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py", line 109, in smart_cond
return smart_module.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/smart_cond.py", line 56, in smart_cond
return false_fn()
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 167, in <lambda>
lambda: replace_training_and_call(False))
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 163, in replace_training_and_call
return wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 663, in call_and_return_conditional_losses
call_output = layer_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py", line 802, in call
last_output, outputs, states = backend.rnn(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/backend.py", line 4377, in rnn
output_time_zero, _ = step_function(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py", line 789, in step
output, new_states = cell_call_fn(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1030, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 69, in return_outputs_and_add_losses
outputs, losses = fn(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 165, in wrap_with_training_arg
return control_flow_util.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py", line 109, in smart_cond
return smart_module.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/smart_cond.py", line 56, in smart_cond
return false_fn()
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 167, in <lambda>
lambda: replace_training_and_call(False))
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 163, in replace_training_and_call
return wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 639, in __call__
return self.wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 763, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/function.py", line 3279, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 599, in wrapper
ret = method(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 165, in wrap_with_training_arg
return control_flow_util.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py", line 109, in smart_cond
return smart_module.smart_cond(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/smart_cond.py", line 56, in smart_cond
return false_fn()
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 167, in <lambda>
lambda: replace_training_and_call(False))
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/utils.py", line 163, in replace_training_and_call
return wrapped_call(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/save_impl.py", line 663, in call_and_return_conditional_losses
call_output = layer_call(*args, **kwargs)
File "example.py", line 27, in call
h = matmul(inputs, self.kernel) + constants[0]
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1250, in binary_op_wrapper
raise e
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1234, in binary_op_wrapper
return func(x, y, name=name)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 206, in wrapper
return target(*args, **kwargs)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/math_ops.py", line 1565, in _add_dispatch
return gen_math_ops.add_v2(x, y, name=name)
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py", line 532, in add_v2
_, _, _op, _outputs = _op_def_library._apply_op_helper(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 748, in _apply_op_helper
op = g._create_op_internal(op_type_name, inputs, dtypes=None,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 599, in _create_op_internal
return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3557, in _create_op_internal
ret = Operation(
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 2041, in __init__
self._c_op = _create_c_op(self._graph, node_def, inputs,
File "/home/itamarst/Devel/tensorflow/venv/lib64/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1883, in _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 32 and 5 for '{{node add}} = AddV2[T=DT_FLOAT](MatMul, constants)' with input shapes: [?,32], [?,?,5].
Any idea what's going on? Original code: https://github.com/tensorflow/tensorflow/issues/48213

Resolved by attaching
.h5 extension in TF 2.6
in while saving model file
import tensorflow as tf
from tensorflow.linalg import matmul
import tensorflow.keras as tfk
import tensorflow.keras.backend as K
import numpy as np
class MinimalRNNCell(tfk.layers.Layer):
def __init__(self, units, **kwargs):
self.units = units
self.state_size = units
super(MinimalRNNCell, self).__init__(**kwargs)
def build(self, input_shape):
self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
initializer='uniform',
name='kernel')
self.recurrent_kernel = self.add_weight(
shape=(self.units, self.units),
initializer='uniform',
name='recurrent_kernel')
self.built = True
def call(self, inputs, states=None, constants=None, *args, **kwargs):
prev_output = states[0]
print("constants: ", constants[0].name)
h = matmul(inputs, self.kernel) + constants[0]
output = h + matmul(prev_output, self.recurrent_kernel)
return output, [output]
def get_config(self):
return dict(super().get_config(), **{'units': self.units})
cell = MinimalRNNCell(32)
x = tfk.Input((None, 5), name='x')
z = tfk.Input((1,), name='z')
layer = tfk.layers.RNN(cell, name='rnn')
y = layer(x, constants=[z])
model = tfk.Model(inputs=[x, z], outputs=[y])
model.compile(optimizer='adam', loss='mse')
model.predict([np.array([[[0,0,0,0,0]]]), np.array([[0]])])
model.save('tmp.model.h5')

Related

InvalidArgumentError: Graph execution error:

I keep getting a "graph execution error" The complete code is in the link if you'd like to see it. I don't really understand the error at all. I was trying to get the epochs running but instead, I get "1/30" and then it just stops. I checked my folders and it appears that I have all jpeg files. I'm in a corner I don't know what to do.
history = model.fit_generator(train_generator,
epochs=30,
verbose=1,
validation_data=validation_generator,
callbacks = [best_model]
)
https://colab.research.google.com/drive/1hvHkDusyqEsdZg5ZRVhhriZrDagpFdU6?usp=sharing
Epoch 1/30
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-42-5368c251678d> in <module>
----> 1 history = model.fit_generator(train_generator,
2 epochs=30,
3 verbose=1,
4 validation_data=validation_generator,
5 callbacks = [best_model]
2 frames
/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
53 ctx.ensure_initialized()
---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
app.start()
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
self.io_loop.start()
File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
self._run_once()
File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
handle._run()
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
ret = callback()
File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
self.run()
File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
yielded = self.gen.send(value)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
self.do_execute(
File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
yielded = next(result)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
result = self._run_cell(
File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
return runner(coro)
File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-25-f51df55a1054>", line 1, in <module>
history = model.fit_generator(train_datagen.flow_from_directory(TRAINING_DIR,
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 2260, in fit_generator
return self.fit(
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 948, in compute_loss
return self.compiled_loss(
File "/usr/local/lib/python3.8/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 139, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 243, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.8/dist-packages/keras/losses.py", line 1787, in categorical_crossentropy
return backend.categorical_crossentropy(
File "/usr/local/lib/python3.8/dist-packages/keras/backend.py", line 5134, in categorical_crossentropy
return tf.nn.softmax_cross_entropy_with_logits(
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[16,5] labels_size=[16,11]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_1983]

model.fit gives me Graph execution error. How do I solve?

I am new to image processing and machine learning in python. I have been trying to execute a model in google colab using inceptionv3 but i am stuck at fitting the model.
r = model.fit(
training_set,
validation_data=test_set,
epochs=10,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
it is showing me the below errors
Epoch 1/10
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-24-c27d8fba63ce> in <module>()
6 epochs=10,
7 steps_per_epoch=len(training_set),
----> 8 validation_steps=len(test_set)
9 )
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnimplementedError: Graph execution error:
Detected at node 'model/conv2d/Conv2D' defined at (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
self.io_loop.start()
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
self._run_once()
File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
handle._run()
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 577, in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 606, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-20-8cba7706098f>", line 12, in <module>
validation_steps=validation_data
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1409, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 490, in __call__
return super().__call__(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 459, in call
inputs, training=training, mask=mask)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py", line 596, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/convolutional/base_conv.py", line 250, in call
outputs = self.convolution_op(inputs, self.kernel)
File "/usr/local/lib/python3.7/dist-packages/keras/layers/convolutional/base_conv.py", line 232, in convolution_op
name=self.__class__.__name__)
Node: 'model/conv2d/Conv2D'
DNN library is not found.
[[{{node model/conv2d/Conv2D}}]] [Op:__inference_train_function_12299]
the whole code is in my git repository: https://github.com/Aditya757/MyRepository.git
the image of the dataset is here: https://i.stack.imgur.com/jWaJ8.png
Try to truncate to max_length=64 when tokenization. It worked in my case when training the text classification model.
The error appears when I set max_lenght to 128 or above.

Invalid Argument Error: Graph Execution Error and Traceback Error

I'm having multiple errors while running this vanilla rnn training code (code and errors shown below). I don't know if its because of my dataset or is it something else.
The data is made up of opcodes in assembly language.
The size of the training data is (2000, 53203).
This is vanilla RNN training code
import numpy as np
X_test = data[:200]
Y_test = np.array(Y_data[:200])
X_train = data[200:]
Y_train = np.array(Y_data[200:])
from tensorflow.keras.layers import SimpleRNN, Embedding, Dense
from tensorflow.keras.models import Sequential
model = Sequential()
model.add(Embedding(len(word_to_index), 32)) # 임베딩 벡터의 차원은 32
model.add(SimpleRNN(32)) # RNN 셀의 hidden_size는 32
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
It's Error Code...
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_13744/877033043.py in <module>
8
9 model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
---> 10 history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
53 ctx.ensure_initialized()
---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
InvalidArgumentError: Graph execution error:
Detected at node 'sequential_3/embedding_2/embedding_lookup' defined at (most recent call last):
File "C:\Users\Administrator\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Administrator\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Users\Administrator\anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\Users\Administrator\anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Users\Administrator\anaconda3\lib\asyncio\base_events.py", line 596, in run_forever
self._run_once()
File "C:\Users\Administrator\anaconda3\lib\asyncio\base_events.py", line 1890, in _run_once
handle._run()
File "C:\Users\Administrator\anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 457, in dispatch_queue
await self.process_one()
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 446, in process_one
await dispatch(*args)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 353, in dispatch_shell
await result
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 648, in execute_request
reply_content = await reply_content
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 353, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\Administrator\anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2901, in run_cell
result = self._run_cell(
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2947, in _run_cell
return runner(coro)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3172, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3364, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_13744/877033043.py", line 10, in <module>
history = model.fit(X_train, Y_train, epochs=4, batch_size=64, validation_split=0.2)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1445, in fit
val_logs = self.evaluate(
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1756, in evaluate
tmp_logs = self.test_function(iterator)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1557, in test_function
return step_function(self, iterator)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1546, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1535, in run_step
outputs = model.test_step(data)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 1499, in test_step
y_pred = self(x, training=False)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\training.py", line 490, in __call__
return super().__call__(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\sequential.py", line 374, in call
return super(Sequential, self).call(inputs, training=training, mask=mask)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\functional.py", line 458, in call
return self._run_internal_graph(
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\functional.py", line 596, in _run_internal_graph
outputs = node.layer(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
outputs = call_fn(inputs, *args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
return fn(*args, **kwargs)
File "C:\Users\Administrator\anaconda3\lib\site-packages\keras\layers\core\embedding.py", line 199, in call
out = tf.nn.embedding_lookup(self.embeddings, inputs)
Node: 'sequential_3/embedding_2/embedding_lookup'
indices[25,40743] = 575 is not in [0, 575)
[[{{node sequential_3/embedding_2/embedding_lookup}}]] [Op:__inference_test_function_4239]
I'm running this on JupyterLab

How to initialize a kernel with a tensor

I have created a custom layer in keras, which simply perform a dot product between the input and a kernel. But for the kernel I wanted to use the mean of the batch as a kernel initialization, meaning taking the mean of the batch and producing a kernel which initial value is that mean. To do so I have created a custom kernel initializer as follow:
class Tensor_Init(Initializer):
"""Initializer that generates tensors initialized to a given tensor.
# Arguments
Tensor: the generator tensors.
"""
def __init__(self, Tensor=None):
self.Tensor = Tensor
def __call__(self, shape, dtype=None):
return tf.Variable(self.Tensor)
def get_config(self):
return {'Tensor': self.Tensor}
This is the call method of the custom layer in keras. I simply compute the mean of the batch and use it with the above initializer class to produce a kernel. I use it as follow in the custom layer
def call(self, inputs):
data_format = conv_utils.convert_data_format(self.data_format, self.rank + 2)
inputs = tf.extract_image_patches(
inputs,
ksizes=(1,) + self.kernel_size + (1,),
strides=(1,) + self.strides + (1,),
rates=(1,) + self.dilation_rate + (1,),
padding=self.padding.upper(),
)
inputs = K.reshape(inputs,[-1,inputs.get_shape().as_list()[1],inputs.get_shape().as_list()
[2],self.kernel_size[0]*self.kernel_size[1] ,self.output_dim])
self.kernel = self.add_weight(name='kernel',shape=(),initializer=Tensor_Init(Tensor=tf.reduce_mean(inputs, 0)),trainable=True)
outputs = (tf.einsum('NHWKC,HWKC->NHWC',inputs,self.kernel)+self.c)**self.p
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, (0, 3, 1, 2))
return outputs
Th e model is created and compiled normaly but I start training I am getting this error
InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,48,48,3]
[[node conv2d_1_input (defined at C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py:736) ]]
Original stack trace for 'conv2d_1_input':
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\kernelapp.py", line 563, in start
self.io_loop.start()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\platform\asyncio.py", line 148, in start
self.asyncio_loop.run_forever()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\asyncio\base_events.py", line 438, in run_forever
self._run_once()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\asyncio\base_events.py", line 1451, in _run_once
handle._run()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\asyncio\events.py", line 145, in _run
self._callback(*self._args)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\ioloop.py", line 690, in <lambda>
lambda f: self._run_callback(functools.partial(callback, future))
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\ioloop.py", line 743, in _run_callback
ret = callback()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 787, in inner
self.run()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 748, in run
yielded = self.gen.send(value)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 378, in dispatch_queue
yield self.process_one()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 225, in wrapper
runner = Runner(result, future, yielded)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 714, in __init__
self.run()
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 748, in run
yielded = self.gen.send(value)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 365, in process_one
yield gen.maybe_future(dispatch(*args))
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 272, in dispatch_shell
yield gen.maybe_future(handler(stream, idents, msg))
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\kernelbase.py", line 542, in execute_request
user_expressions, allow_stdin,
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tornado\gen.py", line 209, in wrapper
yielded = next(result)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\ipkernel.py", line 294, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\ipykernel\zmqshell.py", line 536, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2855, in run_cell
raw_cell, store_history, silent, shell_futures)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in _run_cell
return runner(coro)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\async_helpers.py", line 68, in _pseudo_sync_runner
coro.send(None)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3058, in run_cell_async
interactivity=interactivity, compiler=compiler, result=result)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3249, in run_ast_nodes
if (await self.run_code(code, result, async_=asy)):
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-35eda01d200a>", line 75, in <module>
model = create_vgg16()
File "<ipython-input-2-35eda01d200a>", line 12, in create_vgg16
model.add(Conv2D(64, (5, 5), input_shape=(48,48,3), padding='same'))
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\sequential.py", line 162, in add
name=layer.name + '_input')
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\input_layer.py", line 178, in Input
input_tensor=tensor)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\input_layer.py", line 87, in __init__
name=self.name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\backend\tensorflow_backend.py", line 736, in placeholder
shape=shape, ndim=ndim, dtype=dtype, sparse=sparse, name=name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\backend.py", line 998, in placeholder
x = array_ops.placeholder(dtype, shape=shape, name=name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2143, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7401, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in create_op
op_def=op_def)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 2005, in __init__
self._traceback = tf_stack.extract_stack()
I was able to pass the mean of the batch to the kernel by simply creating a zero initialized kernel then assigning the mean value to it, without even creating a custom initilizer. I modified the custom layer as follow
def call(self, inputs):
data_format = conv_utils.convert_data_format(self.data_format, self.rank + 2)
inputs = tf.extract_image_patches(
inputs,
ksizes=(1,) + self.kernel_size + (1,),
strides=(1,) + self.strides + (1,),
rates=(1,) + self.dilation_rate + (1,),
padding=self.padding.upper(),
)
inputs = K.reshape(inputs,[-1,inputs.get_shape().as_list()[1],inputs.get_shape().as_list()
[2],self.kernel_size[0]*self.kernel_size[1] ,self.output_dim])
weights = tf.reduce_mean(inputs, 0)
self.kernel = self.add_weight(name='kernel',
shape=(weights.get_shape().as_list()[0],weights.get_shape().as_list()
[1],weights.get_shape().as_list()[2],weights.get_shape().as_list()[3]),
initializer='zeros',
trainable=True)
tf.compat.v1.assign(self.kernel, weights)
outputs = (tf.einsum('NHWKC,HWKC->NHWC',inputs,self.kernel)+self.c)**self.p
if self.data_format == 'channels_first':
outputs = K.permute_dimensions(outputs, (0, 3, 1, 2))
return outputs

Odd Dimension() object in shape of input?

I am experiencing issues with Tensorflow. I've narrowed the issues down to the format of my input shape of the build() method of a custom Attention layer in tf-keras. I am getting a list of Dimension() objects instead of the actual input-shape. Please help.
Attention Layer
x_attn = []
for i in range(self.attn_range): #4
x_attn.append(Attention()(x))
print("DEBUG:00001")
x = L.Concatenate(-1)(x_attn)
Custom Attention Layer build() method:
def build(self,
input_shape):
print("DEBUG:00005")
params_shape = list(input_shape[1:])
print("DEBUG:00007", params_shape)
self.query_weights = self.add_weight(
name='q_weights',
shape=params_shape,
initializer=self.q_weights_init
)
print("DEBUG:00006")
self.key_weights = self.add_weight(
name='key_weights',
shape=params_shape,
initializer=self.key_weights_init
)
self.val_weights = self.add_weight(
name='val_weights',
shape=params_shape,
initializer=self.value_weights_init
)
Output of param shape debug print function is:
DEBUG:00007 [Dimension(10), Dimension(5), Dimension(128)]
Edit:
Full Error Message as Requested:
Traceback (most recent call last):
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 558, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 558, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got Dimension(10)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Empowerment\.py", line 327, in <module>
env_actor = ComputerEnv()
File "Empowerment\.py", line 225, in __init__
self.dqn = ICDQNAgent(self.state_size + (3,), self.state_size[0], 4)
File "Empowerment\.py", line 78, in __init__
self.model, self.autoencoder, self.critic = self.build_model()
File "Empowerment\.py", line 99, in build_model
x_attn.append(Attention()(x))
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 591, in __call__
self._maybe_build(inputs)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1881, in _maybe_build
self.build(input_shapes)
File "/home/ai/Desktop/ai_proj/layers.py", line 69, in build
initializer=self.q_weights_init
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 384, in add_weight
aggregation=aggregation)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 663, in _add_variable_with_custom_getter
**kwargs_for_getter)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 155, in make_variable
shape=variable_shape if variable_shape.rank else None)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 259, in __call__
return cls._variable_v1_call(*args, **kwargs)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 220, in _variable_v1_call
shape=shape)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 198, in <lambda>
previous_getter = lambda **kwargs: default_variable_creator(None, **kwargs)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/variable_scope.py", line 2495, in default_variable_creator
shape=shape)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/variables.py", line 263, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 460, in __init__
shape=shape)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 604, in _init_from_args
initial_value() if init_from_fn else initial_value,
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 135, in <lambda>
init_val = lambda: initializer(shape, dtype=dtype)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/init_ops.py", line 533, in __call__
shape, -limit, limit, dtype, seed=self.seed)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/random_ops.py", line 239, in random_uniform
shape = _ShapeTensor(shape)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/random_ops.py", line 44, in _ShapeTensor
return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1087, in convert_to_tensor
return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1145, in convert_to_tensor_v2
as_ref=False)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 305, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 246, in constant
allow_broadcast=True)
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/constant_op.py", line 284, in _constant_impl
allow_broadcast=allow_broadcast))
File "/home/ai/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/tensor_util.py", line 562, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [Dimension(10), Dimension(5), Dimension(128)]. Consider casting elements to a supported type.

Categories