第五课 Tensorflow TFRecord读取数据
来源:互联网 发布:美工刀片可以过安检吗 编辑:程序博客网 时间:2024/05/17 01:38
分享朋友的机器学习应用案例:使用机器学习实现财富自由www.abuquant.com
虽然,可以使用常用的类型,但是使用tfrecord更好。
- protobuf的格式传输更快
- 结构统一。相当于屏蔽了底层的数据结构。
import tensorflow as tfimport numpy as npfrom IPython.display import display, HTMLimport matplotlib.pyplot as pltimport pandas as pdplt.rcParams["figure.figsize"] = (20,10)
train_df = pd.read_csv('train.csv')display(train_df.head())
5 rows × 785 columns
label_df = train_df.pop(item='label')train_values = train_df.valuestrain_labels = label_df.valuesdisplay(type(train_values))display(train_values.shape)display(type(train_labels))display(train_labels.shape)
numpy.ndarray(42000, 784)numpy.ndarray(42000,)
Example protobuf:
message Example { Features features = 1;};message Features { map<string, Feature> feature = 1;};message Feature { oneof kind { BytesList bytes_list = 1; FloatList float_list = 2; Int64List int64_list = 3; }};
# 建立tfrecorder writerwriter = tf.python_io.TFRecordWriter('csv_train.tfrecords')for i in xrange(train_values.shape[0]): image_raw = train_values[i].tostring() # build example protobuf example = tf.train.Example( features=tf.train.Features(feature={ 'image_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_raw])), 'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[train_labels[i]])) })) writer.write(record=example.SerializeToString())writer.close()
从TFRecord中读取数据
reader = tf.TFRecordReader()filename_queue = tf.train.string_input_producer(['csv_train.tfrecords'])_, serialized_record = reader.read(filename_queue)features = tf.parse_single_example(serialized_record, features={ ## tf.FixedLenFeature return Tensor ## tf.VarLenFeature return SparseTensor "image_raw": tf.FixedLenFeature([], tf.string), "label": tf.FixedLenFeature([], tf.int64), })images = tf.decode_raw(features['image_raw'], tf.uint8)labels = tf.cast(features['label'], tf.int32)with tf.Session() as session: session.run(tf.local_variables_initializer()) session.run(tf.global_variables_initializer()) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=session, coord=coord) for i in xrange(2): image, label = session.run([images, labels]) display(label) display(image) print '-' * 40
1array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)----------------------------------------0array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)----------------------------------------INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.CancelledError'>, Enqueue operation was cancelled [[Node: input_producer_3/input_producer_3_EnqueueMany = QueueEnqueueManyV2[Tcomponents=[DT_STRING], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer_3, input_producer_3/RandomShuffle)]]
阅读全文
0 0
- 第五课 Tensorflow TFRecord读取数据
- Tensorflow读取数据2-tfrecord
- tensorflow读取数据-tfrecord格式
- tensorflow读取数据-tfrecord格式
- TensorFlow高效读取数据——TFRecord
- Tensorflow中使用tfrecord方式读取数据
- Tensorflow-tfrecord数据
- Tensorflow使用TFRecord构建自己的数据集并读取
- TensorFlow学习记录-- 7.TensorFlow高效读取数据之tfrecord详细解读
- TensorFlow 学习(二) 制作自己的TFRecord数据集,读取,显示及代码详解
- TensorFlow学习笔记(二十四)自制TFRecord数据集 读取、显示及代码详解
- TensorFlow 制作自己的TFRecord数据集
- TensorFlow 制作自己的TFRecord数据集
- tensorflow制作数据集之TFRecord
- TensorFlow TFRecord
- tensorflow中的TFRecord格式文件的写入和读取
- Tensorflow 训练自己的数据集(二)(TFRecord)
- TFRecord —— tensorflow 下的统一数据存储格式
- 数学建模(4)——模拟退火
- 安全测试
- 《leetcode》spiral-matrix-ii(构造螺旋矩阵)
- 内存重叠
- squid 启用https反向代理代理
- 第五课 Tensorflow TFRecord读取数据
- 欢迎使用CSDN-markdown编辑器
- 谱聚类(spectral clustering)原理总结
- [LintCode 376] 二叉树的路径和(Python)
- Linux系统里安装源码包
- js call 方法简单介绍
- java 鼠标悬停等一系列操作
- 前端模块化开发
- 动态链接库的学习经验