Esempio n. 1
0
 def test_logging_trainable(self):
   with ops.Graph().as_default() as g, self.test_session(g):
     var = variables.Variable(constant_op.constant(42.0), name='foo')
     var.initializer.run()
     cof = constant_op.constant(1.0)
     loss = math_ops.subtract(
         math_ops.multiply(var, cof), constant_op.constant(1.0))
     train_step = gradient_descent.GradientDescentOptimizer(0.5).minimize(loss)
     ops.get_default_session().run(train_step)
     self._run_monitor(learn.monitors.LoggingTrainable('foo'))
     self.assertRegexpMatches(str(self.logged_message), var.name)
Esempio n. 2
0
 def test_logging_trainable(self):
   with ops.Graph().as_default() as g, self.test_session(g):
     var = variables.Variable(constant_op.constant(42.0), name='foo')
     var.initializer.run()
     cof = constant_op.constant(1.0)
     loss = math_ops.subtract(
         math_ops.multiply(var, cof), constant_op.constant(1.0))
     train_step = gradient_descent.GradientDescentOptimizer(0.5).minimize(loss)
     ops.get_default_session().run(train_step)
     self._run_monitor(learn.monitors.LoggingTrainable('foo'))
     self.assertRegexpMatches(str(self.logged_message), var.name)
Esempio n. 3
0
def _to_numpy(a):
    """Converts tensors to numpy arrays.

  Converts Tensors and EagerTensors to numpy arrays.
  When eager execution is enabled, converts IndexedSlices
  to IndexedSlicesValue with numpy indices/values

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run().
    If a is IndexedSlices and eager execution is enabled, calls numpy() on a's
    fields. Otherwise returns a unchanged.
  """
    if isinstance(a, ops.EagerTensor):
        return a.numpy()
    if isinstance(a, ops.Tensor):
        sess = ops.get_default_session()
        return sess.run(a)
    if isinstance(a, ops.IndexedSlices) and context.executing_eagerly():
        return ops.IndexedSlicesValue(indices=[x.numpy() for x in a.indices],
                                      values=[x.numpy() for x in a.values],
                                      dense_shape=a.dense_shape)
    return a
def assert_bijective_and_finite(bijector, x, y, atol=0, rtol=1e-5, sess=None):
  """Assert that forward/inverse (along with jacobians) are inverses and finite.

  It is recommended to use x and y values that are very very close to the edge
  of the Bijector's domain.

  Args:
    bijector:  A Bijector instance.
    x:  np.array of values in the domain of bijector.forward.
    y:  np.array of values in the domain of bijector.inverse.
    atol:  Absolute tolerance.
    rtol:  Relative tolerance.
    sess:  TensorFlow session.  Defaults to the default session.

  Raises:
    AssertionError:  If tests fail.
  """
  sess = sess or ops.get_default_session()

  # These are the incoming points, but people often create a crazy range of
  # values for which these end up being bad, especially in 16bit.
  assert_finite(x)
  assert_finite(y)

  f_x = bijector.forward(x)
  g_y = bijector.inverse(y)

  [
      x_from_x,
      y_from_y,
      ildj_f_x,
      fldj_x,
      ildj_y,
      fldj_g_y,
      f_x_v,
      g_y_v,
  ] = sess.run([
      bijector.inverse(f_x),
      bijector.forward(g_y),
      bijector.inverse_log_det_jacobian(f_x),
      bijector.forward_log_det_jacobian(x),
      bijector.inverse_log_det_jacobian(y),
      bijector.forward_log_det_jacobian(g_y),
      f_x,
      g_y,
  ])

  assert_finite(x_from_x)
  assert_finite(y_from_y)
  assert_finite(ildj_f_x)
  assert_finite(fldj_x)
  assert_finite(ildj_y)
  assert_finite(fldj_g_y)
  assert_finite(f_x_v)
  assert_finite(g_y_v)

  np.testing.assert_allclose(x_from_x, x, atol=atol, rtol=rtol)
  np.testing.assert_allclose(y_from_y, y, atol=atol, rtol=rtol)
  np.testing.assert_allclose(-ildj_f_x, fldj_x, atol=atol, rtol=rtol)
  np.testing.assert_allclose(-ildj_y, fldj_g_y, atol=atol, rtol=rtol)
Esempio n. 5
0
    def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder):
        sess = ops.get_default_session()
        shape = list(shape)

        # Either 1 or 2 matrices, depending.
        num_operators = rng.randint(low=1, high=3)
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(shape, dtype, force_well_conditioned=True)
            for _ in range(num_operators)
        ]

        if use_placeholder:
            matrices_ph = [array_ops.placeholder(dtype=dtype) for _ in range(num_operators)]
            # Evaluate here because (i) you cannot feed a tensor, and (ii)
            # values are random and we want the same value used for both mat and
            # feed_dict.
            matrices = sess.run(matrices)
            operator = linalg.LinearOperatorComposition([linalg.LinearOperatorMatrix(m_ph) for m_ph in matrices_ph])
            feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
        else:
            operator = linalg.LinearOperatorComposition([linalg.LinearOperatorMatrix(m) for m in matrices])
            feed_dict = None

        # Convert back to Tensor.  Needed if use_placeholder, since then we have
        # already evaluated each matrix to a numpy array.
        apply_order_list = list(reversed(matrices))
        mat = ops.convert_to_tensor(apply_order_list[0])
        for other_mat in apply_order_list[1:]:
            mat = math_ops.matmul(other_mat, mat)

        return operator, mat, feed_dict
  def _operator_and_matrix(self, build_info, dtype, use_placeholder):
    sess = ops.get_default_session()
    shape = list(build_info.shape)

    # Either 1 or 2 matrices, depending.
    num_operators = rng.randint(low=1, high=3)
    matrices = [
        linear_operator_test_util.random_positive_definite_matrix(
            shape, dtype, force_well_conditioned=True)
        for _ in range(num_operators)
    ]

    lin_op_matrices = matrices

    if use_placeholder:
      lin_op_matrices = [
          array_ops.placeholder_with_default(
              matrix, shape=None) for matrix in matrices]

    operator = linalg.LinearOperatorComposition(
        [linalg.LinearOperatorFullMatrix(l) for l in lin_op_matrices],
        is_square=True)

    matmul_order_list = list(reversed(matrices))
    mat = matmul_order_list[0]
    for other_mat in matmul_order_list[1:]:
      mat = math_ops.matmul(other_mat, mat)

    return operator, mat
Esempio n. 7
0
def initialize(
    graph=None,  # pylint: disable=redefined-outer-name
    session=None):
  """Initializes summary writing for graph execution mode.

  This helper method provides a higher-level alternative to using
  @{tf.contrib.summary.summary_writer_initializer_op} and
  @{tf.contrib.summary.graph}.

  Most users will also want to call @{tf.train.create_global_step}
  which can happen before or after this function is called.

  Args:
    graph: A @{tf.Graph} or @{tf.GraphDef} to output to the writer.
      This function will not write the default graph by default. When
      writing to an event log file, the associated step will be zero.
    session: So this method can call @{tf.Session.run}. This defaults
      to @{tf.get_default_session}.

  Raises:
    RuntimeError: If in eager mode, or if the current thread has no
      default @{tf.contrib.summary.SummaryWriter}.
    ValueError: If session wasn't passed and no default session.
  """
  if context.context().summary_writer_resource is None:
    raise RuntimeError("No default tf.contrib.summary.SummaryWriter found")
  if session is None:
    session = ops.get_default_session()
    if session is None:
      raise ValueError("session must be passed if no default session exists")
  session.run(summary_writer_initializer_op())
  if graph is not None:
    data = _serialize_graph(graph)
    x = array_ops.placeholder(dtypes.string)
    session.run(_graph(x, 0), feed_dict={x: data})
