将数据帧转换为tfrecord

问题描述

我无法转换包含字符串标签和2D浮点数的numpy数组的数据框,我不知道为什么,但是当我使用dtypes时,我发现它们都具有对象类型,因此我尝试将标签从对象转换为到字符串,然后到字节,但是它不起作用,我尝试使用以下方法将点转换为numpy:

data['Points']=data['Points'].to_numpy()

但是点仍然是object类型,我的目标是将点转换为float,即使我尝试注释标签功能,点的功能也不起作用,我也会得到字符串索引muts是整数

我真的很困,谢谢您的帮助!

Error:has type Series,but expected one of: bytes,although I converted Labels to bytes

这是完整的代码

SPLIT_SIZE=0.8


training_length = int(len(data) * SPLIT_SIZE)
validation_length = int(len(data) - training_length)

data=data.sample(frac=1)

training_set = data.iloc[0:training_length,0:2]
training_set['Labels']=training_set['Labels'].astype('string')
training_set['Points']=training_set['Points'].to_numpy()
validation_set=data.iloc[-validation_length:,0:2]
validation_set['Labels']=validation_set['Labels'].astype('string')
validation_set['Points']=validation_set['Points'].to_numpy()

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _float_feature(value):
    return tf.train.Feature(float_list=tf.train.FloatList(value=value))



def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _dtype_feature(ndarray):
    """match appropriate tf.train.Feature class with dtype of ndarray. """
    #assert isinstance(ndarray,np.ndarray)
    dtype_ = ndarray.dtype
    if dtype_ == np.float64 or dtype_ == np.float32  :
        return lambda array: tf.train.Feature(float_list=tf.train.FloatList(value=array))
    elif dtype_ == np.int64:
        return lambda array: tf.train.Feature(int64_list=tf.train.Int64List(value=array))

def tables_to_TF( data,tf_filename):
    # Target variable needs to be the last column of data

    filepath = os.path.join(tf_filename)
    print('Writing',filepath)
    #writer = tf.compat.v1.python_io.TFRecordWriter(tf_filename)
    writer = tf.io.TFRecordWriter(tf_filename)

    'for file in tqdm(queue_list):'
   
    #feature1= data['Labels']
    #feature2= data['Points']
    
    
    example = tf.train.Example(
        features=tf.train.Features(feature={

            'Labels': _bytes_feature(data['Labels'].str.encode('utf-8')),'Points': _dtype_feature(data['Points']),}) 


                    )
    


    writer.write(example.SerializetoString())

if __name__ == "__main__":


    filepathlist = ["dataset_astyx_hires2019/dataset_astyx_hires2019/data/"]
  
    tffilename = "dataset_astyx_hires2019/dataset_astyx_hires2019/data/train.tfrecord"
    tffilename2 = "dataset_astyx_hires2019/dataset_astyx_hires2019/data/validation.tfrecord"
    #import pdb; pdb.set_trace()      

    tables_to_TF( training_set,tffilename)
    tables_to_TF( validation_set,tffilename2)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)