I get an error when fitting the model with Keras model.fit()
:
Traceback (most recent call last):
File "C:\Users\username\Documents\FAE\Maths\Code\variational auto encoders\continuous text generation\variational_autoencoder_text_gen_V2\vae_train.py", line 87, in <module>
vae.fit([x, x_decoder], x, epochs=epochs, verbose=1)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py", line 1184, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 885, in __call__
result = self._call(*args, **kwds)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 759, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3066, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3463, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\function.py", line 3298, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1007, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\eager\def_function.py", line 668, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\func_graph.py", line 994, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:853 train_function *
return step_function(self, iterator)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:835 run_step **
outputs = model.train_step(data)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\training.py:788 train_step
loss = self.compiled_loss(
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\compile_utils.py:239 __call__
self._loss_metric.update_state(
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\utils\metrics_utils.py:73 decorated
update_op = update_state_fn(*args, **kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\metrics.py:177 update_state_fn
return ag_update_state(*args, **kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\metrics.py:409 update_state **
sample_weight = tf.__internal__.ops.broadcast_weights(
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\ops\weights_broadcast_ops.py:157 broadcast_weights
values = ops.convert_to_tensor(values, name="values")
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\profiler\trace.py:163 wrapped
return func(*args, **kwargs)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py:1566 convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py:346 _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py:271 constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py:288 _constant_impl
tensor_util.make_tensor_proto(
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\tensor_util.py:435 make_tensor_proto
values = np.asarray(values)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\numpy\core\_asarray.py:83 asarray
return array(a, dtype, copy=False, order=order)
C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\keras_tensor.py:244 __array__
raise TypeError(
TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.
Here the code for data preparation
from gensim.utils import tokenize
from lstm_vae import create_lstm_vae, inference
def get_text_data(data_path, num_samples=1000):
# vectorize the data
input_texts = []
input_characters = set(["\t"])
with open(data_path, "r", encoding="utf-8") as f:
lines = f.read().lower().split("\n")
for line in lines[: min(num_samples, len(lines) - 1)]:
input_text = line.split("\t")[0]
# input_text = word_tokenize(input_text) # using nltk
input_text = list ( tokenize (input_text) ) # using gensim
input_text.append("<end>")
input_texts.append(input_text)
for char in input_text:
if char not in input_characters:
input_characters.add(char)
input_characters = sorted(list(input_characters))
num_encoder_tokens = len(input_characters)
max_encoder_seq_length = max([len(txt) for txt in input_texts]) + 1
print("Number of samples:", len(input_texts))
print("Number of unique input tokens:", num_encoder_tokens)
print("Max sequence length for inputs:", max_encoder_seq_length)
input_token_index = dict([(char, i) for i, char in enumerate(input_characters)])
reverse_input_char_index = dict((i, char) for char, i in input_token_index.items())
encoder_input_data = np.zeros((len(input_texts), max_encoder_seq_length, num_encoder_tokens), dtype="float32")
decoder_input_data = np.zeros((len(input_texts), max_encoder_seq_length, num_encoder_tokens), dtype="float32")
for i, input_text in enumerate(input_texts):
decoder_input_data[i, 0, input_token_index["\t"]] = 1.0
for t, char in enumerate(input_text):
encoder_input_data[i, t, input_token_index[char]] = 1.0
decoder_input_data[i, t + 1, input_token_index[char]] = 1.0
return max_encoder_seq_length, num_encoder_tokens, input_characters, input_token_index, reverse_input_char_index, \
encoder_input_data, decoder_input_data
and the main
code
if __name__ == "__main__":
timesteps_max, enc_tokens, characters, char2id, id2char, x, x_decoder = get_text_data(num_samples=3000,
data_path="data/fra.txt")
print ( x[0] )
print(x.shape, "Creating model...")
input_dim = x.shape[-1]
timesteps = x.shape[-2]
batch_size = 1
latent_dim = 191
intermediate_dim = 353
epochs = 40
vae, enc, gen, stepper = create_lstm_vae(input_dim,
batch_size=batch_size,
intermediate_dim=intermediate_dim,
latent_dim=latent_dim)
print("Training model...")
vae.fit([x, x_decoder], x, epochs=epochs, verbose=1)
So, the last line is producing the error.
Data inspection, when i do
print("Number of samples:", len(input_texts))
print("Number of unique input tokens:", num_encoder_tokens)
print("Max sequence length for inputs:", max_encoder_seq_length)
print ( x[0] )
gives
Number of samples: 3000
Number of unique input tokens: 742
Max sequence length for inputs: 6
[[0. 0. 0. ... 0. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
Disabling eager execution as suggested in Tensorflow Model fit format data correctly -- TypeError: Cannot convert a symbolic Keras input/output to a numpy array enable to execute the fit function, however i fear it is not the proper way. Maybe related to the way the custom loss is defined ?
def vae_loss(x, x_decoded_onehot):
# xent_loss = objectives.categorical_crossentropy(x, x_decoded_onehot) # seems deprecated
xent_loss = losses.CategoricalCrossentropy()(x, x_decoded_onehot)
kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma))
loss = xent_loss + kl_loss
return loss
What would you suggest ?