Esempio n. 8
0
def _to_numpy(a):
  """Converts tensors to numpy arrays.

  Converts Tensors and EagerTensors to numpy arrays.
  When eager execution is enabled, converts IndexedSlices
  to IndexedSlicesValue with numpy indices/values

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run().
    If a is IndexedSlices and eager execution is enabled, calls numpy() on a's
    fields. Otherwise returns a unchanged.
  """
  if isinstance(a, ops.EagerTensor):
    return a.numpy()
  if isinstance(a, ops.Tensor):
    sess = ops.get_default_session()
    return sess.run(a)
  if isinstance(a, ops.IndexedSlices) and context.executing_eagerly():
    return ops.IndexedSlicesValue(
        indices=[x.numpy() for x in a.indices],
        values=[x.numpy() for x in a.values],
        dense_shape=a.dense_shape)
  return a
Esempio n. 9
0
    def applyOptimizer(self, opt, dtype, steps=5, is_sparse=False):
        if is_sparse:
            var0 = variables.Variable([[0.0], [0.0]], dtype=dtype)
            var1 = variables.Variable([[0.0], [0.0]], dtype=dtype)
            grads0 = ops.IndexedSlices(
                constant_op.constant([0.1], shape=[1, 1], dtype=dtype),
                constant_op.constant([0]), constant_op.constant([2, 1]))
            grads1 = ops.IndexedSlices(
                constant_op.constant([0.02], shape=[1, 1], dtype=dtype),
                constant_op.constant([1]), constant_op.constant([2, 1]))
        else:
            var0 = variables.Variable([0.0, 0.0], dtype=dtype)
            var1 = variables.Variable([0.0, 0.0], dtype=dtype)
            grads0 = constant_op.constant([0.1, 0.2], dtype=dtype)
            grads1 = constant_op.constant([0.01, 0.02], dtype=dtype)

        update = opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
        variables.global_variables_initializer().run()

        sess = ops.get_default_session()
        v0_val, v1_val = self.evaluate([var0, var1])
        if is_sparse:
            self.assertAllCloseAccordingToType([[0.0], [0.0]], v0_val)
            self.assertAllCloseAccordingToType([[0.0], [0.0]], v1_val)
        else:
            self.assertAllCloseAccordingToType([0.0, 0.0], v0_val)
            self.assertAllCloseAccordingToType([0.0, 0.0], v1_val)

        # Run Ftrl for a few steps
        for _ in range(steps):
            update.run()

        v0_val, v1_val = self.evaluate([var0, var1])
        return v0_val, v1_val
Esempio n. 10
0
def start_queue_runners(sess=None, coord=None, daemon=True, start=True,
                        collection=ops.GraphKeys.QUEUE_RUNNERS):
  """Starts all queue runners collected in the graph.

  This is a companion method to `add_queue_runner()`.  It just starts
  threads for all queue runners collected in the graph.  It returns
  the list of all threads.

  Args:
    sess: `Session` used to run the queue ops.  Defaults to the
      default session.
    coord: Optional `Coordinator` for coordinating the started threads.
    daemon: Whether the threads should be marked as `daemons`, meaning
      they don't block program exit.
    start: Set to `False` to only create the threads, not start them.
    collection: A `GraphKey` specifying the graph collection to
      get the queue runners from.  Defaults to `GraphKeys.QUEUE_RUNNERS`.

  Returns:
    A list of threads.
  """
  if sess is None:
    sess = ops.get_default_session()
    if not sess:
      raise ValueError("Cannot start queue runners: No default session is "
                       "registered. Use `with sess.as_default()` or pass an "
                       "explicit session to tf.start_queue_runners(sess=sess)")
  with sess.graph.as_default():
    threads = []
    for qr in ops.get_collection(collection):
      threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon,
                                       start=start))
  return threads
Esempio n. 11
0
    def run_evaluation(init_op, call_op, results_op, sess=None):
        """Convenience method for running the ops returned by evaluate_on_dataset.

    Args:
      init_op: An op that initializes/resets evaluation state.
      call_op: An op that updates evaluation state on a mini-batch of examples.
        Must generate an tf.errors.OutOfRangeError when done.
      results_op: A dictionary of tensors that compute the final evaluation
        results from the evaluation state.
      sess: The Session to run the evaluation in. Defaults to the default
        Session.

    Returns:
      A dictionary of values, parallel to results_op.

    Raises:
      RuntimeError: if eager execution is enabled.

    @compatibility(eager)
    Only for graph execution.
    @end_compatibility
    """
        if context.executing_eagerly():
            raise RuntimeError("Evaluator.run_evaluation() not supported when "
                               "eager execution is enabled.")
        sess = sess or ops.get_default_session()
        sess.run(init_op)
        try:
            while True:
                sess.run(call_op)
        except errors_impl.OutOfRangeError:
            pass
        return sess.run(results_op)
Esempio n. 12
0
def initialize(
    graph=None,  # pylint: disable=redefined-outer-name
    session=None):
  """Initializes summary writing for graph execution mode.

  This helper method provides a higher-level alternative to using
  @{tf.contrib.summary.summary_writer_initializer_op} and
  @{tf.contrib.summary.graph}.

  Most users will also want to call @{tf.train.create_global_step}
  which can happen before or after this function is called.

  Args:
    graph: A @{tf.Graph} or @{tf.GraphDef} to output to the writer.
      This function will not write the default graph by default. When
      writing to an event log file, the associated step will be zero.
    session: So this method can call @{tf.Session.run}. This defaults
      to @{tf.get_default_session}.

  Raises:
    RuntimeError: If in eager mode, or if the current thread has no
      default @{tf.contrib.summary.SummaryWriter}.
    ValueError: If session wasn't passed and no default session.
  """
  if context.context().summary_writer_resource is None:
    raise RuntimeError("No default tf.contrib.summary.SummaryWriter found")
  if session is None:
    session = ops.get_default_session()
    if session is None:
      raise ValueError("session must be passed if no default session exists")
  session.run(summary_writer_initializer_op())
  if graph is not None:
    data = _serialize_graph(graph)
    x = array_ops.placeholder(dtypes.string)
    session.run(_graph(x, 0), feed_dict={x: data})
Esempio n. 13
0
  def initialize_or_restore(self, session=None):
    """Run operations to initialize or restore objects in the dependency graph.

    Any objects in the dependency graph which have initializers but are not in
    the checkpoint will have those initializers run, unless those variables are
    being restored by a later call to `tf.train.Checkpoint.restore()`.

    This method has a sibling in `InitializationOnlyStatus` which instead
    initializes variables. That type is returned if no checkpoint is specified
    in `Saver.restore`.

    Args:
      session: The session to run init/restore ops in. If `None`, uses the
        default session.
    """
    if context.executing_eagerly():
      return  # Initialization and restoration ops are run eagerly
    if session is None:
      session = ops.get_default_session()
    all_objects = list_objects(self._root_checkpointable)
    already_initialized_objects = set(
        self._checkpoint.object_by_proto_id.values())
    initializers_for_non_restored_variables = [
        c.initializer for c in all_objects
        if hasattr(c, "initializer")
        and c not in already_initialized_objects
        and (getattr(c, "_update_uid", self._checkpoint.restore_uid - 1)
             < self._checkpoint.restore_uid)]
    self.run_restore_ops(session=session)
    session.run(initializers_for_non_restored_variables)
