问题描述
这是用于将数据转换为TFRecord的代码
This is the code used to convert data to TFRecord
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _floats_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
with tf.python_io.TFRecordWriter("train.tfrecords") as writer:
for row in train_data:
prices, label, pip = row[0],row[1],row[2]
prices = np.asarray(prices).astype(np.float32)
example = tf.train.Example(features=tf.train.Features(feature={
'prices': _floats_feature(prices),
'label': _int64_feature(label[0]),
'pip': _floats_feature(pip)
}))
writer.write(example.SerializeToString())
Feature prices 是一个形状数组 (1,288).转换成功了!但是当使用解析函数和数据集 API 解码数据时.
Feature prices is an array of shape(1,288). It converted successfully! But when decoded the data using a parse function and Dataset API.
def parse_func(serialized_data):
keys_to_features = {'prices': tf.FixedLenFeature([], tf.float32),
'label': tf.FixedLenFeature([], tf.int64)}
parsed_features = tf.parse_single_example(serialized_data, keys_to_features)
return parsed_features['prices'],tf.one_hot(parsed_features['label'],2)
它给了我错误
C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\framework\op_kernel.cc:1202] OP_REQUIRES 在 example_parsing_ops.cc:240 失败:参数无效:密钥:价格.无法解析序列化示例.2018-03-31 15:37:11.443073: WC:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\framework\op_kernel.cc:1202] OP_REQUIRES 在 example_parsing_op 失败:240 : 无效参数:键:价格.无法解析序列化示例.2018-03-31 15:37:11.443313: W C:\tf_jenkins\workspace\rel-win\M\windows-gpu\ raise type(e)(node_def, op, message)PY\36\tensortensorflow.python.framework.errors_impl.InvalidArgumentError:关键:价格.无法解析序列化示例.[[节点:ParseSingleExample/ParseSingleExample = ParseSingleExample[Tdense=[DT_INT64,DT_FLOAT],dense_keys=[标签",价格"],dense_shapes=[[],[]],num_sparse=0,sparse_keys=[types],=[]](arg0, ParseSingleExample/Const, ParseSingleExample/Const_1)]][[节点:IteratorGetNext_1 = IteratorGetNextoutput_shapes=[[?], [?,2]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]flow\core\framework\op_kernel.cc:1202] OP_REQUIRES 在 example_parsing_ops.cc:240 失败:参数无效:键:价格.无法解析序列化示例.
推荐答案
我发现了问题.不要使用 tf.io.FixedLenFeature
来解析数组,而是使用 tf.io.FixedLenSequenceFeature
(对于 TensorFlow 1,使用 tf.
而不是 tf.io.
)
I found the problem. Instead of using tf.io.FixedLenFeature
for parsing an array, use tf.io.FixedLenSequenceFeature
(for TensorFlow 1, use tf.
instead of tf.io.
)
这篇关于如何将 Float 数组/列表转换为 TFRecord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!