def alignData(self, data):
"""
Align data to a multiple of the macro batch size, pad last incomplete minibatch with random samples
:param data: data for alignment
:return: padded data
"""
# pad with zeros to macro batch size, but only along dimension 0 ie samples
topad = self.getNumSamplesPerMacroBatch() - data.shape[0] % self.getNumSamplesPerMacroBatch()
sz = []
sz.append((0, topad))
for i in range(len(data.shape) - 1):
sz.append((0, 0))
padded = numpy.pad(data, sz, mode='constant', constant_values=0)
# fill last incomplete minibatch with random samples
if (data.shape[0] % self.cfgParams.batch_size) != 0:
# start from same random seed every time the data is padded, otherwise labels and data mix up
rng = numpy.random.RandomState(data.shape[0])
for i in xrange(0, self.cfgParams.batch_size - (data.shape[0] % self.cfgParams.batch_size)):
padded[data.shape[0]+i] = padded[rng.randint(0, data.shape[0])]
return padded
评论列表
文章目录