Esempio n. 14
0
    def optimize(self, session=None, step_callback=None, feed_dict=None):
        """
        Runs optimization on a scalar `Tensor` defined by `init_optimize`.

        Args:
          **run_kwargs: kwargs to pass to `session.run`.
        """
        session = session or ops.get_default_session()
        step_callback = step_callback or (lambda xk: None)

        # Perform minimization.
        self._feed_dict = feed_dict if feed_dict is not None else {}
        initial_packed_var_val = session.run(self._packed_var)
        packed_var_val = self._minimize(initial_val=initial_packed_var_val,
                                        packed_bounds=self._packed_bounds,
                                        optimizer_kwargs=self.optimizer_kwargs,
                                        step_callback=step_callback,
                                        **self._minimize_args)

        var_vals = [
            packed_var_val[packing_slice]
            for packing_slice in self._packing_slices
        ]

        # Set optimization variables to their new values.
        run_feed_dict = dict(zip(self._update_placeholders, var_vals))
        session.run(self._var_updates, feed_dict=run_feed_dict)
Esempio n. 15
0
def assert_bijective_and_finite(bijector, x, y, atol=0, rtol=1e-5, sess=None):
  """Assert that forward/inverse (along with jacobians) are inverses and finite.

  It is recommended to use x and y values that are very very close to the edge
  of the Bijector's domain.

  Args:
    bijector:  A Bijector instance.
    x:  np.array of values in the domain of bijector.forward.
    y:  np.array of values in the domain of bijector.inverse.
    atol:  Absolute tolerance.
    rtol:  Relative tolerance.
    sess:  TensorFlow session.  Defaults to the default session.

  Raises:
    AssertionError:  If tests fail.
  """
  sess = sess or ops.get_default_session()

  # These are the incoming points, but people often create a crazy range of
  # values for which these end up being bad, especially in 16bit.
  assert_finite(x)
  assert_finite(y)

  f_x = bijector.forward(x)
  g_y = bijector.inverse(y)

  [
      x_from_x,
      y_from_y,
      ildj_f_x,
      fldj_x,
      ildj_y,
      fldj_g_y,
      f_x_v,
      g_y_v,
  ] = sess.run([
      bijector.inverse(f_x),
      bijector.forward(g_y),
      bijector.inverse_log_det_jacobian(f_x),
      bijector.forward_log_det_jacobian(x),
      bijector.inverse_log_det_jacobian(y),
      bijector.forward_log_det_jacobian(g_y),
      f_x,
      g_y,
  ])

  assert_finite(x_from_x)
  assert_finite(y_from_y)
  assert_finite(ildj_f_x)
  assert_finite(fldj_x)
  assert_finite(ildj_y)
  assert_finite(fldj_g_y)
  assert_finite(f_x_v)
  assert_finite(g_y_v)

  np.testing.assert_allclose(x_from_x, x, atol=atol, rtol=rtol)
  np.testing.assert_allclose(y_from_y, y, atol=atol, rtol=rtol)
  np.testing.assert_allclose(-ildj_f_x, fldj_x, atol=atol, rtol=rtol)
  np.testing.assert_allclose(-ildj_y, fldj_g_y, atol=atol, rtol=rtol)
Esempio n. 16
0
  def initialize_or_restore(self, session=None):
    """Runs initialization ops for variables.

    Objects which would be saved by `Saver.save` will be initialized, unless
    those variables are being restored by a later call to
    `tf.train.Checkpoint.restore()`.

    This method does nothing when executing eagerly (initializers get run
    eagerly).

    Args:
      session: The session to run initialization ops in. If `None`, uses the
        default session.
    """
    if context.executing_eagerly():
      return  # run eagerly
    if session is None:
      session = ops.get_default_session()
    checkpointable_objects = list_objects(self._root_checkpointable)
    initializers = [
        c.initializer for c in checkpointable_objects
        if hasattr(c, "initializer") and c.initializer is not None
        and (getattr(c, "_update_uid", self._restore_uid - 1)
             < self._restore_uid)]
    session.run(initializers)
Esempio n. 17
0
    def init_optimize(self,
                      session=None,
                      fetches=None,
                      loss_callback=None,
                      **optimizer_kwargs):
        """Create intermediate tensors for optimizing a scalar `Tensor`.

        Variables subject to optimization are updated in-place at the end of
        optimization.

        Note that this method does *not* just return a minimization `Op`, unlike
        `Optimizer.minimize()`; instead it actually performs minimization by
        executing commands to control a `Session`.

        Args:
          session: A `Session` instance.
          feed_dict: A feed dict to be passed to calls to `session.run`.
          fetches: A list of `Tensor`s to fetch and supply to `loss_callback`
            as positional arguments.
          step_callback: A function to be called at each optimization step;
            arguments are the current values of all optimization variables
            flattened into a single vector.
          loss_callback: A function to be called every time the loss and gradients
            are computed, with evaluated fetches supplied as positional arguments.
          **optimizer_kwargs: kwargs to pass to ScipyOptimizer `minimize` method.
        """
        session = session or ops.get_default_session()
        fetches = fetches or []

        loss_callback = loss_callback or (lambda *fetches: None)

        # Get initial value from TF session.
        self._initialize_updated_shapes(session)
        self._make_minimize_tensors(session, fetches, loss_callback,
                                    **optimizer_kwargs)
def _prepare(f, xs_dtypes):
  """Return a function that executes 'f'.

    In TF 2.x, this is the same as `f`.
    In TF 1.x, returns a Python function that executes the graph defined by `f`
    in a Session.

  Args:
    f: the function.
    xs_dtypes: dtypes of f's arguments.

  Returns:
    a function that will be evaluated in both graph and eager mode
  """
  if context.executing_eagerly():

    def decorated_eager(*xs_data):
      return f(*map(ops.convert_to_tensor, xs_data))

    return decorated_eager
  xs = [array_ops.placeholder(x_dtype) for x_dtype in xs_dtypes]
  y = f(*xs)
  sess = ops.get_default_session()
  def decorated_graph(*xs_data):
    xs_data = [_to_numpy(a) for a in xs_data]
    return sess.run(y, feed_dict=dict(zip(xs, xs_data)))
  return decorated_graph
Esempio n. 19
0
 def run_restore_ops(self, session=None):
   """Load the name-based training checkpoint using a new `tf.train.Saver`."""
   if session is None and not context.executing_eagerly():
     session = ops.get_default_session()
   with ops.device("/cpu:0"):
     saver_lib.Saver(self._object_saver._global_variable_names()).restore(  # pylint: disable=protected-access
         sess=session, save_path=self._save_path)
def _to_numpy(a):
  """Converts Tensors, EagerTensors, and IndexedSlicesValue to numpy arrays.

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run(). If a is IndexedSlicesValue, constructs the corresponding
    dense numpy array. Otherwise returns a unchanged.
  """
  if isinstance(a, ops.EagerTensor):
    return a.numpy()
  if isinstance(a, ops.Tensor):
    sess = ops.get_default_session()
    return sess.run(a)
  if isinstance(a, ops.IndexedSlicesValue):
    arr = np.zeros(a.dense_shape)
    assert len(a.values) == len(a.indices), (
        "IndexedSlicesValue has %s value slices but %s indices\n%s" %
        (a.values, a.indices, a))
    for values_slice, index in zip(a.values, a.indices):
      assert 0 <= index < len(arr), (
          "IndexedSlicesValue has invalid index %s\n%s" % (index, a))
      arr[index] += values_slice
    return arr
  return a
    def _operator_and_matrix(self, build_info, dtype, use_placeholder):
        sess = ops.get_default_session()
        shape = list(build_info.shape)

        # Either 1 or 2 matrices, depending.
        num_operators = rng.randint(low=1, high=3)
        matrices = [
            linear_operator_test_util.random_positive_definite_matrix(
                shape, dtype, force_well_conditioned=True)
            for _ in range(num_operators)
        ]

        lin_op_matrices = matrices

        if use_placeholder:
            lin_op_matrices = [
                array_ops.placeholder_with_default(matrix, shape=None)
                for matrix in matrices
            ]

        operator = linalg.LinearOperatorComposition(
            [linalg.LinearOperatorFullMatrix(l) for l in lin_op_matrices],
            is_square=True)

        matmul_order_list = list(reversed(matrices))
        mat = matmul_order_list[0]
        for other_mat in matmul_order_list[1:]:
            mat = math_ops.matmul(other_mat, mat)

        return operator, mat
