0
votes

What am I doing wrong?

https://repl.it/@zbitname/outputnamesproblem


    import tensorflow as tf
    import numpy as np

    def random_generator():
        while True:
            yield ({"input_1": np.random.randint(1, 10000), "input_2": np.random.randint(1, 10000)}, {"output": np.random.randint(0, 1)})

    model = tf.keras.models.Sequential()

    model.add(tf.keras.layers.Dense(16, activation=tf.nn.tanh))
    model.add(tf.keras.layers.Dense(4, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))

    model.build((1000, 2))

    categories_train = random_generator()

    model.compile(
        optimizer='sgd',
        loss='categorical_crossentropy',
        metrics=['accuracy']
    )

    model.fit_generator(
        generator=categories_train,
        use_multiprocessing=True,
        workers=6,
        steps_per_epoch=10000
    )

Actual results

OS: Windows 10

python.exe --version
> Python 3.6.7
python.exe -c 'import tensorflow as tf; print(tf.VERSION)'
> 1.12.0

python.exe bug.py
Traceback (most recent call last):
  File "bug.py", line 21, in 
    metrics=['accuracy']
  File "C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\checkpointable\base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py", line 600, in compile
    skip_target_weighing_indices)
  File "C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py", line 134, in _set_sample_weight_attributes
    self.output_names, sample_weight_mode, skip_target_weighing_indices)
AttributeError: 'Sequential' object has no attribute 'output_names'

OS: Ubuntu

$ cat /etc/lsb-release
> DISTRIB_ID=Ubuntu
> DISTRIB_RELEASE=16.04
> DISTRIB_CODENAME=xenial
> DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"

$ python3.6 --version
> Python 3.6.8
$ python -c 'import tensorflow as tf; print(tf.VERSION)'
> 1.12.0

$ python3.6 bug.py
Traceback (most recent call last):
  File "bug.py", line 21, in 
    metrics=['accuracy']
  File "/home/***/.local/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/***/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 600, in compile
    skip_target_weighing_indices)
  File "/home/***/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 134, in _set_sample_weight_attributes
    self.output_names, sample_weight_mode, skip_target_weighing_indices)
AttributeError: 'Sequential' object has no attribute 'output_names'
2

2 Answers

0
votes

You have a sequential model, which can only have one input and one output, with a linear structure (sequential). Your generator produces data for two inputs and one output. This is of course incompatible, and Keras tries to get the names of the inputs/outputs from your model but sequential doesn't support multiple inputs or outputs.

So solution is to make a proper model using the Functional API, or to rewrite your generator to have one input/output without names.

0
votes

Combine this with @Matias Valdenegro's answer. You can't use Sequential models with multiple inputs.

The problem is that you're passing the data with names, which aren't defined for your model.

Just pass the data in the correct order (for a model that supports multiple outputs) and it's enough:

def random_generator():
    while True:
        yield ([np.random.randint(1, 10000), np.random.randint(1, 10000)], 
                np.random.randint(0, 1))

For a sequential model, only one input and one output are valid:

yield np.random.randint(1, 10000), np.random.randint(0, 1)