我做错了什么?
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
)
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'
您有一个顺序模型,它只能有一个输入和一个输出,具有线性结构(顺序)。您的生成器为两个输入和一个输出生成数据。这当然是不兼容的,Keras试图从您的模型中获取输入/输出的名称,但顺序不支持多个输入或输出。
因此,解决方案是使用功能API创建一个适当的模型,或者重写生成器以使其具有一个没有名称的输入/输出。
将此与@Matias Valdenegros的答案相结合。您不能使用具有多个输入的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)