I find out this is actually very easy 

def batch(batch_size=batch_size, lower=0, upper=n_sample):
    indices = np.random.randint(low=lower, high=upper, size=batch_size)
    batch_data = torch.FloatTensor(batch_size, nc, image_size, image_size)
    for i in range(batch_size):
        sample, target = dataset[indices[i]]
        batch_data[i] = sample
    return batch_data