Python tensorflow.initialize_variables() Examples

The following are 30 code examples of tensorflow.initialize_variables(). 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 , or try the search function .
Example #1
Source File: dqn_utils.py    From rl_algorithms with MIT License 6 votes vote down vote up
def initialize_interdependent_variables(session, vars_list, feed_dict):
    """Initialize a list of variables one at a time, which is useful if
    initialization of some variables depends on initialization of the others.
    """
    vars_left = vars_list
    while len(vars_left) > 0:
        new_vars_left = []
        for v in vars_left:
            try:
                # If using an older version of TensorFlow, uncomment the line
                # below and comment out the line after it.
		#session.run(tf.initialize_variables([v]), feed_dict)
                session.run(tf.variables_initializer([v]), feed_dict)
            except tf.errors.FailedPreconditionError:
                new_vars_left.append(v)
        if len(new_vars_left) >= len(vars_left):
            # This can happend if the variables all depend on each other, or more likely if there's
            # another variable outside of the list, that still needs to be initialized. This could be
            # detected here, but life's finite.
            raise Exception("Cycle in variable dependencies, or extenrnal precondition unsatisfied.")
        else:
            vars_left = new_vars_left 
Example #2
Source File: metric_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def _test_streaming_sparse_average_precision_at_k(
      self, predictions, labels, k, expected, weights=None):
    with tf.Graph().as_default() as g, self.test_session(g):
      if weights is not None:
        weights = tf.constant(weights, tf.float32)
      predictions = tf.constant(predictions, tf.float32)
      metric, update = metrics.streaming_sparse_average_precision_at_k(
          predictions, labels, k, weights=weights)

      # Fails without initialized vars.
      self.assertRaises(tf.OpError, metric.eval)
      self.assertRaises(tf.OpError, update.eval)
      local_variables = tf.local_variables()
      tf.initialize_variables(local_variables).run()

      # Run per-step op and assert expected values.
      if math.isnan(expected):
        _assert_nan(self, update.eval())
        _assert_nan(self, metric.eval())
      else:
        self.assertAlmostEqual(expected, update.eval())
        self.assertAlmostEqual(expected, metric.eval()) 
Example #3
Source File: metric_ops_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def _test_streaming_sparse_precision_at_top_k(self,
                                                top_k_predictions,
                                                labels,
                                                expected,
                                                class_id=None,
                                                weights=None):
    with tf.Graph().as_default() as g, self.test_session(g):
      if weights is not None:
        weights = tf.constant(weights, tf.float32)
      metric, update = metrics.streaming_sparse_precision_at_top_k(
          top_k_predictions=tf.constant(top_k_predictions, tf.int32),
          labels=labels, class_id=class_id, weights=weights)

      # Fails without initialized vars.
      self.assertRaises(tf.OpError, metric.eval)
      self.assertRaises(tf.OpError, update.eval)
      tf.initialize_variables(tf.local_variables()).run()

      # Run per-step op and assert expected values.
      if math.isnan(expected):
        self.assertTrue(math.isnan(update.eval()))
        self.assertTrue(math.isnan(metric.eval()))
      else:
        self.assertEqual(expected, update.eval())
        self.assertEqual(expected, metric.eval()) 
Example #4
Source File: dqn_utils.py    From cs294-112_hws with MIT License 6 votes vote down vote up
def initialize_interdependent_variables(session, vars_list, feed_dict):
    """Initialize a list of variables one at a time, which is useful if
    initialization of some variables depends on initialization of the others.
    """
    vars_left = vars_list
    while len(vars_left) > 0:
        new_vars_left = []
        for v in vars_left:
            try:
                # If using an older version of TensorFlow, uncomment the line
                # below and comment out the line after it.
		#session.run(tf.initialize_variables([v]), feed_dict)
                session.run(tf.variables_initializer([v]), feed_dict)
            except tf.errors.FailedPreconditionError:
                new_vars_left.append(v)
        if len(new_vars_left) >= len(vars_left):
            # This can happend if the variables all depend on each other, or more likely if there's
            # another variable outside of the list, that still needs to be initialized. This could be
            # detected here, but life's finite.
            raise Exception("Cycle in variable dependencies, or extenrnal precondition unsatisfied.")
        else:
            vars_left = new_vars_left 
Example #5
Source File: variable_scope_test.py    From deep_image_model with Apache License 2.0 6 votes vote down vote up
def testInitializeFromValue(self):
    with self.test_session() as sess:
      init = tf.constant(0.1)
      w = tf.get_variable("v", initializer=init)
      sess.run(tf.initialize_variables([w]))
      self.assertAllClose(w.eval(), 0.1)

      with self.assertRaisesRegexp(ValueError, "shape"):
        # We disallow explicit shape specification when initializer is constant.
        tf.get_variable("u", [1], initializer=init)

      with tf.variable_scope("foo", initializer=init):
        # Constant initializer can be passed through scopes if needed.
        v = tf.get_variable("v")
        sess.run(tf.initialize_variables([v]))
        self.assertAllClose(v.eval(), 0.1)

      # Check that non-float32 initializer creates a non-float32 variable.
      init = tf.constant(1, dtype=tf.int32)
      t = tf.get_variable("t", initializer=init)
      self.assertEqual(t.dtype.base_dtype, tf.int32)

      # Raise error if `initializer` dtype and `dtype` are not identical.
      with self.assertRaisesRegexp(ValueError, "don't match"):
        tf.get_variable("s", initializer=init, dtype=tf.float64) 
Example #6
Source File: utils.py    From variational-continual-learning with Apache License 2.0 6 votes vote down vote up
def load_params(sess, filename, checkpoint, init_all = True):
    params = tf.trainable_variables()
    filename = filename + '_' + str(checkpoint)
    f = open(filename + '.pkl', 'r')
    param_dict = cPickle.load(f)
    print 'param loaded', len(param_dict)
    f.close()
    ops = []
    for v in params:
        if v.name in param_dict.keys():
            ops.append(tf.assign(v, param_dict[v.name]))
    sess.run(ops)
    # init uninitialised params
    if init_all:
        all_var = tf.all_variables()
        var = [v for v in all_var if v not in params]
        sess.run(tf.initialize_variables(var))
    print 'loaded parameters from ' + filename + '.pkl' 
Example #7
Source File: nmt_generator.py    From NMT_GAN with Apache License 2.0 6 votes vote down vote up
def init_and_reload(self):

         ##########
         # this function is only used for the gan training with reload
         ##########

         params = [param for param in tf.trainable_variables() if 'generate' in param.name]
         #params = [param for param in tf.all_variables()]
         if not self.sess.run(tf.is_variable_initialized(params[0])):
            #init_op = tf.initialize_variables(params)
            init_op = tf.global_variables_initializer()  ## this is important here to initialize_all_variables()
            self.sess.run(init_op)

         saver = tf.train.Saver(params)
         self.saver=saver
         
         if self.gen_reload:                                     ##here must be true
           print('reloading params from %s '% self.saveto)
           self.saver.restore(self.sess, './'+self.saveto)
           print('reloading params done')
         else:
           print('error, reload must be true!!') 
Example #8
Source File: dqn_utils.py    From deep-reinforcement-learning with MIT License 6 votes vote down vote up
def initialize_interdependent_variables(session, vars_list, feed_dict):
    """Initialize a list of variables one at a time, which is useful if
    initialization of some variables depends on initialization of the others.
    """
    vars_left = vars_list
    while len(vars_left) > 0:
        new_vars_left = []
        for v in vars_left:
            try:
                # If using an older version of TensorFlow, uncomment the line
                # below and comment out the line after it.
		#session.run(tf.initialize_variables([v]), feed_dict)
                session.run(tf.variables_initializer([v]), feed_dict)
            except tf.errors.FailedPreconditionError:
                new_vars_left.append(v)
        if len(new_vars_left) >= len(vars_left):
            # This can happend if the variables all depend on each other, or more likely if there's
            # another variable outside of the list, that still needs to be initialized. This could be
            # detected here, but life's finite.
            raise Exception("Cycle in variable dependencies, or extenrnal precondition unsatisfied.")
        else:
            vars_left = new_vars_left 
Example #9
Source File: coldStart.py    From neural-el with Apache License 2.0 6 votes vote down vote up
def typeAndWikiDescBasedColdEmbExp(self, ckptName="FigerModel-20001"):
        ''' Train cold embeddings using wiki desc loss
        '''
        saver = tf.train.Saver(var_list=tf.all_variables())

        print("Loading Model ... ")
        if ckptName == None:
            print("Given CKPT Name")
            sys.exit()
        else:
            load_status = self.fm.loadSpecificCKPT(
              saver=saver, checkpoint_dir=self.fm.checkpoint_dir,
              ckptName=ckptName, attrs=self.fm._attrs)
        if not load_status:
            print("No model to load. Exiting")
            sys.exit(0)

        self._makeDescLossGraph()
        self.fm.sess.run(tf.initialize_variables(self.allcoldvars))
        self._trainColdEmbFromTypesAndDesc(epochsToTrain=5)

        self.runEval()

    # EVALUATION FOR COLD START WHEN INITIALIZING COLD EMB FROM WIKI DESC ENCODING 
Example #10
Source File: coldStart.py    From neural-el with Apache License 2.0 6 votes vote down vote up
def typeBasedColdEmbExp(self, ckptName="FigerModel-20001"):
        ''' Train cold embeddings using wiki desc loss
        '''
        saver = tf.train.Saver(var_list=tf.all_variables())

        print("Loading Model ... ")
        if ckptName == None:
            print("Given CKPT Name")
            sys.exit()
        else:
            load_status = self.fm.loadSpecificCKPT(
              saver=saver, checkpoint_dir=self.fm.checkpoint_dir,
              ckptName=ckptName, attrs=self.fm._attrs)
        if not load_status:
            print("No model to load. Exiting")
            sys.exit(0)

        self._makeDescLossGraph()
        self.fm.sess.run(tf.initialize_variables(self.allcoldvars))
        self._trainColdEmbFromTypes(epochsToTrain=5)

        self.runEval()

    ############################################################################## 
Example #11
Source File: test_optimizer.py    From tensorprob with MIT License 6 votes vote down vote up
def test_migrad():
    sess = tf.Session()
    x = tf.Variable(np.float64(2), name='x')
    sess.run(tf.initialize_variables([x]))
    optimizer = MigradOptimizer(session=sess)
    # With gradient
    results = optimizer.minimize([x], x**2, [2 * x])
    assert results.success
    # Without gradient
    results = optimizer.minimize([x], x**2)
    assert results.success
    @raises(ValueError)
    def test_illegal_parameter_as_variable1():
        optimizer.minimize([42], x**2)
    test_illegal_parameter_as_variable1()
    @raises(ValueError)
    def test_illegal_parameter_as_variable2():
        optimizer.minimize(42, x**2)
    test_illegal_parameter_as_variable2() 
Example #12
Source File: test_optimizer.py    From tensorprob with MIT License 6 votes vote down vote up
def test_scipy_lbfgsb():
    sess = tf.Session()
    x = tf.Variable(np.float64(2), name='x')
    sess.run(tf.initialize_variables([x]))
    optimizer = ScipyLBFGSBOptimizer(verbose=True, session=sess)
    # With gradient
    results = optimizer.minimize([x], x**2, [2 * x])
    assert results.success
    # Without gradient
    results = optimizer.minimize([x], x**2)
    assert results.success
    # Test callback
    def callback(xs):
        pass
    optimizer = ScipyLBFGSBOptimizer(verbose=True, session=sess, callback=callback)
    assert optimizer.minimize([x], x**2).success
    @raises(ValueError)
    def test_illegal_parameter_as_variable1():
        optimizer.minimize([42], x**2)
    test_illegal_parameter_as_variable1()
    @raises(ValueError)
    def test_illegal_parameter_as_variable2():
        optimizer.minimize(42, x**2)
    test_illegal_parameter_as_variable2() 
Example #13
Source File: prune.py    From ternarynet with Apache License 2.0 5 votes vote down vote up
def _setup_graph(self):
        self._init_mask_op = tf.initialize_variables(tf.get_collection('masks'))
        self._init_thre_op = tf.initialize_variables(tf.get_collection('thresholds')) 
Example #14
Source File: common.py    From ternarynet with Apache License 2.0 5 votes vote down vote up
def _setup_graph(self):
        self.optimizer = self.trainer.config.optimizer
        variables = tf.get_collection('trainable_variables')
        slot_names = self.optimizer.get_slot_names()
        slot_vars = [self.optimizer.get_slot(var, s)
                for s in slot_names for var in variables]

        self.init_slot_ops = tf.initialize_variables(slot_vars) 
