Python tensorflow.python.ops.variables.variables_initializer() Examples

The following are 10 code examples of tensorflow.python.ops.variables.variables_initializer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.ops.variables , or try the search function .
Example #1
Source File: variables_test.py    From tf-slim with Apache License 2.0 6 votes vote down vote up
def create_checkpoint_from_values(self,
                                    var_names_to_values,
                                    checkpoint_dir,
                                    global_step=None):
    """Creates a checkpoint from a mapping of name to values in model_dir.

    Args:
      var_names_to_values: a map from variable names to values.
      checkpoint_dir: the directory where the checkpoint will be saved.
      global_step: the global step used to save the checkpoint.

    Returns:
      the model_path to the checkpoint.
    """
    var_list = []
    with session.Session('', graph=ops.Graph()) as sess:
      # Create a set of variables to save in the checkpoint.
      for var_name in var_names_to_values:
        var_value = var_names_to_values[var_name]
        var_list.append(variables_lib.VariableV1(var_value, name=var_name))
      saver = saver_lib.Saver(var_list)
      init_op = variables_lib.variables_initializer(var_list)
      sess.run(init_op)
      # Save the initialized values in the file at 'checkpoint_dir'
      return saver.save(sess, checkpoint_dir, global_step=global_step) 
Example #2
Source File: variables_test.py    From tf-slim with Apache License 2.0 6 votes vote down vote up
def create_checkpoint_from_values(self,
                                    var_names_to_values,
                                    checkpoint_dir,
                                    global_step=None):
    """Creates a checkpoint from a mapping of name to values in model_dir.

    Args:
      var_names_to_values: a map from variable names to values.
      checkpoint_dir: the directory where the checkpoint will be saved.
      global_step: the global step used to save the checkpoint.

    Returns:
      the model_path to the checkpoint.
    """
    var_list = []
    with session.Session('', graph=ops.Graph()) as sess:
      # Create a set of variables to save in the checkpoint.
      for var_name in var_names_to_values:
        var_value = var_names_to_values[var_name]
        var_list.append(variables_lib.VariableV1(var_value, name=var_name))
      saver = saver_lib.Saver(var_list)
      init_op = variables_lib.variables_initializer(var_list)
      sess.run(init_op)
      # Save the initialized values in the file at 'checkpoint_dir'
      return saver.save(sess, checkpoint_dir, global_step=global_step) 
Example #3
Source File: backend.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 6 votes vote down vote up
def _initialize_variables(session):
  """Utility to initialize uninitialized variables on the fly."""
  variables = variables_module.global_variables()
  candidate_vars = []
  for v in variables:
    if not getattr(v, '_keras_initialized', False):
      candidate_vars.append(v)
  # This step is expensive, so we only run it on variables not already
  # marked as initialized.
  is_initialized = session.run(
      [variables_module.is_variable_initialized(v) for v in candidate_vars])
  uninitialized_vars = []
  for flag, v in zip(is_initialized, candidate_vars):
    if not flag:
      uninitialized_vars.append(v)
    v._keras_initialized = True
  if uninitialized_vars:
    session.run(variables_module.variables_initializer(uninitialized_vars)) 
Example #4
Source File: factorization_ops.py    From lambda-packs with MIT License 5 votes vote down vote up
def initialize_op(self):
    """Returns an op for initializing tensorflow variables."""
    all_vars = self._row_factors + self._col_factors
    all_vars.extend([self._row_gramian, self._col_gramian])
    if self._row_weights is not None:
      assert self._col_weights is not None
      all_vars.extend(self._row_weights + self._col_weights)
    return variables.variables_initializer(all_vars) 
Example #5
Source File: backend.py    From lambda-packs with MIT License 5 votes vote down vote up
def _initialize_variables():
  """Utility to initialize uninitialized variables on the fly.
  """
  variables = variables_module.global_variables()
  uninitialized_variables = []
  for v in variables:
    if not hasattr(v, '_keras_initialized') or not v._keras_initialized:
      uninitialized_variables.append(v)
      v._keras_initialized = True
  if uninitialized_variables:
    sess = get_session()
    sess.run(variables_module.variables_initializer(uninitialized_variables)) 
Example #6
Source File: factorization_ops.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def initialize_op(self):
    """Returns an op for initializing tensorflow variables."""
    all_vars = self._row_factors + self._col_factors
    all_vars.extend([self._row_gramian, self._col_gramian])
    if self._row_weights is not None:
      assert self._col_weights is not None
      all_vars.extend(self._row_weights + self._col_weights)
    return variables.variables_initializer(all_vars) 
Example #7
Source File: variables_test.py    From tf-slim with Apache License 2.0 5 votes vote down vote up
def test_local_variable(self):
    with self.cached_session() as sess:
      self.assertEqual([], variables_lib.local_variables())
      value0 = 42
      variables_lib2.local_variable(value0)
      value1 = 43
      variables_lib2.local_variable(value1)
      variables = variables_lib.local_variables()
      self.assertEqual(2, len(variables))
      self.assertRaises(errors_impl.OpError, sess.run, variables)
      variables_lib.variables_initializer(variables).run()
      self.assertAllEqual(set([value0, value1]), set(sess.run(variables))) 
Example #8
Source File: variables_test.py    From tf-slim with Apache License 2.0 5 votes vote down vote up
def test_global_variable(self):
    with self.cached_session() as sess:
      self.assertEqual([], variables_lib.global_variables())
      value0 = 42
      variables_lib2.global_variable(value0)
      value1 = 43
      variables_lib2.global_variable(value1)
      variables = variables_lib.global_variables()
      self.assertEqual(2, len(variables))
      with self.assertRaises(errors_impl.FailedPreconditionError):
        sess.run(variables)
      variables_lib.variables_initializer(variables).run()
      self.assertAllEqual(set([value0, value1]), set(sess.run(variables))) 
Example #9
Source File: optimistic_restore_saver.py    From active-qa with Apache License 2.0 5 votes vote down vote up
def __init__(self,
               var_list=None,
               init_uninitialized_variables=False,
               **kwargs):
    kwargs['restore_sequentially'] = False
    kwargs['builder'] = BaseSaverBuilder()
    super(OptimisticRestoreSaver, self).__init__(var_list=var_list, **kwargs)
    self.init_uninitialized_variables = init_uninitialized_variables
    if self.init_uninitialized_variables:
      self.uninit_vars_op = variables.report_uninitialized_variables(
          var_list=self._var_list)
      self.init_ops = dict((v.name, variables.variables_initializer([v]))
                           for v in self._var_list) 
Example #10
Source File: factorization_ops.py    From keras-lambda with MIT License 5 votes vote down vote up
def initialize_op(self):
    """Returns an op for initializing tensorflow variables."""
    all_vars = self._row_factors + self._col_factors
    all_vars.extend([self._row_gramian, self._col_gramian])
    if self._row_weights is not None:
      assert self._col_weights is not None
      all_vars.extend(self._row_weights + self._col_weights)
    return variables.variables_initializer(all_vars)