Esempio n. 22
0
def get_session(op_input_list=()):
    """Returns the session object for the current thread."""
    global _SESSION

    def valid_session(session):
        if session is None:
            return False
        if not isinstance(session, tfe.Session):
            return False
        if session.graph is not _current_graph(op_input_list):
            return False
        return True

    if ops.inside_function():
        raise RuntimeError(
            "Cannot get session inside Tensorflow graph function.")

    # return any suitable session already specified
    session = getattr(_SESSION, "session", None)
    if valid_session(session):
        return session

    # return default TF session if of right type
    session = ops.get_default_session()
    if valid_session(session):
        return session

    # we don't have a suitable session, create and cache a new one
    _SESSION.session = tfe.Session()
    assert valid_session(_SESSION.session)
    return _SESSION.session
Esempio n. 23
0
  def applyOptimizer(self, opt, dtype, steps=5, is_sparse=False):
    if is_sparse:
      var0 = variables.Variable([[0.0], [0.0]], dtype=dtype)
      var1 = variables.Variable([[0.0], [0.0]], dtype=dtype)
      grads0 = ops.IndexedSlices(
          constant_op.constant([0.1], shape=[1, 1], dtype=dtype),
          constant_op.constant([0]), constant_op.constant([2, 1]))
      grads1 = ops.IndexedSlices(
          constant_op.constant([0.02], shape=[1, 1], dtype=dtype),
          constant_op.constant([1]), constant_op.constant([2, 1]))
    else:
      var0 = variables.Variable([0.0, 0.0], dtype=dtype)
      var1 = variables.Variable([0.0, 0.0], dtype=dtype)
      grads0 = constant_op.constant([0.1, 0.2], dtype=dtype)
      grads1 = constant_op.constant([0.01, 0.02], dtype=dtype)

    update = opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
    variables.global_variables_initializer().run()

    sess = ops.get_default_session()
    v0_val, v1_val = sess.run([var0, var1])
    if is_sparse:
      self.assertAllCloseAccordingToType([[0.0], [0.0]], v0_val)
      self.assertAllCloseAccordingToType([[0.0], [0.0]], v1_val)
    else:
      self.assertAllCloseAccordingToType([0.0, 0.0], v0_val)
      self.assertAllCloseAccordingToType([0.0, 0.0], v1_val)

    # Run Ftrl for a few steps
    for _ in range(steps):
      update.run()

    v0_val, v1_val = sess.run([var0, var1])
    return v0_val, v1_val
Esempio n. 24
0
 def run_restore_ops(self, session=None):
   """Run operations to restore objects in the dependency graph."""
   if context.executing_eagerly():
     return  # Run eagerly
   if session is None:
     session = ops.get_default_session()
   session.run(self._checkpoint.restore_ops, feed_dict=self._feed_dict)
Esempio n. 25
0
    def save(self, checkpoint_number=None):
        """Creates a new checkpoint and manages it.

    Args:
      checkpoint_number: An optional integer, or an integer-dtype `Variable` or
        `Tensor`, used to number the checkpoint. If `None` (default),
        checkpoints are numbered using `checkpoint.save_counter`. Even if
        `checkpoint_number` is provided, `save_counter` is still incremented. A
        user-provided `checkpoint_number` is not incremented even if it is a
        `Variable`.

    Returns:
      The path to the new checkpoint. It is also recorded in the `checkpoints`
      and `latest_checkpoint` properties.
    """
        # Save counter logic duplicated from tf.train.Checkpoint, soon to diverge
        # slightly with a custom numbering option.
        if context.executing_eagerly():
            save_counter = self._checkpoint.save_counter
            save_counter.assign_add(1)
            session = None
        else:
            session = ops.get_default_session()

            def _initializing_creator(next_creator, **kwargs):
                """Initialize the save counter if it has been newly created."""
                v = next_creator(**kwargs)
                session.run(v.initializer)
                return v

            with variable_scope.variable_creator_scope(_initializing_creator):
                save_counter = self._checkpoint.save_counter
            if self._save_counter_assign is None:
                self._save_counter_assign = save_counter.assign_add(
                    1, read_value=False)
            session.run(self._save_counter_assign)
        if checkpoint_number is None:
            checkpoint_number = save_counter
        if not isinstance(checkpoint_number, compat.integral_types):
            checkpoint_number = training_util.global_step(
                sess=session, global_step_tensor=checkpoint_number)
        prefix = "%s-%d" % (self._prefix, checkpoint_number)
        save_path = self._checkpoint.write(prefix)
        timestamp = time.time()
        # If this is an overwritten checkpoint we were previously tracking, delete
        # and reinsert it to make sure it goes to the end of the queue.
        if save_path in self._maybe_delete:
            del self._maybe_delete[save_path]
        self._maybe_delete[save_path] = timestamp
        self._latest_checkpoint = save_path
        # Before deleting anything we update the Checkpoint proto with the new
        # checkpoint. We'll go back and correct it after cleaning up old files, but
        # a preemption while deleting will be more likely to see the new checkpoint
        # this way.
        self._record_state()
        self._sweep()
        # Write out the Checkpoint proto a second time, now without the deleted
        # checkpoints.
        self._record_state()
        return save_path
Esempio n. 26
0
 def _run_monitor(self,
                  monitor,
                  num_epochs=3,
                  num_steps_per_epoch=10,
                  pass_max_steps=True):
   if pass_max_steps:
     max_steps = num_epochs * num_steps_per_epoch - 1
   else:
     max_steps = None
   monitor.begin(max_steps=max_steps)
   for epoch in xrange(num_epochs):
     monitor.epoch_begin(epoch)
     should_stop = False
     step = epoch * num_steps_per_epoch
     next_epoch_step = step + num_steps_per_epoch
     while (not should_stop) and (step < next_epoch_step):
       tensors = monitor.step_begin(step)
       output = ops.get_default_session().run(tensors) if tensors else {}
       output = dict(
           zip([t.name if isinstance(t, ops.Tensor) else t for t in tensors],
               output))
       should_stop = monitor.step_end(step=step, output=output)
       monitor.post_step(step=step, session=None)
       step += 1
     monitor.epoch_end(epoch)
   monitor.end()
Esempio n. 27
0
def _prepare(f, xs_dtypes, xs_shapes):
    """Return a function that executes 'f'.

    In TF 2.x, this is the same as `f`.
    In TF 1.x, returns a Python function that executes the graph defined by `f`
    in a Session.

  Args:
    f: the function.
    xs_dtypes: dtypes of f's arguments.
    xs_shapes: shapes of f's arguments.

  Returns:
  """
    if context.executing_eagerly():

        def decorated_eager(*xs_data):
            return f(*map(ops.convert_to_tensor, xs_data))

        return decorated_eager
    xs = [
        array_ops.placeholder(x_dtype, shape=x_shape)
        for x_dtype, x_shape in zip(xs_dtypes, xs_shapes)
    ]
    y = f(*xs)
    sess = ops.get_default_session()

    def decorated_graph(*xs_data):
        xs_data = [_to_numpy(a) for a in xs_data]
        return sess.run(y, feed_dict=dict(zip(xs, xs_data)))

    return decorated_graph
