New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tf.keras multi input models don't work when using tf.data.Dataset #20698
Comments
I have the same problem and I have also multiple input dataset. But not sure if this problem caused by the multiple input datset. And I am using tensorflow 1.9 In order to be able to use dataset iterator in model.fit So
I got : 2- If I do the following : I got : 3- If I do the following : I got : Note: |
I opened #20753 to fix the issues not related to multi input models. |
I could reproduce the error. |
Thanks for taking the time and reproducing it. Did you have a chance to checked out my fix in #20753? |
Theres also a related PR that adds support for using tuples as multi dim inputs: #20136 |
My situation seems similar. The iterator of dataset fed to xy_ds = (
tf.data.Dataset.zip((audio_ds, label_ds))
.batch(
batch_size=batch_size,
# drop_remainder=True if is_training else False
)
.repeat(repeat)
.prefetch(tf.contrib.data.AUTOTUNE)
) Both tf.data.TextLineDataset(id_path)
.map(load_audio, num_parallel_calls=N_READ_THREAD) Before fed to the model, its iterator is created. tr_iterator = tr_set.make_one_shot_iterator()
tr_iterator.get_next()
(<tf.Tensor 'IteratorGetNext:0' shape=<unknown> dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=<unknown> dtype=float32>) And this is the error message when File "data_io.py", line 127, in <module>
model.fit(
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 950, in fit
batch_size=batch_size)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 749, in _standardize_user_data
exception_prefix='input')
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py", line 91, in standardize_input_data
data = [standardize_single_array(x) for x in data]
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py", line 91, in <listcomp>
data = [standardize_single_array(x) for x in data]
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py", line 26, in standardize_single_array
elif x.ndim == 1:
AttributeError: 'Iterator' object has no attribute 'ndim' tensorflow: 1.9.0, keras:2.2.2 |
I think I discovered the problem fro my situation. The problem was I am using the standalone |
@was84san Wow, same here, and now it seems solved. Thanks! |
@lgeiger is this issue of passing multiple input to keras model vi tf.dataset api fixed? |
Hi, @was84san. |
Which problem exactly!, feeding multiple inputs, or feeding the iterator directly to model.fit. I figure out only the last one. |
@hhwxxx I was also unable to use |
@gabrielibagon Could you post a snippet how you got a nested dataset iterator with multiple inputs working? |
The final example at here is interesting:
now, how to define a model that accepts and trains correctly with that datase? Is the full example available somewhere? |
@JanRuettinger @ricoms Sorry for the delayed response. I drafted up a toy example using MNIST in order to train a model with two inputs and two outputs. The model is simply two identical models fused together, which takes in two copies of the MNIST data (two inputs) and outputs a prediction for each (two outputs). You can adapt this to more complex models and input pipelines. Note: This is still using
Update: As @jashshopin mentions below, the |
Is it necessary to use |
@jashshopin Thanks for pointing that out, apparently you can pass the zipped |
thanks, @gabrielibagon. I have something like that here., although I used the keras generator format because I could not deal with a video input pipeline using tensorflow methods. I might refactor it to tf.Dataset someday but it's working for now. :) |
Nagging Assignee @fchollet: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly. |
This isn't an issue on tensorflow 1.12 and above anymore. Thanks for the help everybody. |
@ Igeiger, I tried to pass multiple inputs as a list of tf.dataset api to model fit directly, like this then I got this error
And this is with tensorflow 1.12, so how you can pass multiple input using tf.dataset api with model fit not with model.fit_generator? |
Returning a list of tensors in a single dataset and then passing it to |
@lgeiger what about using dictionaries as targets? #25299 (comment) |
I can confirm this works in tensorflow 2.0.0-rc0. Multiple input and output, even without all the zipping:
|
This still seems broken to me (in tensorflow 2.0.0-rc0). See this snippet: import tensorflow as tf
from tensorflow import keras
inputs = [keras.Input((1,), name="a"), keras.Input((1,), name="b")]
outputs = inputs[0] + inputs[1]
model = keras.Model(inputs=inputs, outputs=outputs)
list_input = [tf.zeros((10, 1)), tf.ones((10, 1))]
dict_input = {"a": tf.zeros((10, 1)), "b": tf.ones((10, 1))}
print(model.predict(list_input))
print(model.predict(dict_input))
print(model.predict(tf.data.Dataset.from_tensors(dict_input)))
# error here
print(model.predict(tf.data.Dataset.from_tensors(list_input))) which gives
|
workaround could be to convert list to dictionary in Dataset ds=tf.data.Dataset.from_tensors(list_input) def to_dict(lst): ds=ds.map(to_dict) print(model.predict(ds)) |
Thx, my problem solved! Just have changed |
I am not finding documentation for feeding models with multiple inputs with different dimensions with tf.data.
I can use the generator directly, but my goal is to move the generator to a full tf.data pipleline, but I am missing something fundamental to get started. This works, but does not use tf.data:
The following as close to a solution I have gotten to, but it fails
Generates error
I know that my request smells like "a request for help", it is, but please interpret it as a request for improved documentation. Stack overflow does not have anything on multiple inputs of different shapes. btw:
|
@johngrabner For the sake of completeness, here is a minimal example of a dataset that expects two inputs (shapes (1,32) and (1,128)):
|
hey! guys. Traceback (most recent call last):
File "practice.py", line 279, in <module>
action = np.argmax([0.1, 1, 0.2]*agent.get_qs(current_state))
File "practice.py", line 186, in get_qs
return self.model.predict(state)[0]
File "C:\Users\liuzhen\.conda\envs\python37\lib\site-packages\keras\engine\training.py", line 1380, in predict
x, _, _ = self._standardize_user_data(x)
File "C:\Users\liuzhen\.conda\envs\python37\lib\site-packages\keras\engine\training.py", line 757, in _standardize_user_data
exception_prefix='input')
File "C:\Users\liuzhen\.conda\envs\python37\lib\site-packages\keras\engine\training_utils.py", line 95, in standardize_input_data
data = [standardize_single_array(x) for x in data]
File "C:\Users\liuzhen\.conda\envs\python37\lib\site-packages\keras\engine\training_utils.py", line 95, in <listcomp>
data = [standardize_single_array(x) for x in data]
File "C:\Users\liuzhen\.conda\envs\python37\lib\site-packages\keras\engine\training_utils.py", line 30, in standardize_single_array
elif x.ndim == 1:
AttributeError: 'list' object has no attribute 'ndim' the 'state' is a list of two nd-arrays there model = Model(inputs=[input1, input2], outputs=predictions) I would really appreciate it if anyone is willing to give some tips |
hey! consider trying this:
hope this helps! |
@MatanSandori I think this method will work only if the inputs are of the same shape? |
|
Please go to Stack Overflow for help and support:
https://stackoverflow.com/questions/tagged/tensorflow
If you open a GitHub issue, here is our policy:
Here's why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.
System information
You can collect some of this information using our environment capture script:
https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh
You can obtain the TensorFlow version with
python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
Describe the problem
tf.keras
multi input models don't work when used together withtf.data.Dataset
due to input broken validation checks. This problem reproduces both on tf@1.9.0 and the latest nightly.@fchollet Do you have any ideas what's going on here, or am I missing something obvious?
Source code / logs
Multi input model
Consider the following toy model:
Using
numpy
dataWhen fitting using
numpy
data this works as expected when passing a list or dictionary of inputs:Using
tf.data.Dataset.from_tensor_slices
dictionaryWhen trying the same with a
tf.data.Dataset
the following fails due to incorrect input validation:Using
tf.data.Dataset.from_generator
dictionaryHowever using the same network together with
tf.data.Dataset.from_generator
works. Probably because less validation is done:Using
tf.data.Dataset
tuplePassing the multi-input as a tuple to the model both datasets generated with
from_tensor_slices
andfrom_generator
fail:The text was updated successfully, but these errors were encountered: