tensorflow 批次读取文件内的数据,并将顺序随机化处理. --[python]
使用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]的更多相关文章
- TensorFlow csv读取文件数据(代码实现)
TensorFlow csv读取文件数据(代码实现) 大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 Tens ...
- HDFS读文件过程分析:读取文件的Block数据
转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...
- python读取文件内的IP信息 练习
代码如下: #导包 import fileinput import re def readArw(): for line in fileinput.input(r"G:/raw.txt&qu ...
- C语言:字符串读取流读取文件中的数据
#include<stdio.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fopen("1.txt",&qu ...
- Android - 读取文件存储的数据
存取手机中的文件数据. 写入和读取的操作格式均为UTF-8. import java.io.File; import java.io.FileInputStream; import java.io.F ...
- fprintf写入字符串入文件/fread读取文件内的字符串
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { FILE * ...
- EasyExcel读取文件-同步处理数据
读取代码 // 前端传过来的文件 MultipartFile file; InputStream inputStream = file.getInputStream(); // 读取excel数据,边 ...
- JAVA流读取文件并保存数据
如图有文本如下数据 写方法读取数据 private String[][] getData(){ // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> a ...
随机推荐
- TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值—Jason niu
#TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值 import tensorflow as tf input1 = tf.placeholder ...
- 拯救者Y720-gtx1050-window10-配置tensorflow-gpu环境
https://www.cnblogs.com/31415926535x/p/10536572.html 概述 因为选修了数字图像这门课,,要做一个人脸识别的项目,和室友打算利用tensorflow来 ...
- DTL
DTL:Django模板语言(Django Template Language). 一.变量 1.视图函数可以通过两种方式将变量传递给模板页面: ① render(request, 'test_pag ...
- word插入行
如何在Word中添加多行或多列 在弹出的列表中选择[插入],再选择[在下方插入行]即可. 选择多少行就可添加多少行. 按F4重复上一操作可快速添加. 添加列也同样如此,选中一个单元格,右键单击,在弹出 ...
- [数据库]Sqlite使用入门
官网的文档结构十分恶劣,大概翻了一下,提供入门指引. 0. sqlite的安装 根据自身情况,在官网下载32位/64位的dll文件以及sqlite-tools-win32-x86-3240000.zi ...
- 洛谷P1541 乌龟棋(四维DP)
To 洛谷.1541 乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游 ...
- centos下python安装与虚拟环境配置
Centos7下安装Python3.7 首先安装依赖包,centos里面是-devel,如果在ubuntu下安装则要改成-dev,依赖包缺一不可,笔者曾安装python3未成功就是因为没有安装libf ...
- 潭州课堂25班:Ph201805201 django 项目 第七课 用户模型设计 (课堂笔记
在 user 的应用中的 models.py: 导入 dango 自带的用户模型 from django.contrib.auth.models import AbstractUser,UserMan ...
- SpringMVC框架简介
1.简介 SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 01.Spring mvc的优缺点 M ...
- 小型资源管理器之动态添加TreeView节点
FrmMain主界面 using System; using System.Collections.Generic; using System.ComponentModel; using System ...