Esempio n. 28
0
def _to_numpy(a):
    """Converts Tensors, EagerTensors, and IndexedSlicesValue to numpy arrays.

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run(). If a is IndexedSlicesValue, constructs the corresponding
    dense numpy array. Otherwise returns a unchanged.
  """
    if isinstance(a, ops.EagerTensor):
        return a.numpy()
    if isinstance(a, ops.Tensor):
        sess = ops.get_default_session()
        return sess.run(a)
    if isinstance(a, indexed_slices.IndexedSlicesValue):
        arr = np.zeros(a.dense_shape)
        assert len(a.values) == len(a.indices), (
            "IndexedSlicesValue has %s value slices but %s indices\n%s" %
            (a.values, a.indices, a))
        for values_slice, index in zip(a.values, a.indices):
            assert 0 <= index < len(arr), (
                "IndexedSlicesValue has invalid index %s\n%s" % (index, a))
            arr[index] += values_slice
        return arr
    return a
Esempio n. 29
0
  def run_evaluation(init_op, call_op, results_op, sess=None):
    """Convenience method for running the ops returned by evaluate_on_dataset.

    Args:
      init_op: An op that initializes/resets evaluation state.
      call_op: An op that updates evaluation state on a mini-batch of examples.
        Must generate an tf.errors.OutOfRangeError when done.
      results_op: A dictionary of tensors that compute the final evaluation
        results from the evaluation state.
      sess: The Session to run the evaluation in. Defaults to the default
        Session.

    Returns:
      A dictionary of values, parallel to results_op.

    Raises:
      RuntimeError: if eager execution is enabled.

    @compatibility(eager)
    Only for graph execution.
    @end_compatibility
    """
    if context.in_eager_mode():
      raise RuntimeError("Evaluator.run_evaluation() not supported when "
                         "eager execution is enabled.")
    sess = sess or ops.get_default_session()
    sess.run(init_op)
    try:
      while True:
        sess.run(call_op)
    except errors_impl.OutOfRangeError:
      pass
    return sess.run(results_op)
Esempio n. 30
0
 def _run_monitor(self,
                  monitor,
                  num_epochs=3,
                  num_steps_per_epoch=10,
                  pass_max_steps=True):
   if pass_max_steps:
     max_steps = num_epochs * num_steps_per_epoch - 1
   else:
     max_steps = None
   monitor.begin(max_steps=max_steps)
   for epoch in xrange(num_epochs):
     monitor.epoch_begin(epoch)
     should_stop = False
     step = epoch * num_steps_per_epoch
     next_epoch_step = step + num_steps_per_epoch
     while (not should_stop) and (step < next_epoch_step):
       tensors = monitor.step_begin(step)
       output = ops.get_default_session().run(tensors) if tensors else {}
       output = dict(
           zip([t.name if isinstance(t, ops.Tensor) else t for t in tensors],
               output))
       should_stop = monitor.step_end(step=step, output=output)
       monitor.post_step(step=step, session=None)
       step += 1
     monitor.epoch_end(epoch)
   monitor.end()
Esempio n. 31
0
def broadcast_object_fn(root_rank=0, session=None, name=None):
    name = name or 'broadcast_object_fn'

    sz = tf.placeholder(tf.int32, [1], name='bcast_object_size')
    bcast_size = broadcast(sz, root_rank, name + '.sz')

    t = tf.placeholder(tf.uint8, [None], name='bcast_object_data')
    bcast_data = broadcast(t, root_rank, name + '.t')

    session = session or ops.get_default_session()

    def _bcast(obj):
        if rank() == root_rank:
            b = io.BytesIO()
            cloudpickle.dump(obj, b)
            t_ = bytearray(b.getvalue())
            sz_ = [len(t_)]
            session.run(bcast_size, feed_dict={sz: sz_})
        else:
            sz_ = [0]
            sz_ = session.run(bcast_size, feed_dict={sz: sz_})
            t_ = np.zeros(sz_, dtype=np.uint8)

        t_ = session.run(bcast_data, feed_dict={t: t_})

        if rank() != root_rank:
            buf = io.BytesIO(t_.tobytes())
            obj = cloudpickle.load(buf)

        return obj

    return _bcast
Esempio n. 32
0
 def _evaluate(tensors, feed_dict):
   sess = ops.get_default_session()
   if sess is None:
     with self.test_session() as sess:
       return sess.run(tensors, feed_dict=feed_dict)
   else:
     return sess.run(tensors, feed_dict=feed_dict)
Esempio n. 33
0
 def run_restore_ops(self, session=None):
     """Load the name-based training checkpoint using a new `tf.train.Saver`."""
     if session is None and context.in_graph_mode():
         session = ops.get_default_session()
     saver_lib.Saver(self._object_saver._global_variable_names()).restore(  # pylint: disable=protected-access
         sess=session,
         save_path=self._save_path)
Esempio n. 34
0
    def applyOptimizer(self, opt, steps=5, is_sparse=False):
        if is_sparse:
            var0 = variables.Variable([[1.0], [2.0]])
            var1 = variables.Variable([[3.0], [4.0]])
            grads0 = ops.IndexedSlices(
                constant_op.constant([0.1], shape=[1, 1]),
                constant_op.constant([0]), constant_op.constant([2, 1]))
            grads1 = ops.IndexedSlices(
                constant_op.constant([0.02], shape=[1, 1]),
                constant_op.constant([1]), constant_op.constant([2, 1]))
        else:
            var0 = variables.Variable([1.0, 2.0])
            var1 = variables.Variable([3.0, 4.0])
            grads0 = constant_op.constant([0.1, 0.2])
            grads1 = constant_op.constant([0.01, 0.02])

        update = opt.apply_gradients(zip([grads0, grads1], [var0, var1]))
        self.evaluate(variables.global_variables_initializer())

        sess = ops.get_default_session()
        v0_val, v1_val = self.evaluate([var0, var1])
        if is_sparse:
            self.assertAllClose([[1.0], [2.0]], v0_val)
            self.assertAllClose([[3.0], [4.0]], v1_val)
        else:
            self.assertAllClose([1.0, 2.0], v0_val)
            self.assertAllClose([3.0, 4.0], v1_val)

        # Run ProximalAdagrad for a few steps
        for _ in range(steps):
            update.run()

        v0_val, v1_val = self.evaluate([var0, var1])
        return v0_val, v1_val
Esempio n. 35
0
def get_session(op_input_list=()):
    """Returns the session object for the current thread."""
    global _SESSION
    if getattr(_SESSION, 'session', None) is not None:
        return _SESSION.session
    default_session = ops.get_default_session()
    if default_session is not None:
        # If the default session is a TFE Session return this session
        if isinstance(default_session, tfe.Session()):
            return default_session
        if not isinstance(default_session, tfe.Session()):
            raise TypeError('The default session should be a tfe.Session(). '
                            'You are probably trying to run this graph with '
                            'tf.Session() instead of tfe.Session()')
    else:
        if ops.inside_function():
            raise RuntimeError(
                'Cannot get session inside Tensorflow graph function.')
            # If we don't have a session, or that session does not match the current
            # graph, create and cache a new session.
        if (getattr(_SESSION, 'session', None) is None or
                _SESSION.session.graph is not _current_graph(op_input_list)):
            _SESSION.session = tfe.Session()
        session = _SESSION.session
    return session
