-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerator.py
More file actions
40 lines (34 loc) · 1.18 KB
/
Copy pathgenerator.py
File metadata and controls
40 lines (34 loc) · 1.18 KB
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
##
## generator.py
## Generator random minibatches for stochastic gradient descent.
## This method only works if all your data fits in RAM.
##
import numpy as np
class Generator(object):
'''
Minibatch generator class.
'''
def __init__(self, X, y, minibatch_size):
'''
X = array of all training or testing data
y = array of all training or testing labels
'''
self.all_X = X
self.all_y = y
self.minibatch_size = minibatch_size
self.indices = np.arange(self.all_X.shape[0])
np.random.shuffle(self.indices)
self.pointer = 0
self.num_epochs = 0
def generate(self):
'''
Make that minibatch!
'''
self.X = self.all_X[self.indices[self.pointer:self.pointer+self.minibatch_size]]
self.y = self.all_y[self.indices[self.pointer:self.pointer+self.minibatch_size]]
self.pointer += self.minibatch_size
#End of Epoch
if self.pointer > self.all_X.shape[0] - self.minibatch_size:
self.pointer = 0
self.num_epochs += 1
np.random.shuffle(self.indices) #Reshuffle indices at the end of each epoch