utils.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. def batch_indices(batch_nb, data_length, batch_size):
  16. """
  17. This helper function computes a batch start and end index
  18. :param batch_nb: the batch number
  19. :param data_length: the total length of the data being parsed by batches
  20. :param batch_size: the number of inputs in each batch
  21. :return: pair of (start, end) indices
  22. """
  23. # Batch start and end index
  24. start = int(batch_nb * batch_size)
  25. end = int((batch_nb + 1) * batch_size)
  26. # When there are not enough inputs left, we reuse some to complete the batch
  27. if end > data_length:
  28. shift = end - data_length
  29. start -= shift
  30. end -= shift
  31. return start, end