Python tensorflow使用多个数组功能创建tfrecord

Python tensorflow使用多个数组功能创建tfrecord,python,tensorflow,tfrecord,Python,Tensorflow,Tfrecord,我按照TensorFlow从三个NumPy数组生成一个tf.record,但是,我在尝试序列化数据时遇到了一个错误。我希望生成的tfrecord包含三个特性 将numpy导入为np 作为pd进口熊猫 #一些随机数据 x=np.random.randn(85) y=np.random.randn(852128) z=np.随机选择(范围(10)、(85155)) def_浮动功能(值): “”“从浮点/双精度返回浮点\u列表。”“” 返回tf.train.Feature(float\u list=

我按照TensorFlow从三个NumPy数组生成一个tf.record,但是,我在尝试序列化数据时遇到了一个错误。我希望生成的
tfrecord
包含三个特性

将numpy导入为np
作为pd进口熊猫
#一些随机数据
x=np.random.randn(85)
y=np.random.randn(852128)
z=np.随机选择(范围(10)、(85155))
def_浮动功能(值):
“”“从浮点/双精度返回浮点\u列表。”“”
返回tf.train.Feature(float\u list=tf.train.FloatList(value=[value]))
def_int64_功能(值):
“”“从bool/enum/int/uint返回int64_列表。”“”
返回tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def序列化_示例(功能0、功能1、功能2):
"""
创建准备写入文件的tf.Example消息。
"""
#创建一个字典,将功能名称映射到tf.Example-compatible
#数据类型。
特征={
“功能0”:“浮动”功能(功能0),
“功能1”:“浮动”功能(功能1),
“功能2”:功能(功能2)
}
#使用tf.train.Example创建功能消息。
示例\u proto=tf.train.example(features=tf.train.features(feature=feature))
返回示例\u proto.SerializeToString()
features_dataset=tf.data.dataset.from_tensor_切片((x,y,z))
特征数据集
对于要素_数据集中的f0、f1、f2。取(1):
打印(f0)
打印(f1)
打印(f2)
def tf_序列化_示例(f0、f1、f2):
tf_字符串=tf.py_函数(
序列化您的示例,
(f0、f1、f2),#将这些参数传递给上述函数。
tf.string)#返回类型为'tf.string'。
返回tf.reformate(tf_string,())#结果是一个标量
然而,当尝试运行
tf\u序列化\u示例(f0、f1、f2)

我得到一个错误:

InvalidArgumentError: TypeError: <tf.Tensor: shape=(2128,), dtype=float32, numpy=
array([-0.5435242 ,  0.97947884, -0.74457455, ...,  has type tensorflow.python.framework.ops.EagerTensor, but expected one of: int, long, float
Traceback (most recent call last):

InvalidArgumentError:TypeError:好的,我现在有时间仔细看看。我注意到
features\u dataset
tf\u serialize\u example
的用法来自tensorflow webppage上的教程。我不知道这种方法的优点是什么以及如何解决这个问题

但是这里有一个工作流程应该适用于您的代码(我重新打开了生成的tfrecords文件,它们很好)


这段代码的主要区别在于,您将numpy数组而不是tensorflow张量馈送到
序列化\u示例
。希望这有帮助

抱歉,但我在您的代码中没有看到
tf\u serialize\u示例
。你能澄清一下以便重现你的错误吗?如果我将
value=[value]
替换为
value=value.flatte()
代码对我来说运行良好!我使用
tf
版本:
1.15.0
。您有什么版本?谢谢您的评论,我的版本是2.1.0,使用flatte()会产生错误:AttributeError:'tensorflow.python.framework.ops.热切Tensor'对象没有属性'flatten'。我应该降级吗?是的,代码是针对TF 1.x的,版本是1.15.0,我得到“RuntimeError:\uuuu iter\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu'I get UnknownError:AttributeError:'tensorflow.python.framework.ops.热切Tensor'对象没有属性'flatten'。非常感谢。这对我有用。我再次解析这个时遇到问题(得到“无法解析序列化的示例”,在使用这个答案时也是如此:)。不过,我想这与这个问题无关。
import numpy as np
import tensorflow as tf

# some random data
x = np.random.randn(85)
y = np.random.randn(85,2128)
z = np.random.choice(range(10),(85,155))

def _float_feature(value):
    """Returns a float_list from a float / double."""
    return tf.train.Feature(float_list=tf.train.FloatList(value=value.flatten()))

def _int64_feature(value):
    """Returns an int64_list from a bool / enum / int / uint."""

    return tf.train.Feature(int64_list=tf.train.Int64List(value=value.flatten()))

def serialize_example(feature0, feature1, feature2):
    """
    Creates a tf.Example message ready to be written to a file.
    """
    # Create a dictionary mapping the feature name to the tf.Example-compatible
    # data type.
    feature = {
      'feature0': _float_feature(feature0),
      'feature1': _float_feature(feature1),
      'feature2': _int64_feature(feature2)
    }
    # Create a Features message using tf.train.Example.
    return tf.train.Example(features=tf.train.Features(feature=feature))


writer = tf.python_io.TFRecordWriter('TEST.tfrecords')
example = serialize_example(x,y,z)
writer.write(example.SerializeToString())
writer.close()