Esempio n. 36
0
  def save(self, file_prefix, session=None):
    """Save a training checkpoint.

    The saved checkpoint includes variables created by this object and any
    checkpointable objects it depends on at the time `Checkpoint.save()` is
    called.

    Args:
      file_prefix: A prefix to use for the checkpoint filenames
        (/path/to/directory/and_a_prefix). Names are generated based on this
        prefix and `Checkpoint.save_counter`.
      session: The session to evaluate variables in. Ignored when executing
        eagerly. If not provided when graph building, the default session is
        used.

    Returns:
      The full path to the checkpoint.
    """
    in_graph_mode = not context.executing_eagerly()
    if in_graph_mode:
      if session is None:
        session = ops.get_default_session()
      if self._save_counter is None:
        # When graph building, if this is a new save counter variable then it
        # needs to be initialized before assign_add. This is only an issue if
        # restore() has not been called first.
        session.run(self.save_counter.initializer)
    with ops.colocate_with(self.save_counter):
      assign_op = self.save_counter.assign_add(1)
    if in_graph_mode:
      session.run(assign_op)
    return self._saver.save(
        file_prefix=file_prefix,
        checkpoint_number=self.save_counter,
        session=session)
def start_queue_runners(sess=None, coord=None, daemon=True, start=True,
                        collection=ops.GraphKeys.QUEUE_RUNNERS):
  """Starts all queue runners collected in the graph.

  This is a companion method to `add_queue_runner()`.  It just starts
  threads for all queue runners collected in the graph.  It returns
  the list of all threads.

  Args:
    sess: `Session` used to run the queue ops.  Defaults to the
      default session.
    coord: Optional `Coordinator` for coordinating the started threads.
    daemon: Whether the threads should be marked as `daemons`, meaning
      they don't block program exit.
    start: Set to `False` to only create the threads, not start them.
    collection: A `GraphKey` specifying the graph collection to
      get the queue runners from.  Defaults to `GraphKeys.QUEUE_RUNNERS`.

  Raises:
    ValueError: if `sess` is None and there isn't any default session.
    TypeError: if `sess` is not a `tf.Session` object.

  Returns:
    A list of threads.

  Raises:
    RuntimeError: If called with eager execution enabled.
    ValueError: If called without a default `tf.Session` registered.

  @compatibility(eager)
  Not compatible with eager execution. To ingest data under eager execution,
  use the `tf.data` API instead.
  @end_compatibility
  """
  return
  if context.executing_eagerly():
    raise RuntimeError("Queues are not compatible with eager execution.")
  if sess is None:
    sess = ops.get_default_session()
    if not sess:
      raise ValueError("Cannot start queue runners: No default session is "
                       "registered. Use `with sess.as_default()` or pass an "
                       "explicit session to tf.start_queue_runners(sess=sess)")

  if not isinstance(sess, session.SessionInterface):
    # Following check is due to backward compatibility. (b/62061352)
    if sess.__class__.__name__ in [
        "MonitoredSession", "SingularMonitoredSession"]:
      return []
    raise TypeError("sess must be a `tf.Session` object. "
                    "Given class: {}".format(sess.__class__))

  with sess.graph.as_default():
    threads = []
    for qr in ops.get_collection(collection):
      threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon,
                                       start=start))
  return threads
  def save(self, file_prefix, checkpoint_number=None, session=None):
    """Save a training checkpoint.

    The saved checkpoint includes variables created by this object and any
    Checkpointable objects it depends on at the time `Saver.save()` is called.

    Args:
      file_prefix: A prefix to use for the checkpoint filenames
        (/path/to/directory/and_a_prefix). Names are generated based on this
        prefix and `checkpoint_number`, if provided.
      checkpoint_number: An integer variable or Tensor, used to number
        checkpoints. Typically this value is saved along with other variables in
        training checkpoints, which will happen automatically if it was created
        by `root_checkpointable` or one of its dependencies (via
        `Checkpointable._add_variable`).
      session: The session to evaluate variables in. Ignored when executing
        eagerly. If not provided when graph building, the default session is
        used.

    Returns:
      The full path to the checkpoint.
    """
    named_variables, graph_proto = _serialize_object_graph(
        self._root_checkpointable)
    if not context.executing_eagerly():
      if session is None:
        session = ops.get_default_session()
      if self._object_graph_feed_tensor is None:
        with ops.device("/cpu:0"):
          self._object_graph_feed_tensor = constant_op.constant(
              "", dtype=dtypes.string)
      object_graph_tensor = self._object_graph_feed_tensor
      feed_additions = {object_graph_tensor: graph_proto.SerializeToString()}
    else:
      session = None
      with ops.device("/cpu:0"):
        object_graph_tensor = constant_op.constant(
            graph_proto.SerializeToString(), dtype=dtypes.string)
      feed_additions = None
    assert _OBJECT_GRAPH_PROTO_KEY not in named_variables
    named_variables[_OBJECT_GRAPH_PROTO_KEY] = _NoRestoreSaveable(
        tensor=object_graph_tensor,
        name=_OBJECT_GRAPH_PROTO_KEY)
    if self._last_save_object_graph != graph_proto:
      if self._last_save_object_graph is not None:
        self._last_save_saver = _copy_saver_with_new_var_list(
            old_saver=self._last_save_saver, new_var_list=named_variables)
      else:
        self._last_save_saver = saver_lib.Saver(var_list=named_variables)
      self._last_save_object_graph = graph_proto
    with ops.device("/cpu:0"):
      save_path = self._last_save_saver.save(
          sess=_SessionWithFeedDictAdditions(
              session=session, feed_additions=feed_additions),
          save_path=file_prefix,
          write_meta_graph=False,
          global_step=checkpoint_number)
    return save_path
Esempio n. 39
0
def _compute_theoretical_jacobian(x, x_shape, x_data, dy, dy_shape, dx):
    """Computes the theoretical Jacobian for dy/dx.

  Computes the theoretical Jacobian using the ops generated by
  compute_gradient().

  Args:
    x: the tensor "x".
    x_shape: the dimensions of x as a tuple or an array of ints.
    x_data: a numpy parray as the input data for x
    dy: the tensor "dy".
    dy_shape: the dimensions of dy as a tuple or an array of ints.
    dx: Tensor or IndexedSlices representing dx

  Returns:
    A 2-d numpy array representing the Jacobian for dy/dx. It has "x_size" rows
    and "dy_size" columns where "x_size" is the number of elements in x and
    "dy_size" is the number of elements in dy.
  """
    # Complex vectors are treated as vectors of twice as many reals.
    if x.dtype.is_complex:
        x_shape = tuple(x_shape) + (2, )
    dy_factor = 2 if dy.dtype.is_complex else 1

    # To compute the jacobian, we treat x and y as one-dimensional vectors.
    x_size = _product(x_shape)
    x_val_size = _product(x_shape[1:])  # This is used for sparse gradients
    dy_size = _product(dy_shape) * dy_factor

    jacobian = np.zeros((x_size, dy_size),
                        dtype=x.dtype.real_dtype.as_numpy_dtype)
    # For each of the entry of dy, we set this to be 1 and
    # everything else to be 0 and compute the backprop -- this will give us one
    # one column of the Jacobian matrix.
    dy_data = np.zeros(dy_shape, dtype=dy.dtype.as_numpy_dtype)
    dy_data_flat = dy_data.ravel().view(dy.dtype.real_dtype.as_numpy_dtype)
    sess = ops.get_default_session()
    for col in range(dy_size):
        dy_data_flat[col] = 1
        if isinstance(dx, ops.IndexedSlices):
            backprop_indices, backprop_values = sess.run(
                [dx.indices, dx.values], feed_dict={
                    x: x_data,
                    dy: dy_data
                })
            for i, v in zip(backprop_indices, backprop_values):
                r_begin = i * x_val_size
                r_end = r_begin + x_val_size
                jacobian[r_begin:r_end, col] += v.flat
        else:
            assert isinstance(dx, ops.Tensor), "dx = " + str(dx)
            backprop = sess.run(dx, feed_dict={x: x_data, dy: dy_data})
            jacobian[:, col] = backprop.ravel().view(jacobian.dtype)
        dy_data_flat[col] = 0

    logging.vlog(1, "Theoretical Jacobian =\n%s", jacobian)
    return jacobian
