下面是程序代码,实现mini_batch,做深度学习的训练进行输入:
import numpy as np
def next_batch(num, data):
"""
Return a total of `num` samples from the array `data`.
"""
idx = np.arange(0, len(data)) # get all possible indexes
np.random.shuffle(idx) # shuffle indexes
idx = idx[0:num] # use only `num` random indexes
data_shuffle = [data[i] for i in idx] # get list of `num` random samples
data_shuffle = np.asarray(data_shuffle) # get back numpy array
return data_shuffle
def next_batch1(num, data1, data2):
"""
Return a total of `num` samples from the array `data`.
"""
idx = np.arange(0, len(data1)) # get all possible indexes
np.random.shuffle(idx) # shuffle indexes ,that means diorder the squeue of array
#the squeue is different for each shuffle
idx = idx[0:num] # use only `num` random indexes
data_shuffle1 = [data1[i] for i in idx] # get list of `num` random samples
data_shuffle1 = np.asarray(data_shuffle1) # get back numpy array
data_shuffle2 = [data2[i] for i in idx] # get list of `num` random samples
data_shuffle2 = np.asarray(data_shuffle2) # get back numpy array
return data_shuffle1, data_shuffle2
# demo data, 1d and 2d array
Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10)
data=[[]]
data= np.array([[1,2],[4,5],[7,8],[9,10],[3,6]])
data1= np.array([[1,2,3,2,],[4,5,3,2,],[7,8,6,2,],[9,10,8,2,],[3,6,7,2,]])
data2= np.array([[1,0],[0,1],[1,0],[0,1],[1, 0]])
print(Xtr)
print(Ytr)
print("data is:",data)
print("\n5 randnom samples from 1d array:")
print(next_batch(5, Xtr))
print("5 randnom samples from 2d array:")
print(next_batch(5, Ytr))
print("3 randnom samples from data:")
print(next_batch(3, data))
print("new create next_batch1() funcation from data1 and data2:")
x,y = next_batch1(3, data1, data2)
#print(x,y = next_batch1(3, data1, data2))
print('x is:',x)
print('y is:',y)
print('=======================')
#print(next_batch1(3, data1, data2)[0],next_batch1(3, data1, data2)[1])
print('=======================')