0

I have train data as

xTrain = numpy.asarray([100, 1, 5, 6 ...])
yTrain = numpy.asarray([200, 2, 10, 12 ...])

How to define next_batch(size) method to get size number of random elements from the train data.

1 Answer 1

1

You can use this as your next batch function:

def batch_data(source, target, batch_size):

   # Shuffle data
   shuffle_indices = np.random.permutation(np.arange(len(target)))
   source = source[shuffle_indices]
   target = target[shuffle_indices]

   for batch_i in range(0, len(source)//batch_size):
      start_i = batch_i * batch_size
      source_batch = source[start_i:start_i + batch_size]
      target_batch = target[start_i:start_i + batch_size]

      yield np.array(source_batch), np.array(target_batch)
Sign up to request clarification or add additional context in comments.

2 Comments

ValueError: too many values to unpack When I gave batch_size greater than size if source.
you want batch_size > source size?. why? then its no longer a batch. Can you edit your question of what functionality you actually want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.