2017-12-18 4 views
1

numpy 배열에서 tfrecord 형식으로 데이터 집합을 만들려고합니다. 2 차원 및 3 차원 좌표를 저장하려고합니다.numpy 배열을 tfrecord로 저장하는 방법은 무엇입니까?

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() 
:

2 차원 좌표는 3 차원 좌표를 입력 float64

의 모양 (3,10)의 NumPy와 배열입니다 float64 타입의 모양 (2,10)의 NumPy와 배열이 내 코드되어 있습니다 나는이 문제를 해결하는 방법을 잘 모릅니다

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에 이르기 때문에 이것에 대해 갈 방법이 없다. 어떤 도움이 될거야! 감사

답변

1

tf.train.Feature 클래스는 목록 (또는 1-D 배열)을 float_list 인수를 사용하여 지원합니다. 데이터에 따라 다음 방법 중 하나를 시도 할 수 있습니다 :

  1. 가 배열의 데이터를 평평하게 tf.train.Feature에 전달하기 전에 : 당신이 다른 기능을 추가해야 할 수도 있습니다

    def _floats_feature(value): 
        return tf.train.Feature(float_list=tf.train.FloatList(value=value.reshape(-1))) 
    

    주 이 데이터를 다시 구문 분석 할 때이 데이터의 모양을 변경하는 방법을 나타냅니다 (그 목적으로 int64_list 기능을 사용할 수 있음).

  2. 다차원 피쳐를 여러 1 차원 피쳐로 분할합니다. 예를 들어 c2dN * 2 x 및 y 좌표 배열이 포함되어있는 경우 해당 기능을 각각 x 및 y 좌표 데이터가 들어있는 train/coord2d/xtrain/coord2d/y 피쳐로 나눌 수 있습니다.

관련 문제