forked from NeuroSyn-AI-Club/simple-deep-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.py
More file actions
25 lines (20 loc) · 839 Bytes
/
Copy pathUtils.py
File metadata and controls
25 lines (20 loc) · 839 Bytes
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
import numpy as np
def random_mini_batches(X, Y, batch_size):
m = X.shape[1]
mini_batches = []
# Shuffle (X, Y)
permutation = list(np.random.permutation(m))
X_shuffled = X[:, permutation]
Y_shuffled = Y[:, permutation]
# Partition into mini-batches
num_complete_batches = m // batch_size
for k in range(num_complete_batches):
X_batch = X_shuffled[:, k * batch_size: (k + 1) * batch_size]
Y_batch = Y_shuffled[:, k * batch_size: (k + 1) * batch_size]
mini_batches.append((X_batch, Y_batch))
# Handle last batch (if m % batch_size != 0)
if m % batch_size != 0:
X_batch = X_shuffled[:, num_complete_batches * batch_size:]
Y_batch = Y_shuffled[:, num_complete_batches * batch_size:]
mini_batches.append((X_batch, Y_batch))
return mini_batches