/tensorflow/contrib/learn/python/learn/trainer.py

https://gitlab.com/hrishikeshvganu/tensorflow
Python | 75 lines | 42 code | 5 blank | 28 comment | 7 complexity | dcaa42bfe1b4aaa18012f1bfd1ea588d MD5 | raw file
  1. """Generic trainer for TensorFlow models."""
  2. # Copyright 2015-present The Scikit Flow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. from six.moves import xrange # pylint: disable=redefined-builtin
  19. from tensorflow.python.framework import ops
  20. from tensorflow.python.ops import init_ops
  21. from tensorflow.python.ops import clip_ops
  22. from tensorflow.python.ops import control_flow_ops
  23. from tensorflow.python.ops import gradients
  24. from tensorflow.python.ops import variables
  25. from tensorflow.python.ops import variable_scope as vs
  26. from tensorflow.contrib.layers import optimizers
  27. def train(session,
  28. train_op,
  29. loss,
  30. global_step,
  31. feed_dict_fn,
  32. steps,
  33. monitor,
  34. summary_writer=None,
  35. summaries=None,
  36. feed_params_fn=None):
  37. """Trains a model for given number of steps, given feed_dict function.
  38. Args:
  39. session: Session object.
  40. train: Tensor, trains model.
  41. loss: Tensor, loss value.
  42. global_step: Tensor, global step of the model.
  43. feed_dict_fn: Function that will return a feed dictionary.
  44. summary_writer: SummaryWriter object to use for writing summaries.
  45. steps: Number of steps to run.
  46. monitor: Monitor object to track training progress and induce early
  47. stopping
  48. summaries: Joined object of all summaries that should be ran.
  49. """
  50. for step in xrange(steps):
  51. feed_dict = feed_dict_fn()
  52. if summaries is not None:
  53. global_step_value, loss_value, summ, _ = session.run(
  54. [global_step, loss, summaries, train_op],
  55. feed_dict=feed_dict)
  56. else:
  57. global_step_value, loss_value, _ = session.run(
  58. [global_step, loss, train_op],
  59. feed_dict=feed_dict)
  60. monitor.update(step,
  61. global_step_value,
  62. loss_value,
  63. session,
  64. feed_params_fn,
  65. loss_expression_tensor=loss)
  66. if summaries is not None and summary_writer and summ is not None:
  67. summary_writer.add_summary(summ, global_step_value)
  68. if monitor.monitor_inducing_stop():
  69. break