Python tf.train.Features类型错误:不允许位置参数

Python tf.train.Features类型错误:不允许位置参数,python,tensorflow,Python,Tensorflow,我可能在这里做了一些愚蠢的事情,但我不知道为什么会出现这个错误 此代码适用于: example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': _int64_feature(FLAGS.img_height), 'image/width': _int64_feature(FLAGS.img_width), 'image/colorspace': _bytes_fea

我可能在这里做了一些愚蠢的事情,但我不知道为什么会出现这个错误

此代码适用于:

example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      'image/fn_0': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[0]))),
      'image/encoded_0': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[0])),
      'image/fn_1': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[1]))),
      'image/encoded_1': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[1])),
      'image/fn_2': _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[2]))),
      'image/encoded_2': _bytes_feature(tf.compat.as_bytes(ex_image_buffers[2]))}))
return example
但此代码不起作用(在文章标题中抛出TypeError):

ex_image_buffers是一个列表

据我所知,tf.train.Features将字典作为参数,我在第一个示例和第二个示例中组装了相同的字典(我认为)。第二种方法允许我根据其他代码调整字典,因此我更愿意避免硬编码不同的字段


想法?谢谢你的帮助

是的,我认为你犯了一个愚蠢的错误。试一试

example=tf.train.example(features=tf.train.features(feature=feature\u dict))


正如错误所述,tf.train.Features要求您通过关键字/参数对进行传递。您需要添加关键字
功能
,就像您提供的第一个示例中所做的那样。

是的,我认为您犯了一个愚蠢的错误。试一试

example=tf.train.example(features=tf.train.features(feature=feature\u dict))


正如错误所述,tf.train.Features要求您通过关键字/参数对进行传递。您需要添加关键字
功能
,就像您在提供的第一个示例中所做的那样。

谢谢,这很有效。谢谢你的帮助。为什么会有这个要求?我没有设计API,所以我不能说。您使用的是类的构造函数,文档()仅显示
**kwargs
作为输入。@JimParker您提供的文档链接完全没有任何内容。有没有办法知道这些方法的作用,因为文档主要是所有方法的列表。@deadcode:嗯,该网页已更改(2018年1月)…看不到旧信息。最好尝试以下链接的定义。谢谢,这是有效的。谢谢你的帮助。为什么会有这个要求?我没有设计API,所以我不能说。您使用的是类的构造函数,文档()仅显示
**kwargs
作为输入。@JimParker您提供的文档链接完全没有任何内容。有没有办法知道这些方法的作用,因为文档主要是所有方法的列表。@deadcode:嗯,该网页已更改(2018年1月)…看不到旧信息。最好尝试以下链接的定义。
feature_dict={
      'image/height': _int64_feature(FLAGS.img_height),
      'image/width': _int64_feature(FLAGS.img_width),
      'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)),
      'image/channels': _int64_feature(channels),
      'image/format': _bytes_feature(tf.compat.as_bytes(image_format)),
      'image/label': _bytes_feature(label_img_buffer),
      'image/label_path': _bytes_feature(tf.compat.as_bytes(os.path.basename(lbl_path))),
      }

  for idx, image in sorted(ex_image_buffers.iteritems()):
    img_key = 'image/encoded_' + str(idx)
    fn_key = 'image/fn_' + str(idx)
    feature_dict[img_key] = _bytes_feature(tf.compat.as_bytes(image))
    feature_dict[fn_key] = _bytes_feature(tf.compat.as_bytes(os.path.basename(ex_paths[idx])))

  example = tf.train.Example(features=tf.train.Features(feature_dict))
  return example