【发布时间】:2023-03-14 18:38:02
【问题描述】:
这是用于将数据转换为 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())
功能 价格 是一个形状数组 (1,288)。它转换成功!但是当使用解析函数和数据集 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: W 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.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=[“label”,“prices”],dense_shapes=[[],[]],num_sparse=0,sparse_keys=[],sparse_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" ]]fl ow\core\framework\op_kernel.cc:1202] OP_REQUIRES 在 example_parsing_ops.cc:240 失败:无效参数:键:价格。无法解析序列化的示例。
【问题讨论】:
标签: python tensorflow