AttributeError:“顺序”对象没有属性“ output_names”。不是托科问题

时间:2018-12-25 20:27:34

标签: python python-3.x tensorflow keras deep-learning

我在做什么错了?

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
    )

实际结果

操作系统: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'

操作系统: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 个答案:

答案 0 :(得分:0)

您有一个顺序模型,该模型只能具有一个输入和一个输出,并且具有线性结构(顺序)。生成器为两个输入和一个输出生成数据。这当然是不兼容的,Keras尝试从模型中获取输入/输出的名称,但是顺序不支持多个输入或输出。

因此,解决方案是使用Functional API创建适当的模型,或者重写生成器以使其具有一个没有名称的输入/输出。

答案 1 :(得分:0)

使用@Matias Valdenegro的答案将其合并。您不能将Sequential模型用于多个输入。

问题在于您要传递带有名称的数据,这些名称尚未为模型定义。

只需以正确的顺序传递数据(对于支持多个输出的模型)就足够了:

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

对于顺序模型,只有一个输入和一个输出有效:

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