python - 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:“顺序”对象没有属性“输出名称”
解决方案
将此与@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)
推荐阅读
- java - 谷歌应用更新导致语音识别问题
- javascript - 在传单中获取 WMS 的最小缩放级别
- spring-mvc - 使用 Spring Security 进行会话管理
- c# - 在水晶报表中隐藏和过滤记录
- angularjs - iphone X 媒体查询也适用于 iphone 8 plus
- c# - 使用 C# 以编程方式编辑大型 Word 文档
- c# - 自定义 msiexec 进度条?
- javascript - gapi.auth2 已使用不同的选项进行初始化。考虑调用 gapi.auth2.getAuthInstance() 而不是 gapi.auth2.init()
- junit - 如何在 Java <= 8 中强制执行模块边界
- python-3.x - 替换python数据框中的重复条目并根据其他列保留第一个值