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的更多相关文章

  1. 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 ...

  2. tensorflow.python.framework.errors_impl.PermissionDeniedError: /data; Permission denied

    在linux系统中,tensorflow跑mnist数据集出现错误,本应该自动下载的数据集 将mnist自动下载的路径,由/data/mnist之前的/删掉即可.改为data/mnist.

  3. tensorflow的object detection的data augmention的使用

    在protoc的目录下有data augmention的提示,而且注意是repeated,也就是你要这样写: 不能写在一个data_aumentation_options下面,至于有哪些选项可以用,可 ...

  4. How to use Data Iterator in TensorFlow

    How to use Data Iterator in TensorFlow one_shot_iterator initializable iterator reinitializable iter ...

  5. [TensorFlow] Introduction to TensorFlow Datasets and Estimators

    Datasets and Estimators are two key TensorFlow features you should use: Datasets: The best practice ...

  6. 逻辑回归,附tensorflow实现

    本文旨在通过二元分类问题.多元分类问题介绍逻辑回归算法,并实现一个简单的数字分类程序 在生活中,我们经常会碰到这样的问题: 根据苹果表皮颜色判断是青苹果还是红苹果 根据体温判断是否发烧 这种答案只有两 ...

  7. 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习

    MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...

  8. 逻辑斯特回归tensorflow实现

    calss #!/usr/bin/python2.7 #coding:utf-8 from __future__ import print_function import tensorflow as ...

  9. TensorFlow在win10上的安装与使用(三)

    本篇博客介绍最经典的手写数字识别Mnist在tf上的应用. Mnist有两种模型,一种是将其数据集看作是没有关系的像素值点,用softmax回归来做.另一种就是利用卷积神经网络,考虑局部图片像素的相关 ...

随机推荐

  1. JDK SPI

    最近学习了JDK SPI   JDK SPI是什么 最近工作中听几个同事说了好几次SPI这个名词,虽然和我没关系,但是心里默默想还是学习一下,不然下次和我说到SPI,连是什么都不知道那就尴尬了. 所以 ...

  2. Dubbo消费方服务调用过程源码分析

    参考:dubbo消费方服务调用过程源码分析dubbo基于spring的构建分析Dubbo概述--调用过程dubbo 请求调用过程分析dubbo集群容错机制代码分析1dubbo集群容错策略的代码分析2d ...

  3. 微信小程序多video播放暂停问题

    <swiper class="swiper" indicator-dots="{{indicatorDots}}" autoplay="{{fl ...

  4. 第十篇.6、python并发编程之IO模型

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  5. npm install 报错,提示`gyp ERR! stack Error: EACCES: permission denied` 解决方法

    m install 报错,提示gyp ERR! stack Error: EACCES: permission denied 猜测可能是因为没有权限读写,ls -la看下文件权限设置情况 [root@ ...

  6. Lomsat gelral CodeForces - 600E (树上启发式合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  7. xss技巧记录

    1.iframe的srcdoc属性 先演示一下 <iframe srcdoc="<script>alert(1)</script>"> 浏览器渲 ...

  8. 递推DP(至少和至多之间的转换

    UVa 10328 - Coin Toss 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 转换成抛N次至多连续有N个减去抛N次至多连续有K-1个1的情况 dp[i][k]表 ...

  9. Hbase性能优化

    HBase性能优化方法总结 1. 表的设计 1.1 Pre-Creating Regions 默认情况下,在创建HBase表的时候会自动创建一个region分区,当导入数据的时候,所有的HBase客户 ...

  10. Centos安装nmap端口查看工具

    CentOS安装nmap端口查看工具 一.安装nmap yum install nmap    #输入y安装 二.使用nmap      nmap localhost    #查看主机当前开放的端口  ...