如何将numpy数组存储为tfrecord?
我想从numpy数组中创建tfrecord格式的数据集。我试图存储2D和3D坐标。如何将numpy数组存储为tfrecord?
2D坐标型的形状(2,10)的numpy的阵列float64 三维坐标型float64
的形状(3,10)的numpy的阵列,这是我的代码:
def _floats_feature(value): return tf.train.Feature(float_list=tf.train.FloatList(value=value))
train_filename = 'train.tfrecords' # address to save the TFRecords file
writer = tf.python_io.TFRecordWriter(train_filename)
for c in range(0,1000):
#get 2d and 3d coordinates and save in c2d and c3d
feature = {'train/coord2d': _floats_feature(c2d),
'train/coord3d': _floats_feature(c3d)}
sample = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(sample.SerializeToString())
writer.close()
当我运行此我得到的错误:
feature = {'train/coord2d': _floats_feature(c2d), File "genData.py", line 19, in _floats_feature
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\python_message.py", line 510, in init
copy.extend(field_value)
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\containers.py", line 275, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\containers.py", line 275, in <listcomp>
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\google\protobuf\internal\type_checkers.py", line 109, in CheckValue
raise TypeError(message)
TypeError: array([-163.685, 240.818, -114.05 , -518.554, 107.968, 427.184,
157.418, -161.798, 87.102, 406.318]) has type <class 'numpy.ndarray'>, but expected one of: ((<class 'numbers.Real'>,),)
我不知道如何解决这个问题。我应该存储的功能为int64或字节?我不知道如何去做这件事,因为我对tensorflow完全陌生。任何帮助将是伟大的!感谢
回答:
tf.train.Feature
的类只支持列表(或1-d阵列)使用float_list
参数时。根据您的资料,您可以尝试以下方法之一:
拼合你的阵列中的数据将它传递给
tf.train.Feature
前:def _floats_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=value.reshape(-1)))
请注意,您可能需要另一个功能添加到表明这个数据应该如何重塑当你再次解析它(你可以使用为目的的
int64_list
功能)。将多维特征拆分为多个一维特征。例如,如果
c2d
包含N * 2
阵列x和y坐标的,可以拆分特征为单独train/coord2d/x
和train/coord2d/y
特征,每个包含x和分别y坐标数据,。
以上是 如何将numpy数组存储为tfrecord? 的全部内容, 来源链接: utcz.com/qa/260419.html