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
Named dictionary outputs in tf.keras.Model do not work #34199
Comments
I tried reproducing the issue in colab with TF 2.0 . I am seeing the below error message. |
@ravikyram - I update the example and added a colab link for easier reproducibility. |
I have tried on colab with TF version 2.0 ,2.1.0-dev20191119 and was able to reproduce the issue.Please, find the gist here. Thanks! |
I would also like to add that we could have resolved this issue with a simple workaround by simply modifying the model.output_names and model._output_layers (in tensorflow 1.15 and below) e.g:
Now when we do that on tensorflow 2.0: after changing the _output_layers the model adds the nested layers into itself and creates extra layers on top of the original model. |
Any news regarding the bug? |
Thanks for the issue! Support for arbitrary nested structures (including dicts) is available in the latest tf-nightly: I think there are some issues in the code provided regarding the loss used (I don't think the Model is outputting the shape of data that SparseCategoricalAccuracy expects), but confirmed that prediction is working as expected. Also see this bug: #33245 import tensorflow as tf
max_seq_len = 8
channels_count = 11
class MultiOutputModel(tf.keras.Model):
def __init__(self):
super(MultiOutputModel, self).__init__()
self.dense_a = tf.keras.layers.Dense(3)
self.dense_b = tf.keras.layers.Dense(4)
def call(self, inputs):
seq = inputs["F"]
out_a = self.dense_a(seq)
out_b = self.dense_b(seq)
return {"A": out_a, "B": out_b}
def ds_gen():
while True:
inputs = {"F": tf.random.uniform((max_seq_len, channels_count))}
outputs = {"A": tf.random.uniform((), minval=0, maxval=3, dtype=tf.int32),
"B": tf.random.uniform((), minval=0, maxval=4, dtype=tf.int32)}
yield inputs, outputs
ds = tf.data.Dataset.from_generator(ds_gen,
output_types=({"F": tf.float32},
{"A": tf.int32, "B":tf.int32}),
output_shapes=({"F": tf.TensorShape([max_seq_len, channels_count])},
{"A":tf.TensorShape([]), "B":tf.TensorShape([])}))
# check dataset - a (features, labels) tuple
for inp, out in ds.batch(8).take(1):
for ndx, (name, val) in enumerate(inp.items()):
print("features {}: {}: {}".format(ndx, name, val.shape), val.dtype)
for ndx, (name, val) in enumerate(out.items()):
print(" labels {}: {}: {}".format(ndx, name, val.shape), val.dtype)
model = MultiOutputModel()
def features_only(feat, lab):
return feat
pred = model.predict(ds.map(features_only).batch(8).take(1)) |
System information
Describe the current behavior
Using a custom model with named outputs does not work in TF2.
Describe the expected behavior
While using tuples for multi output model works fine, using a dictionary fails. Dict inputs and outputs seem to be allowed in the code (and in the tf.keras documentation), however the functionality seem not to be functional yet.
Code to reproduce the issue
https://colab.research.google.com/gist/kpe/501901b5197675818a2e8a0e0bc8f3a6/keras-named-output-dict.ipynb
The output from the above code is:
The text was updated successfully, but these errors were encountered: