写入Tfrecord

        print("convert data into tfrecord:train\n")
out_file_train = "/home/huadong.wang/bo.yan/fudan_mtl/data/ace2005/bn_nw.train.tfrecord"
writer = tf.python_io.TFRecordWriter(out_file_train) for i in tqdm(range(len(data_train))):
record = tf.train.Example(features=tf.train.Features(feature={
'word_ids': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_x[i].tostring()])),
'et_ids1': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_et1[i].tostring()])),
'et_ids2': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_et2[i].tostring()])),
'position_ids1': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_p1[i].tostring()])),
'position_ids2': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_p1[i].tostring()])),
'chunks': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_chunks[i].tostring()])),
'spath_ids': tf.train.Feature(bytes_list=tf.train.BytesList(value=[train_spath[i].tostring()])),
'seq_len': tf.train.Feature(int64_list=tf.train.Int64List(value=[train_x_len[i]])),
'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[np.argmax(train_relation[i])])),
'task': tf.train.Feature(int64_list=tf.train.Int64List(value=[np.int64(0)]))
}))
writer.write(record.SerializeToString())
writer.close()

  

解析tfrecord

def _parse_tfexample(serialized_example):
'''parse serialized tf.train.SequenceExample to tensors
context features : label, task
sequence features: sentence
'''
context_features={'label' : tf.FixedLenFeature([], tf.int64),
'task' : tf.FixedLenFeature([], tf.int64),
'seq_len': tf.FixedLenFeature([], tf.int64)}
sequence_features={'word_ids': tf.FixedLenSequenceFeature([], tf.int64),
'et_ids1': tf.FixedLenSequenceFeature([], tf.int64),
'et_ids2': tf.FixedLenSequenceFeature([], tf.int64),
'position_ids1': tf.FixedLenSequenceFeature([], tf.int64),
'position_ids2': tf.FixedLenSequenceFeature([], tf.int64),
'chunks': tf.FixedLenSequenceFeature([], tf.int64),
'spath_ids': tf.FixedLenSequenceFeature([], tf.int64),
}
context_dict, sequence_dict = tf.parse_single_sequence_example(
serialized_example,
context_features = context_features,
sequence_features = sequence_features) sentence = (sequence_dict['word_ids'],sequence_dict['et_ids1'],sequence_dict['et_ids2'],sequence_dict['position_ids1'],
sequence_dict['position_ids2'],sequence_dict['chunks'],sequence_dict['spath_ids'], context_dict['seq_len']) label = context_dict['label']
task = context_dict['task'] return task, label, sentence def read_tfrecord(epoch, batch_size):
for dataset in DATASETS:
train_record_file = os.path.join(OUT_DIR, dataset+'.train.tfrecord')
test_record_file = os.path.join(OUT_DIR, dataset+'.test.tfrecord') train_data = util.read_tfrecord(train_record_file,
epoch,
batch_size,
_parse_tfexample,
shuffle=True) test_data = util.read_tfrecord(test_record_file,
epoch,
batch_size,
_parse_tfexample,
shuffle=False)
yield train_data, test_data

模型中使用:

  def build_task_graph(self, data):
task_label, labels, sentence = data
# sentence = tf.nn.embedding_lookup(self.word_embed, sentence)
##########################
word_ids, et_ids1,et_ids2,position_ids1,position_ids2,chunks,spath_ids,seq_len = sentence
# sentence = word_ids
######################### self.word_ids = word_ids
self.position_ids1 = position_ids1
self.position_ids2 = position_ids2
self.et_ids1 = et_ids1
self.et_ids2 = et_ids2
self.chunks_ids = chunks
self.spath_ids = spath_ids
self.seq_len = seq_len sentence = self.add_embedding_layers()

  

 

关于Tfrecord的更多相关文章

  1. Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)

    #写libsvm格式 数据 write libsvm     #!/usr/bin/env python #coding=gbk # ================================= ...

  2. 学习笔记TF016:CNN实现、数据集、TFRecord、加载图像、模型、训练、调试

    AlexNet(Alex Krizhevsky,ILSVRC2012冠军)适合做图像分类.层自左向右.自上向下读取,关联层分为一组,高度.宽度减小,深度增加.深度增加减少网络计算量. 训练模型数据集 ...

  3. [TFRecord格式数据]利用TFRecords存储与读取带标签的图片

    利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...

  4. 深度学习原理与框架-Tfrecord数据集的读取与训练(代码) 1.tf.train.batch(获取batch图片) 2.tf.image.resize_image_with_crop_or_pad(图片压缩) 3.tf.train.per_image_stand..(图片标准化) 4.tf.train.string_input_producer(字符串入队列) 5.tf.TFRecord(读

    1.tf.train.batch(image, batch_size=batch_size, num_threads=1) # 获取一个batch的数据 参数说明:image表示输入图片,batch_ ...

  5. 深度学习原理与框架-Tfrecord数据集的制作 1.tf.train.Examples(数据转换为二进制) 3.tf.image.encode_jpeg(解码图片加码成jpeg) 4.tf.train.Coordinator(构建多线程通道) 5.threading.Thread(建立单线程) 6.tf.python_io.TFR(TFR读入器)

    1. 配套使用: tf.train.Examples将数据转换为二进制,提升IO效率和方便管理 对于int类型 : tf.train.Examples(features=tf.train.Featur ...

  6. 3. Tensorflow生成TFRecord

    1. Tensorflow高效流水线Pipeline 2. Tensorflow的数据处理中的Dataset和Iterator 3. Tensorflow生成TFRecord 4. Tensorflo ...

  7. TFRecord文件的读写

    前言在跑通了官网的mnist和cifar10数据之后,笔者尝试着制作自己的数据集,并保存,读入,显示. TensorFlow可以支持cifar10的数据格式, 也提供了标准的TFRecord 格式,而 ...

  8. 目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

    将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as ...

  9. tfrecord

    制作自己的TFRecord数据集,读取,显示及代码详解 http://blog.csdn.net/miaomiaoyuan/article/details/56865361

  10. 3 TFRecord样例程序实战

    将图片数据写入Record文件 # 定义函数转化变量类型. def _int64_feature(value): return tf.train.Feature(int64_list=tf.train ...

随机推荐

  1. 【BigData】Java基础_HashSet

    HashSet简介 HashSet是一个集合数据类型,具有以下三个特性: (1)可以存储过个数据对象 (2)HashSet中的数据不能重复 (3)HashSet的数据存储是无序的 HashSet的几个 ...

  2. pom.xml文件引入tools.jar

    最近做hbase开发时,引入相关jar包后,出现了以下错误 Missing artifact jdk.tools:jdk.tools:jar:1.8 绝对地址引用 <dependency> ...

  3. Markdown 编辑器指南

    一直觉得博客园默认的编辑器不好用,后来了解了Markdown,并且博客园也支持Markdown标记,所以写篇博客总结下. 一.认识 Markdown Markdown 是一种用来写作的轻量级「标记语言 ...

  4. Linux 就该这么学 CH07 使用RAID和LVM磁盘阵列技术

    1 RAID (独立冗余磁盘阵列) RAID 技术通过把多个硬盘设备组合成一个容量更大.安全性更好的磁盘阵列,并把数据切割成多个区段之后分别存在各个不同的物理硬盘设备上,然后利用分散读写计数来提升磁盘 ...

  5. Flume的Source、Sink总结,及常用使用场景

    数据源Source RPC异构流数据交换 Avro Source Thrift Source 文件或目录变化监听 Exec Source Spooling Directory Source Taild ...

  6. 扫描工具Nikto-安全牛课堂网络安全之Web渗透测试练习记录

    web扫描工具大都支持两种模式:代理扫描和主动扫描 Nikto 扫描内容 扫描软件版本.存在安全隐患的文件.服务器配置漏洞.服务器配置漏洞.web应用安全隐患 常用命令 nikto -list-plu ...

  7. unity内存管理(转)

    转自:https://www.cnblogs.com/zsb517/p/5724908.html Unity3D 里有两种动态加载机制:一个是Resources.Load,另外一个通过AssetBun ...

  8. Docker的安装与使用

    Docker的安装 (1)卸载老版本yum remove docker \                  docker-client \                  docker-clien ...

  9. 『正睿OI 2019SC Day6』

    动态规划 \(dp\)早就已经是经常用到的算法了,于是老师上课主要都在讲题.今天讲的主要是三类\(dp\):树形\(dp\),计数\(dp\),\(dp\)套\(dp\).其中计数\(dp\)是我很不 ...

  10. Windows和Linux简单命令的总结

    MS-DOS 命令提示符(cmd) 启动:                      Win+R,输入cmd回车 切换盘符            盘符名称: 进入文件夹              cd ...