-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathivector_dataset.py
79 lines (55 loc) · 2.09 KB
/
ivector_dataset.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
from tensorflow.python.framework import dtypes
## ivector : Sampels X Dimension (2darray)
## labels : Samples (1darray)
class DataSet(object):
def __init__(self,
ivectors,
labels,
dtype=dtypes.float32):
self._ivectors = ivectors
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
self._num_examples = ivectors.shape[0]
self._dimension = ivectors.shape[1]
@property
def ivectors(self):
return self._ivectors
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
@property
def dimension(self):
return self._dimension
def next_batch(self,
batch_size,
shuffle):
head = self._index_in_epoch
# shuffling dataset at first batch of every epoch
if head == 0 and shuffle:
perm = np.arange(self._num_examples)
np.random.shuffle(perm)
self._ivectors = self.ivectors[perm]
self._labels = self.labels[perm]
# for last batch size => [total - batch_size : total]
if head + batch_size > self._num_examples:
self._index_in_epoch = self._num_examples - batch_size
head = self._index_in_epoch
# Last batch (reset index)
if head + batch_size == self._num_examples:
self._epochs_completed +=1
tail = self._index_in_epoch + batch_size
self._index_in_epoch = 0
return self._ivectors[head:tail], self._labels[head:tail]
#normal batch
else:
self._index_in_epoch += batch_size
tail = self._index_in_epoch
return self._ivectors[head:tail], self._labels[head:tail]