Implement TensorFlow's next_batch for own data
The version of numpy data
import numpy as np
class Dataset:
def __init__(self, data):
self._index_in_epoch = 0
self._epochs_completed = 0
self._data = data
self._num_examples = data.shape[0]
pass
@property
def data(self):
return self._data
def next_batch(self, batch_size, shuffle=True):
start = self._index_in_epoch
if start == 0 and self._epochs_completed == 0:
idx = np.arange(0, self._num_examples)
np.random.shuffle(idx) # shuffle indexe
self._data = self.data[idx] # get the shuffled data
# go to the data of next batch
if start + batch_size > self._num_examples:
'''
note: when start == self._num_examples, data_rest_part = np.array([])
'''
self._epochs_completed += 1
# print(self.data)
rest_num_examples = self._num_examples - start
data_rest_part = self.data[start:self._num_examples]
idx_update = np.arange(0, self._num_examples)
np.random.shuffle(idx_update)
self._data = self.data[idx_update] # get another shuffled data
start = 0
self._index_in_epoch = batch_size - rest_num_examples
end = self._index_in_epoch
data_new_part = self._data[start:end]
return np.concatenate((data_rest_part, data_new_part), axis=0)
else:
self._index_in_epoch += batch_size
end = self._index_in_epoch
return self._data[start:end]
dataset = Dataset(np.arange(0, 10))
for i in range(10):
print(dataset.next_batch(6))
print(dataset.data)
The version of pandas data
import numpy as np
import pandas as pd
class Dataset:
def __init__(self, data):
self._index_in_epoch = 0
self._epochs_completed = 0
self._data = data
self._num_examples = data.shape[0]
pass
@property
def data(self):
return self._data
def next_batch(self, batch_size, shuffle=True):
start = self._index_in_epoch
if start == 0 and self._epochs_completed == 0:
idx = np.arange(0, self._num_examples)
np.random.shuffle(idx) # shuffle index
self._data = self.data.iloc[idx,:] # get the shuffled data
# go to the data of next batch
if start + batch_size > self._num_examples:
'''
note: when start == self._num_examples, data_rest_part = np.array([])
'''
self._epochs_completed += 1
# print(self.data) # this is for debug
rest_num_examples = self._num_examples - start
data_rest_part = self.data.iloc[start:self._num_examples,:]
idx_update = np.arange(0, self._num_examples)
np.random.shuffle(idx_update)
self._data = self.data.iloc[idx_update,:] # get another shuffled data
start = 0
self._index_in_epoch = batch_size - rest_num_examples
end = self._index_in_epoch
data_new_part = self._data.iloc[start:end,:]
return pd.concat((data_rest_part, data_new_part), axis=0)
else:
self._index_in_epoch += batch_size
end = self._index_in_epoch
return self._data[start:end]
df = pd.DataFrame()
df['a']=np.arange(10)
df['b']=np.arange(10)*10
dataset = Dataset(df)
for i in range(10):
print(dataset.next_batch(5))
print(dataset.data)
Implement TensorFlow's next_batch for own data的更多相关文章
- Tensorflow - Implement for generating some 3-dimensional phony data and fitting them with a plane.
Coding according to TensorFlow 官方文档中文版 import tensorflow as tf import numpy as np ''' Intro. for thi ...
- tensorflow.python.framework.errors_impl.PermissionDeniedError: /data; Permission denied
在linux系统中,tensorflow跑mnist数据集出现错误,本应该自动下载的数据集 将mnist自动下载的路径,由/data/mnist之前的/删掉即可.改为data/mnist.
- tensorflow的object detection的data augmention的使用
在protoc的目录下有data augmention的提示,而且注意是repeated,也就是你要这样写: 不能写在一个data_aumentation_options下面,至于有哪些选项可以用,可 ...
- How to use Data Iterator in TensorFlow
How to use Data Iterator in TensorFlow one_shot_iterator initializable iterator reinitializable iter ...
- [TensorFlow] Introduction to TensorFlow Datasets and Estimators
Datasets and Estimators are two key TensorFlow features you should use: Datasets: The best practice ...
- 逻辑回归,附tensorflow实现
本文旨在通过二元分类问题.多元分类问题介绍逻辑回归算法,并实现一个简单的数字分类程序 在生活中,我们经常会碰到这样的问题: 根据苹果表皮颜色判断是青苹果还是红苹果 根据体温判断是否发烧 这种答案只有两 ...
- 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习
MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...
- 逻辑斯特回归tensorflow实现
calss #!/usr/bin/python2.7 #coding:utf-8 from __future__ import print_function import tensorflow as ...
- TensorFlow在win10上的安装与使用(三)
本篇博客介绍最经典的手写数字识别Mnist在tf上的应用. Mnist有两种模型,一种是将其数据集看作是没有关系的像素值点,用softmax回归来做.另一种就是利用卷积神经网络,考虑局部图片像素的相关 ...
随机推荐
- 牛客 2C 圈圈
题面: shy有一个队列a[1], a[2],…,a[n].现在我们不停地把头上的元素放到尾巴上.在这过程中我们会得到n个不同的队列,每个队列都是a[k],a[k+1],…,a[n],a[1],…,a ...
- Css常用的技巧
一.使用css缩写 使用缩写可以帮助减少你CSS文件的大小,更加容易阅读. 具体内容请浏览:CSS常用缩写语法 二.明确定义单位,除非值为0. 忘记定义尺寸的单位是CSS新手普遍的错误.在HTML中 ...
- 本人亲测-C#常用工具类
/* * HTTP接口工具类 */ public class HttpUitls { /* * get请求 */ public static string Get(string Url) { //Sy ...
- hadopp 环境搭建
前序: 首先准备三个虚拟机节点. 配置hosts文件:每个节点都 如下配置: vi /etc/hosts 1. 每个结点分别产生公私密钥 ssh-keygen -t dsa -P '' -f ~/. ...
- hdfs 配置文件详解
– dfs.name.dir – NameNode 元数据存放位置 – 默认值:使用core-site.xml中的hadoop.tmp.dir/dfs/name – dfs.block.size – ...
- 文件hash、上传,实现文件上传重复验证
在平台开发中,我们往往对性能要求十分严苛,每一个字段.接口都有严格的要求. 系统中文件流操作十分占用资源,这里为大家介绍对文件上传进行哈希校验---同一文件只允许上传一次到服务器,其他的上传只要指向文 ...
- iptables 设置特定IP访问指定端口
一.添加规则:设置禁止所有IP访问指定端口8075 [root@zabbix_server ~]# iptables -I INPUT -p tcp --dport -j DROP 二.测试telne ...
- touch cyusbConfig.cmake
touch cyusbConfig.cmake cmake文件丢失,与其解决问题,不如临时建立一个临时文件
- HDU - 6393 Traffic Network in Numazu (基环树+树链剖分/LCA)
题意:给定一个带权边无向基环树,有两种操作,一种是改变某个边的权值,另一种是询问两点间的最短路径. 可以对环进行缩点,以环为根建立一棵新树,并记录与环相连的所有点和环上的哪个点相连,将路径分为环外和环 ...
- Linux使用技巧汇总
Debian是我日常使用的桌面系统,这里记录了我在使用Debian和其他Linux时所有的问题和解决办法,以及一些其他的心得体会. 向Debian致敬! 找回桌面系统关机按钮 在/etc/polkit ...