使用tensorflow批次的读取预处理之后的文本数据,并将其分为一个迭代器批次:

比如此刻,我有一个处理之后的数据包: data.csv  shape =(8,10),其中这个结构中,前五个列为feature , 后五列为label

1,2,3,4,5,6,7,8,9,10
11,12,13,14,15,16,17,18,19,20
21,22,23,24,25,26,27,28,29,30
31,32,33,34,35,36,37,38,39,40
41,42,43,44,45,46,47,48,49,50
51,52,53,54,55,56,57,58,59,60
1,1,1,1,1,2,2,2,2,2
3,3,3,3,3,4,4,4,4,4

现在我需要将其分为4个批次: 也就是每个批次batch的大小为2

然后我可能需要将其顺序打乱,所以这里提供了两种方式,顺序和随机

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'xijun1'
import tensorflow as tf
import numpy as np # data = np.arange(1, 100 + 1)
# print ",".join( [str(i) for i in data])
# data_input = tf.constant(data)
filename_queue = tf.train.string_input_producer(["data.csv"])
reader = tf.TextLineReader(skip_header_lines=0)
key, value = reader.read(filename_queue)
# decode_csv will convert a Tensor from type string (the text line) in
# a tuple of tensor columns with the specified defaults, which also
# sets the data type for each column
words_size = 5 # 每一行数据的长度
decoded = tf.decode_csv(
value,
field_delim=',',
record_defaults=[[0] for i in range(words_size * 2)]) batch_size = 2 # 每一个批次的大小
# 随机
batch_shuffle = tf.train.shuffle_batch(decoded, batch_size=batch_size,
capacity=batch_size * words_size,
min_after_dequeue=batch_size)
#顺序
batch_no_shuffle = tf.train.batch(decoded, batch_size=batch_size, capacity=batch_size * words_size,
allow_smaller_final_batch=batch_size)
shuffle_features = tf.transpose(tf.stack(batch_shuffle[0:words_size]))
shuffle_label = tf.transpose(tf.stack(batch_shuffle[words_size:]))
features = tf.transpose(tf.stack(batch_no_shuffle[0:words_size]))
label = tf.transpose(tf.stack(batch_no_shuffle[words_size:])) with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(8/batch_size):
print (i+10, sess.run([shuffle_features, shuffle_label]))
print (i, sess.run([features, label]))
coord.request_stop()
coord.join(threads)

当我们运行的时候,我们可以得到这个结果:

(10, [array([[ 1,  2,  3,  4,  5],
[31, 32, 33, 34, 35]], dtype=int32), array([[ 6, 7, 8, 9, 10],
[36, 37, 38, 39, 40]], dtype=int32)])
(0, [array([[11, 12, 13, 14, 15],
[21, 22, 23, 24, 25]], dtype=int32), array([[16, 17, 18, 19, 20],
[26, 27, 28, 29, 30]], dtype=int32)])
(11, [array([[51, 52, 53, 54, 55],
[ 3, 3, 3, 3, 3]], dtype=int32), array([[56, 57, 58, 59, 60],
[ 4, 4, 4, 4, 4]], dtype=int32)])
(1, [array([[41, 42, 43, 44, 45],
[ 1, 1, 1, 1, 1]], dtype=int32), array([[46, 47, 48, 49, 50],
[ 2, 2, 2, 2, 2]], dtype=int32)])
(12, [array([[ 3, 3, 3, 3, 3],
[11, 12, 13, 14, 15]], dtype=int32), array([[ 4, 4, 4, 4, 4],
[16, 17, 18, 19, 20]], dtype=int32)])
(2, [array([[ 1, 2, 3, 4, 5],
[21, 22, 23, 24, 25]], dtype=int32), array([[ 6, 7, 8, 9, 10],
[26, 27, 28, 29, 30]], dtype=int32)])
(13, [array([[31, 32, 33, 34, 35],
[ 1, 1, 1, 1, 1]], dtype=int32), array([[36, 37, 38, 39, 40],
[ 2, 2, 2, 2, 2]], dtype=int32)])
(3, [array([[41, 42, 43, 44, 45],
[ 1, 1, 1, 1, 1]], dtype=int32), array([[46, 47, 48, 49, 50],
[ 2, 2, 2, 2, 2]], dtype=int32)])

