首页 > 解决方案 > AttributeError:“顺序”对象没有属性“输出名称”。不是toco问题

问题描述

我究竟做错了什么?

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 --版本
> Python 3.6.7
python.exe -c '将张量流导入为 tf; 打印(tf.VERSION)'
> 1.12.0

python.exe错误.py
回溯(最近一次通话最后):
  文件“bug.py”,第 21 行,在
    指标=['准确性']
  _method_wrapper 中的文件“C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\training\checkpointable\base.py”,第 474 行
    方法(自我,*args,**kwargs)
  编译中的文件“C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py”,第 600 行
    skip_target_weighing_indices)
  _set_sample_weight_attributes 中的文件“C:\Users\***\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\training.py”,第 134 行
    self.output_names、sample_weight_mode、skip_target_weighing_indices)
AttributeError:“顺序”对象没有属性“输出名称”

操作系统:Ubuntu

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

$ python3.6 --版本
> Python 3.6.8
$ python -c '将张量流导入为 tf; 打印(tf.VERSION)'
> 1.12.0

$ python3.6 bug.py
回溯(最近一次通话最后):
  文件“bug.py”,第 21 行,在
    指标=['准确性']
  _method_wrapper 中的文件“/home/***/.local/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py”,第 474 行
    方法(自我,*args,**kwargs)
  编译中的文件“/home/***/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py”,第 600 行
    skip_target_weighing_indices)
  _set_sample_weight_attributes 中的文件“/home/***/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py”,第 134 行
    self.output_names、sample_weight_mode、skip_target_weighing_indices)
AttributeError:“顺序”对象没有属性“输出名称”

标签: pythonpython-3.xtensorflowkerasdeep-learning

解决方案


将此与@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)

推荐阅读