Example #15
Source File: elm.py    From LIVE_SER with Apache License 2.0 5 votes vote down vote up
def init(self):
    self._sess.run(tf.initialize_variables(self._var_list))
    self._init = True 
Example #16
Source File: block_compiler_test.py    From fold with Apache License 2.0 5 votes vote down vote up
def test_all_initialized(self):
    with self.test_session() as sess:
      x = tf.Variable(tf.zeros([]))
      sess.run(tf.initialize_variables([x]))
      self.assertEqual([], tdc._init_uninitialized(sess)) 
Example #17
Source File: pg_actor_critic.py    From Codes-for-RL-PER with MIT License 5 votes vote down vote up
def resetModel(self):
    self.cleanUp()
    self.train_iteration = 0
    self.exploration     = self.init_exp
    var_lists = tf.get_collection(tf.GraphKeys.VARIABLES)
    self.session.run(tf.initialize_variables(var_lists)) 
Example #18
Source File: crbm_backup.py    From Convolutional_Deep_Belief_Network with MIT License 5 votes vote down vote up
def init_parameter(self,from_scratch = True):
    """INTENT : Return the tensorflow operation for initializing the parameter of this RBM
    ------------------------------------------------------------------------------------------------------------------------------------------
    PARAMETERS :
    from_scratch               :        specifiy if this RBM is pretrained (True) or restored in order to adjust which variable to initialize"""
    
    if from_scratch:
      return tf.initialize_all_variables()
    elif self.gaussian_unit:
      return tf.initialize_variables([self.vitesse_kernels, self.vitesse_biases_V, self.vitesse_biases_H, self.sigma])
    else:
      return tf.initialize_variables([self.vitesse_kernels, self.vitesse_biases_V, self.vitesse_biases_H]) 
Example #19
Source File: cla_models_multihead.py    From variational-continual-learning with Apache License 2.0 5 votes vote down vote up
def reset_optimiser(self):
        optimiser_scope = tf.get_collection(
            tf.GraphKeys.GLOBAL_VARIABLES,
            "scope/prefix/for/optimizer")
        self.sess.run(tf.initialize_variables(optimiser_scope))

# Either the lower network, or the upper network (top-most layer of model) 
Example #20
Source File: utils.py    From variational-continual-learning with Apache License 2.0 5 votes vote down vote up
def init_variables(sess, old_var_list = set([])):
    all_var_list = set(tf.all_variables())
    init = tf.initialize_variables(var_list = all_var_list - old_var_list)
    sess.run(init)
    return all_var_list 
Example #21
Source File: tensor_util_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_local_variable(self):
    with self.test_session() as sess:
      self.assertEquals([], tf.local_variables())
      value0 = 42
      tf.contrib.framework.local_variable(value0)
      value1 = 43
      tf.contrib.framework.local_variable(value1)
      variables = tf.local_variables()
      self.assertEquals(2, len(variables))
      self.assertRaises(tf.OpError, sess.run, variables)
      tf.initialize_variables(variables).run()
      self.assertAllEqual(set([value0, value1]), set(sess.run(variables))) 
Example #22
Source File: metric_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def test_sparse_tensor_value(self):
    predictions = [[0.1, 0.3, 0.2, 0.4], [0.1, 0.2, 0.3, 0.4]]
    labels = [[0, 0, 1, 0], [0, 0, 0, 1]]
    expected_recall = 0.5
    with self.test_session():
      _, recall = metrics.streaming_sparse_recall_at_k(
          predictions=tf.constant(predictions, tf.float32),
          labels=_binary_2d_label_to_sparse_value(labels), k=1)

      tf.initialize_variables(tf.local_variables()).run()

      self.assertEqual(expected_recall, recall.eval()) 
Example #23
Source File: metric_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _test_streaming_sparse_recall_at_k(self,
                                         predictions,
                                         labels,
                                         k,
                                         expected,
                                         class_id=None,
                                         weights=None):
    with tf.Graph().as_default() as g, self.test_session(g):
      if weights is not None:
        weights = tf.constant(weights, tf.float32)
      metric, update = metrics.streaming_sparse_recall_at_k(
          predictions=tf.constant(predictions, tf.float32),
          labels=labels, k=k, class_id=class_id, weights=weights)

      # Fails without initialized vars.
      self.assertRaises(tf.OpError, metric.eval)
      self.assertRaises(tf.OpError, update.eval)
      tf.initialize_variables(tf.local_variables()).run()

      # Run per-step op and assert expected values.
      if math.isnan(expected):
        _assert_nan(self, update.eval())
        _assert_nan(self, metric.eval())
      else:
        self.assertEqual(expected, update.eval())
        self.assertEqual(expected, metric.eval()) 