tensorflow 批次读取文件内的数据,并将顺序随机化处理. --[python]的更多相关文章

  1. TensorFlow csv读取文件数据(代码实现)

    TensorFlow csv读取文件数据(代码实现) 大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 Tens ...

  2. HDFS读文件过程分析:读取文件的Block数据

    转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...

  3. C语言 读取文件中特定数据

    //读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...

  4. python读取文件内的IP信息 练习

    代码如下: #导包 import fileinput import re def readArw(): for line in fileinput.input(r"G:/raw.txt&qu ...

  5. C语言:字符串读取流读取文件中的数据

    #include<stdio.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fopen("1.txt",&qu ...

  6. Android - 读取文件存储的数据

    存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...

  7. fprintf写入字符串入文件/fread读取文件内的字符串

    #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { FILE * ...

  8. EasyExcel读取文件-同步处理数据

    读取代码 // 前端传过来的文件 MultipartFile file; InputStream inputStream = file.getInputStream(); // 读取excel数据,边 ...

  9. JAVA流读取文件并保存数据

    如图有文本如下数据 写方法读取数据 private String[][] getData(){ // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> a ...

随机推荐

  1. 用面向对象重写thread 实现多次调用一个线程

    思路: 利用thread类中,run方法在子线程中调用,其他方法在主线程调用,所以将生产者写入主线程,将消费者写入run函数中在子线程中执行,完成生产者消费者模型 注意: 1. 要在 init 函数中 ...

  2. axios 进行类库封装

    ,,,,,, ,) { // 与后台约定的成功状态码 callback && callback(result) } else { // 错误处理,优先以自定义的的handle进行处理, ...

  3. Activity插件化解决方案

    --摘自<android插件化开发指南> 1.宿主App加载插件中的类 2.最简单的插件化方案就是在宿主的androidmanifest.xml中申明插件中的四大组件 把插件dex合并到宿 ...

  4. UVA11324 The Largest Clique (强连通缩点+DP最长路)

    <题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...

  5. Codeforces 1073C Vasya and Robot 【二分】

    <题目链接> 题目大意: 一个机器人从(0,0)出发,输入一段指令字符串,和机器人需要在指定步数后到达的终点,问如果机器人需要在指定步数内到达终点,那么需要对原指令字符串做出怎样的改变,假 ...

  6. poj 3685 Matrix 【二分】

    <题目链接> 题目大意: 给你一个n*n的矩阵,这个矩阵中的每个点的数值由   i2 + 100000 × i + j2 - 100000 × j + i × j  这个公式计算得到,N( ...

  7. 二叉搜索树的java实现

    转载请注明出处 一.概念 二叉搜索树也成二叉排序树,它有这么一个特点,某个节点,若其有两个子节点,则一定满足,左子节点值一定小于该节点值,右子节点值一定大于该节点值,对于非基本类型的比较,可以实现Co ...

  8. pwntools简介

    安装binutils: git clone https://github.com/Gallopsled/pwntools-binutils sudo apt-get install software- ...

  9. 如何在ElementUI中的Table控件中使用拼音进行排序

    本人使用版本是1.4.7 在这个版本中对应全是String的column进行排序并不是按照拼音的方式排列的. 这里我贴一下源代码就可以看出是为什么了: export const orderBy = f ...

  10. BZOJ.5251.[八省联考2018]劈配mentor(最大流)

    BZOJ 洛谷 对于每个人,每次枚举一个志愿看是否能增广即可. 对于第二问,可以保留第一问中\(n\)次增广前后的\(n\)张图,二分,在对应图上看是否能增广即可. 貌似匈牙利的某种写法比网络流优多了 ...