def start_queue_runners(sess=None, coord=None, daemon=True, start=True,
                        collection=ops.GraphKeys.QUEUE_RUNNERS):
  """Starts all queue runners collected in the graph.

  This is a companion method to `add_queue_runner()`.  It just starts
  threads for all queue runners collected in the graph.  It returns
  the list of all threads.

  Args:
    sess: `Session` used to run the queue ops.  Defaults to the
      default session.
    coord: Optional `Coordinator` for coordinating the started threads.
    daemon: Whether the threads should be marked as `daemons`, meaning
      they don't block program exit.
    start: Set to `False` to only create the threads, not start them.
    collection: A `GraphKey` specifying the graph collection to
      get the queue runners from.  Defaults to `GraphKeys.QUEUE_RUNNERS`.

  Raises:
    ValueError: if `sess` is None and there isn't any default session.
    TypeError: if `sess` is not a `tf.Session` object.

  Returns:
    A list of threads.

  Raises:
    RuntimeError: If called with eager execution enabled.
    ValueError: If called without a default `tf.Session` registered.

  @compatibility(eager)
  Not compatible with eager execution. To ingest data under eager execution,
  use the `tf.data` API instead.
  @end_compatibility
  """
  if context.in_eager_mode():
    raise RuntimeError("Queues are not compatible with eager execution.")
  if sess is None:
    sess = ops.get_default_session()
    if not sess:
      raise ValueError("Cannot start queue runners: No default session is "
                       "registered. Use `with sess.as_default()` or pass an "
                       "explicit session to tf.start_queue_runners(sess=sess)")

  if not isinstance(sess, session.SessionInterface):
    # Following check is due to backward compatibility. (b/62061352)
    if sess.__class__.__name__ in [
        "MonitoredSession", "SingularMonitoredSession"]:
      return []
    raise TypeError("sess must be a `tf.Session` object. "
                    "Given class: {}".format(sess.__class__))

  with sess.graph.as_default():
    threads = []
    for qr in ops.get_collection(collection):
      threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon,
                                       start=start))
  return threads
 def evaluate(self, tensors):
     if _executing_eagerly():
         return self._eval_helper(tensors)
     sess = ops.get_default_session()
     if sess is None:
         with self.test_session(config=config) as sess:
             return sess.run(tensors)
     else:
         return sess.run(tensors)
Esempio n. 42
0
def evaluate_tensor(tensor):
    if tf.executing_eagerly():
        return tensor.numpy()
    else:
        sess = ops.get_default_session()
        if sess is None:
            with tf.Session() as sess:
                return sess.run(tensor)
        else:
            return sess.run(tensor)
 def save(self, file_prefix, session=None):
   assign_op = self.save_counter.assign_add(1)
   if context.in_graph_mode():
     if session is None:
       session = ops.get_default_session()
     session.run(assign_op)
   return self._saver.save(
       file_prefix=file_prefix,
       checkpoint_number=self.save_counter,
       session=session)
Esempio n. 44
0
 def save(self, file_prefix, session=None):
     """Save a checkpoint. Wraps `tfe.CheckpointableSaver.save`."""
     assign_op = self.save_counter.assign_add(1)
     if context.in_graph_mode():
         if session is None:
             session = ops.get_default_session()
         session.run(assign_op)
     return self._saver.save(file_prefix=file_prefix,
                             checkpoint_number=self.save_counter,
                             session=session)
  def save(self, session=None, checkpoint_number=None):
    """Creates a new checkpoint and manages it.

    Args:
      session: The session to evaluate variables in. Ignored when executing
        eagerly. If not provided when graph building, the default session is
        used.
      checkpoint_number: An optional integer, or an integer-dtype `Variable` or
        `Tensor`, used to number the checkpoint. If `None` (default),
        checkpoints are numbered using `checkpoint.save_counter`. Even if
        `checkpoint_number` is provided, `save_counter` is still incremented. A
        user-provided `checkpoint_number` is not incremented even if it is a
        `Variable`.

    Returns:
      The path to the new checkpoint. It is also recorded in the `checkpoints`
      and `latest_checkpoint` properies.
    """
    # Save counter logic duplicated from tf.train.Checkpoint, soon to diverge
    # slightly with a custom numbering option.
    if context.executing_eagerly():
      save_counter = self._checkpoint.save_counter
      save_counter.assign_add(1)
    else:
      if session is None:
        session = ops.get_default_session()

      def _initializing_creator(next_creator, **kwargs):
        """Initialize the save counter if it has been newly created."""
        v = next_creator(**kwargs)
        session.run(v.initializer)
        return v

      with variable_scope.variable_creator_scope(_initializing_creator):
        save_counter = self._checkpoint.save_counter
      if self._save_counter_assign is None:
        self._save_counter_assign = save_counter.assign_add(1, read_value=False)
      session.run(self._save_counter_assign)
    if checkpoint_number is None:
      checkpoint_number = save_counter
    if not isinstance(checkpoint_number, compat.integral_types):
      checkpoint_number = training_util.global_step(
          sess=session, global_step_tensor=checkpoint_number)
    prefix = "%s-%d" % (self._prefix, checkpoint_number)
    save_path = self._checkpoint.write(prefix)
    timestamp = time.time()
    # If this is an overwritten checkpoint we were previously tracking, delete
    # and reinsert it to make sure it goes to the end of the queue.
    if save_path in self._maybe_delete:
      del self._maybe_delete[save_path]
    self._maybe_delete[save_path] = timestamp
    self._latest_checkpoint = save_path
    self._sweep()
    self._record_state()
    return save_path
Esempio n. 46
0
  def evaluate(self, tensors):
    """Evaluates tensors and returns numpy values.

    Args:
      tensors: A Tensor or a nested list/tuple of Tensors.

    Returns:
      tensors numpy values.
    """
    sess = ops.get_default_session() or self.cached_session()
    return sess.run(tensors)