Example #24
Source File: metric_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def _test_streaming_sparse_precision_at_k(self,
                                            predictions,
                                            labels,
                                            k,
                                            expected,
                                            class_id=None,
                                            weights=None):
    with tf.Graph().as_default() as g, self.test_session(g):
      if weights is not None:
        weights = tf.constant(weights, tf.float32)
      metric, update = metrics.streaming_sparse_precision_at_k(
          predictions=tf.constant(predictions, tf.float32), labels=labels,
          k=k, class_id=class_id, weights=weights)

      # Fails without initialized vars.
      self.assertRaises(tf.OpError, metric.eval)
      self.assertRaises(tf.OpError, update.eval)
      tf.initialize_variables(tf.local_variables()).run()

      # Run per-step op and assert expected values.
      if math.isnan(expected):
        _assert_nan(self, update.eval())
        _assert_nan(self, metric.eval())
      else:
        self.assertEqual(expected, update.eval())
        self.assertEqual(expected, metric.eval()) 
Example #25
Source File: model.py    From ELM-tensorflow with MIT License 5 votes vote down vote up
def init(self):
    self._sess.run(tf.initialize_variables(self._var_list))
    self._init = True 
Example #26
Source File: nn_model.py    From FATE with Apache License 2.0 5 votes vote down vote up
def _initialize_variables(self):
        uninitialized_var_names = [bytes.decode(var) for var in self._sess.run(tf.report_uninitialized_variables())]
        uninitialized_vars = [var for var in tf.global_variables() if var.name.split(':')[0] in uninitialized_var_names]
        self._sess.run(tf.initialize_variables(uninitialized_vars)) 
Example #27
Source File: DeeProtein.py    From AiGEM_TeamHeidelberg2017 with MIT License 5 votes vote down vote up
def guarantee_initialized_variables(self, session, list_of_variables=None):
        if list_of_variables is None:
            list_of_variables = tf.all_variables()
        uninitialized_variables = list(tf.get_variable(name) for name in
                                       session.run(tf.report_uninitialized_variables(list_of_variables)))
        session.run(tf.initialize_variables(uninitialized_variables))
        return uninitialized_variables 
Example #28
Source File: array_ops_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, index, value):
    for use_gpu in [False, True]:
      with self.test.test_session(use_gpu=use_gpu) as sess:
        var = tf.Variable(self.x)
        sess.run(tf.initialize_variables([var]))
        val = sess.run(var[index].assign(
            tf.constant(
                value, dtype=self.tensor_type)))
        valnp = np.copy(self.x_np)
        valnp[index] = np.array(value)
        self.test.assertAllEqual(val, valnp) 
Example #29
Source File: variable_scope_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testVarScopeInitializer(self):
    with self.test_session() as sess:
      init = tf.constant_initializer(0.3)
      with tf.variable_scope("tower") as tower:
        with tf.variable_scope("foo", initializer=init):
          v = tf.get_variable("v", [])
          sess.run(tf.initialize_variables([v]))
          self.assertAllClose(v.eval(), 0.3)
        with tf.variable_scope(tower, initializer=init):
          w = tf.get_variable("w", [])
          sess.run(tf.initialize_variables([w]))
          self.assertAllClose(w.eval(), 0.3) 
Example #30
Source File: variable_scope_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
def testInitFromNonTensorValue(self):
    with self.test_session() as sess:
      v = tf.get_variable("v", initializer=4, dtype=tf.int32)
      sess.run(tf.initialize_variables([v]))
      self.assertAllClose(v.eval(), 4)

      w = tf.get_variable("w",
                          initializer=numpy.array([1, 2, 3]),
                          dtype=tf.int64)
      sess.run(tf.initialize_variables([w]))
      self.assertAllClose(w.eval(), [1, 2, 3])

      with self.assertRaises(TypeError):
        tf.get_variable("x", initializer={})