1源码涉及知识点
numpy.random.permutation将一个向量的顺序随机打乱。类似sklearn.model_selection
.train_test_split
**
栗子
import numpy as np
import math
a = np.random.randn(3,3)
print(a)
b = list(np.random.permutation(3))
a_shuffled = a[b]
2源码干货
X,Y自己替换
import numpy as np
import math
X = np.random.rand(30,10000)
Y = np.array([1,0]*5000).reshape(1,-1)
def random_mini_batches(X,Y,mini_batch_size,seed=2018):
mini_batches = []
#<1> shuffle
np.random.seed(2018)
m = 10000
permutation = list(np.random.permutation(m))
shuffled_X = X[:,permutation]
shuffled_Y = Y[:,permutation].reshape((1,m))
#<2> Partition
num_complete_minibatches = int(math.floor(m/mini_batch_size)) #轮数
for k in range(0,num_complete_minibatches):
mini_batch_X = shuffled_X[:,k * mini_batch_size:(k+1) * mini_batch_size]
mini_batch_Y = shuffled_Y[:,k * mini_batch_size:(k+1) * mini_batch_size]
mini_batch = (mini_batch_X,mini_batch_Y)
mini_batches.append(mini_batch)
if m % mini_batch_size != 0:
mini_batch_X = shuffled_X[:,(k+1) * mini_batch_size:m]
mini_batch_Y = shuffled_Y[:,(k+1) * mini_batch_size:m]
mini_batch = (mini_batch_X,mini_batch_Y)
mini_batches.append(mini_batch)
return mini_batches
3进一步解释
(1)数据格式如下,X是一个30维,数据量为10000的数据,Y是0-1标签
X = np.random.rand(30,10000)
Y = np.array([1,0]*5000).reshape(1,-1)
mini_batch_size = 64
mini_batchs = []
(2)打乱顺序,用到np.permutation
#<1> shuffle
np.random.seed(2018)
m = 10000
permutation = list(np.random.permutation(m))
shuffled_X = X[:,permutation]
shuffled_Y = Y[:,permutation].reshape((1,m))
(3)构造mini_batchs
根据这里的示例数据,m=10000,num_complete_minibatches 表示mini batches的次数。
假定mini_batch_size=64,那么num_complete_minibatches=int(math.floor(10000/64)) 。这里强制num_complete_minibatches向下取整,但是10000/64本质不是整数,为了把m无法被mini_batch_size整除的那部分数据利用上,后面又使用了if语句。
#<2> Partition
num_complete_minibatches = int(math.floor(m/mini_batch_size)) #轮数
for k in range(0,num_complete_minibatches):
mini_batch_X = shuffled_X[:,k * mini_batch_size:(k+1) * mini_batch_size]
mini_batch_Y = shuffled_Y[:,k * mini_batch_size:(k+1) * mini_batch_size]
mini_batch = (mini_batch_X,mini_batch_Y)
mini_batchs.append(mini_batch)
if m % mini_batch_size != 0:
mini_batch_X = shuffled_X[:,(k+1) * mini_batch_size:m]
mini_batch_Y = shuffled_Y[:,(k+1) * mini_batch_size:m]
mini_batch = (mini_batch_X,mini_batch_Y)
mini_batchs.append(mini_batch)
(4)最终得到mini_batches是一个list
x = random_mini_batches(X,Y,64) #list
x[0]
得:
>>>
array([[0.30991641, 0.94624542, 0.6868658 , ..., 0.29698455, 0.79686625,
0.08919725],
[0.45467657, 0.0254565 , 0.06003728, ..., 0.01350439, 0.90013728,
0.14127042],
[0.6224125 , 0.9621526 , 0.75451303, ..., 0.60983775, 0.0946255 ,
0.22334156],
...,
[0.76488744, 0.69523987, 0.52571847, ..., 0.76050906, 0.84368166,
0.11860472],
[0.64949559, 0.856298 , 0.31960172, ..., 0.72840738, 0.98289829,
0.90358176],
[0.95708814, 0.57716676, 0.82892984, ..., 0.39120321, 0.46789932,
0.07285383]]),
array([[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0]]))
每个list里面包含了一个batch ,除了最后一个batch的大小64,其余都64