Esempio n. 47
0
def _compute_theoretical_jacobian(x, x_shape, x_data, dy, dy_shape, dx):
  """Computes the theoretical Jacobian for dy/dx.

  Computes the theoretical Jacobian using the ops generated by
  compute_gradient().

  Args:
    x: the tensor "x".
    x_shape: the dimensions of x as a tuple or an array of ints.
    x_data: a numpy parray as the input data for x
    dy: the tensor "dy".
    dy_shape: the dimensions of dy as a tuple or an array of ints.
    dx: Tensor or IndexedSlices representing dx

  Returns:
    A 2-d numpy array representing the Jacobian for dy/dx. It has "x_size" rows
    and "dy_size" columns where "x_size" is the number of elements in x and
    "dy_size" is the number of elements in dy.
  """
  # Complex vectors are treated as vectors of twice as many reals.
  if x.dtype.is_complex:
    x_shape = tuple(x_shape) + (2,)
  dy_factor = 2 if dy.dtype.is_complex else 1

  # To compute the jacobian, we treat x and y as one-dimensional vectors.
  x_size = _product(x_shape)
  x_val_size = _product(x_shape[1:])  # This is used for sparse gradients
  dy_size = _product(dy_shape) * dy_factor

  jacobian = np.zeros((x_size, dy_size),
                      dtype=x.dtype.real_dtype.as_numpy_dtype)
  # For each of the entry of dy, we set this to be 1 and
  # everything else to be 0 and compute the backprop -- this will give us one
  # one column of the Jacobian matrix.
  dy_data = np.zeros(dy_shape, dtype=dy.dtype.as_numpy_dtype)
  dy_data_flat = dy_data.ravel().view(dy.dtype.real_dtype.as_numpy_dtype)
  sess = ops.get_default_session()
  for col in range(dy_size):
    dy_data_flat[col] = 1
    if isinstance(dx, ops.IndexedSlices):
      backprop_indices, backprop_values = sess.run(
          [dx.indices, dx.values], feed_dict={x: x_data, dy: dy_data})
      for i, v in zip(backprop_indices, backprop_values):
        r_begin = i * x_val_size
        r_end = r_begin + x_val_size
        jacobian[r_begin:r_end, col] += v.flat
    else:
      assert isinstance(dx, ops.Tensor), "dx = " + str(dx)
      backprop = sess.run(dx, feed_dict={x: x_data, dy: dy_data})
      jacobian[:, col] = backprop.ravel().view(jacobian.dtype)
    dy_data_flat[col] = 0

  logging.vlog(1, "Theoretical Jacobian =\n%s", jacobian)
  return jacobian
Esempio n. 48
0
  def evaluate(self, tensors):
    """Evaluates tensors and returns numpy values.

    Args:
      tensors: A Tensor or a nested list/tuple of Tensors.

    Returns:
      tensors numpy values.
    """
    if context.in_eager_mode():
      return self._eval_helper(tensors)
    else:
      sess = ops.get_default_session()
      return sess.run(tensors)
Esempio n. 49
0
def _restore_existing_variables(network, save_path, map_func, user_map_func):
  """Use a standard Saver to restore existing variables from a checkpoint.

  Args:
    network: A Network object to restore.
    save_path: The checkpoint prefix or directory to read from.
    map_func: The function to use when mapping from variable names to checkpoint
      names.
    user_map_func: The original map_func passed by the user, for error checking.

  Returns:
    A dictionary mapping from checkpoint names to variable objects which have
    been restored (for bookkeeping to avoid deferred restorations on these
    variables).
  Raises:
    ValueError: If there is a name collision.
  """
  existing_variables_by_checkpoint_name = {}
  for variable in network.variables:
    checkpoint_name = map_func(variable._shared_name)
    if existing_variables_by_checkpoint_name.setdefault(
        checkpoint_name, variable) is not variable:
      if user_map_func is None:
        raise ValueError(
            _default_naming_conflict_error_message(
                mapped_name=checkpoint_name,
                first_variable=existing_variables_by_checkpoint_name[
                    checkpoint_name],
                second_variable=variable,
                network_name=network.name,
                network_scope_name=network.scope_name))
      else:
        raise ValueError(
            _restore_custom_map_func_error_message(
                mapped_name=checkpoint_name,
                first_variable=existing_variables_by_checkpoint_name[
                    checkpoint_name],
                second_variable=variable,
                network_name=network.name,
                network_scope_name=network.scope_name))
  if existing_variables_by_checkpoint_name:
    if context.executing_eagerly():
      sess = None
    else:
      sess = ops.get_default_session()
    saver_lib.Saver(var_list=existing_variables_by_checkpoint_name).restore(
        sess=sess, save_path=save_path)
  return existing_variables_by_checkpoint_name
Esempio n. 50
0
def _to_numpy(a):
  """Converts Tensors and EagerTensors to numpy arrays.

  Args:
    a: any value.

  Returns:
    If a is EagerTensor or Tensor, returns the evaluation of a by calling
    numpy() or run(). Otherwise returns a unchanged.
  """
  if isinstance(a, ops.EagerTensor):
    return a.numpy()
  if isinstance(a, ops.Tensor):
    sess = ops.get_default_session()
    return sess.run(a)
  return a
  def _operator_and_mat_and_feed_dict(self, shape, dtype, use_placeholder):
    sess = ops.get_default_session()
    shape = list(shape)

    # Test only the case of 2 matrices.
    # The Square test uses either 1 or 2, so we have tested the case of 1 matrix
    # sufficiently.
    num_operators = 2

    # Create 2 matrices/operators, A1, A2, which becomes A = A1 A2.
    # Use inner dimension of 2.
    k = 2
    batch_shape = shape[:-2]
    shape_1 = batch_shape + [shape[-2], k]
    shape_2 = batch_shape + [k, shape[-1]]

    matrices = [
        linear_operator_test_util.random_normal(
            shape_1, dtype=dtype), linear_operator_test_util.random_normal(
                shape_2, dtype=dtype)
    ]

    if use_placeholder:
      matrices_ph = [
          array_ops.placeholder(dtype=dtype) for _ in range(num_operators)
      ]
      # Evaluate here because (i) you cannot feed a tensor, and (ii)
      # values are random and we want the same value used for both mat and
      # feed_dict.
      matrices = sess.run(matrices)
      operator = linalg.LinearOperatorComposition(
          [linalg.LinearOperatorFullMatrix(m_ph) for m_ph in matrices_ph])
      feed_dict = {m_ph: m for (m_ph, m) in zip(matrices_ph, matrices)}
    else:
      operator = linalg.LinearOperatorComposition(
          [linalg.LinearOperatorFullMatrix(m) for m in matrices])
      feed_dict = None

    # Convert back to Tensor.  Needed if use_placeholder, since then we have
    # already evaluated each matrix to a numpy array.
    matmul_order_list = list(reversed(matrices))
    mat = ops.convert_to_tensor(matmul_order_list[0])
    for other_mat in matmul_order_list[1:]:
      mat = math_ops.matmul(other_mat, mat)

    return operator, mat, feed_dict
Esempio n. 52
0
  def evaluate(self, tensors):
    """Evaluates tensors and returns numpy values.

    Args:
      tensors: A Tensor or a list of Tensors.

    Returns:
      tensors numpy values.
    """
    if context.in_eager_mode():
      if isinstance(tensors, list):
        assert all(isinstance(t, ops.EagerTensor) for t in tensors)
        return [t.numpy() for t in tensors]
      assert isinstance(tensors, ops.EagerTensor), "Must be list or EagerTensor"
      return tensors.numpy()
    else:
      sess = ops.get_default_session()
      return sess.run(tensors)
Esempio n. 53
0
  def initialize_or_restore(self, session=None):
    """Runs initialization ops for variables.

    Only objects which would be saved by `Saver.save` will be initialized. See
    `gather_initializers` for details.

    This method does nothing when executing eagerly (initializers get run
    eagerly).

    Args:
      session: The session to run initialization ops in. If `None`, uses the
        default session.
    """
    if context.executing_eagerly():
      return  # run eagerly
    if session is None:
      session = ops.get_default_session()
    session.run(gather_initializers(self._root_checkpointable))
Esempio n. 54
0
  def test_no_default_session(self):
    with ops.Graph().as_default():
      self.assertFalse(ops.get_default_session())
      data = np.random.random((1000, 32)).astype(np.float32)
      labels = np.random.random((1000, 10)).astype(np.float32)

      model = keras.models.Sequential([
          keras.layers.Dense(10, activation='softmax'),
          keras.layers.Dense(10, activation='softmax')])

      model.compile(optimizer=training_module.RMSPropOptimizer(0.001),
                    loss='categorical_crossentropy',
                    metrics=['accuracy'])

      model.fit(data, labels)
      fname = os.path.join(self.get_temp_dir(), 'weights', 'ckpt')
      model.save_weights(fname)
      model.load_weights(fname)