| 10 | |
| 11 | |
| 12 | class DataLoader(object): |
| 13 | def __init__(self, xs, ys, batch_size, pad_with_last_sample=True, shuffle=False): |
| 14 | """ |
| 15 | |
| 16 | :param xs: |
| 17 | :param ys: |
| 18 | :param batch_size: |
| 19 | :param pad_with_last_sample: pad with the last sample to make number of samples divisible to batch_size. |
| 20 | """ |
| 21 | self.batch_size = batch_size |
| 22 | self.current_ind = 0 |
| 23 | if pad_with_last_sample: |
| 24 | num_padding = (batch_size - (len(xs) % batch_size)) % batch_size |
| 25 | x_padding = np.repeat(xs[-1:], num_padding, axis=0) |
| 26 | y_padding = np.repeat(ys[-1:], num_padding, axis=0) |
| 27 | xs = np.concatenate([xs, x_padding], axis=0) |
| 28 | ys = np.concatenate([ys, y_padding], axis=0) |
| 29 | self.size = len(xs) |
| 30 | self.num_batch = int(self.size // self.batch_size) |
| 31 | if shuffle: |
| 32 | permutation = np.random.permutation(self.size) |
| 33 | xs, ys = xs[permutation], ys[permutation] |
| 34 | self.xs = xs |
| 35 | self.ys = ys |
| 36 | |
| 37 | def get_iterator(self): |
| 38 | self.current_ind = 0 |
| 39 | |
| 40 | def _wrapper(): |
| 41 | while self.current_ind < self.num_batch: |
| 42 | start_ind = self.batch_size * self.current_ind |
| 43 | end_ind = min(self.size, self.batch_size * (self.current_ind + 1)) |
| 44 | x_i = self.xs[start_ind: end_ind, ...] |
| 45 | y_i = self.ys[start_ind: end_ind, ...] |
| 46 | yield (x_i, y_i) |
| 47 | self.current_ind += 1 |
| 48 | |
| 49 | return _wrapper() |
| 50 | |
| 51 | |
| 52 | class StandardScaler: |