当前位置: 首页>>代码示例>>Python>>正文


Python tensorflow.assign_add方法代码示例

本文整理汇总了Python中tensorflow.assign_add方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.assign_add方法的具体用法?Python tensorflow.assign_add怎么用?Python tensorflow.assign_add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow的用法示例。


在下文中一共展示了tensorflow.assign_add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _create_autosummary_var

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def _create_autosummary_var(name, value_expr):
    assert not _autosummary_finalized
    v = tf.cast(value_expr, tf.float32)
    if v.shape.ndims is 0:
        v = [v, np.float32(1.0)]
    elif v.shape.ndims is 1:
        v = [tf.reduce_sum(v), tf.cast(tf.shape(v)[0], tf.float32)]
    else:
        v = [tf.reduce_sum(v), tf.reduce_prod(tf.cast(tf.shape(v), tf.float32))]
    v = tf.cond(tf.is_finite(v[0]), lambda: tf.stack(v), lambda: tf.zeros(2))
    with tf.control_dependencies(None):
        var = tf.Variable(tf.zeros(2)) # [numerator, denominator]
    update_op = tf.cond(tf.is_variable_initialized(var), lambda: tf.assign_add(var, v), lambda: tf.assign(var, v))
    if name in _autosummary_vars:
        _autosummary_vars[name].append(var)
    else:
        _autosummary_vars[name] = [var]
    return update_op

#----------------------------------------------------------------------------
# Call filewriter.add_summary() with all summaries in the default graph,
# automatically finalizing and merging them on the first call. 
开发者ID:zalandoresearch,项目名称:disentangling_conditional_gans,代码行数:24,代码来源:tfutil.py

示例2: testBasicDomainSeparationStartPoint

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def testBasicDomainSeparationStartPoint(self):
    with self.test_session() as sess:
      # Test for when global_step < domain_separation_startpoint
      step = tf.contrib.slim.get_or_create_global_step()
      sess.run(tf.global_variables_initializer())  # global_step = 0
      params = {'domain_separation_startpoint': 2}
      weight = dsn.dsn_loss_coefficient(params)
      weight_np = sess.run(weight)
      self.assertAlmostEqual(weight_np, 1e-10)

      step_op = tf.assign_add(step, 1)
      step_np = sess.run(step_op)  # global_step = 1
      weight = dsn.dsn_loss_coefficient(params)
      weight_np = sess.run(weight)
      self.assertAlmostEqual(weight_np, 1e-10)

      # Test for when global_step >= domain_separation_startpoint
      step_np = sess.run(step_op)  # global_step = 2
      tf.logging.info(step_np)
      weight = dsn.dsn_loss_coefficient(params)
      weight_np = sess.run(weight)
      self.assertAlmostEqual(weight_np, 1.0) 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:24,代码来源:dsn_test.py

示例3: get_action

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def get_action(self):
        deterministic_action = self.mean

        if self.ornstein_uhlenbeck_state:
            random_norm = tf.random_normal(
                shape=self.mean.get_shape().as_list()[1:],
                mean=0.0,
                stddev=1.0)
            ou_noise = tf.assign_add(
                self.ornstein_uhlenbeck_state,
                self._theta * (-self.ornstein_uhlenbeck_state) +
                random_norm * self._sigma)
            sample_action = self.mean + ou_noise * self._noise_scale

            output_action = tf.cond(self.deterministic_ph,
                                    lambda: deterministic_action,
                                    lambda: sample_action)
        else:
            output_action = deterministic_action

        return output_action 
开发者ID:alibaba,项目名称:EasyRL,代码行数:23,代码来源:action_utils.py

示例4: _graph_fn_get_should_sync

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def _graph_fn_get_should_sync(self):
        if get_backend() == "tf":
            inc_op = tf.assign_add(self.steps_since_last_sync, 1)
            should_sync = inc_op >= self.q_sync_spec.sync_interval

            def reset_op():
                op = tf.assign(self.steps_since_last_sync, 0)
                with tf.control_dependencies([op]):
                    return tf.no_op()

            sync_op = tf.cond(
                pred=inc_op >= self.q_sync_spec.sync_interval,
                true_fn=reset_op,
                false_fn=tf.no_op
            )
            with tf.control_dependencies([sync_op]):
                return tf.identity(should_sync)
        else:
            raise NotImplementedError("TODO") 
开发者ID:rlgraph,项目名称:rlgraph,代码行数:21,代码来源:sac_agent.py

示例5: _load_sampler

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def _load_sampler(self):
    if not self.structure_path:
      print("Loading the default file")
      with open('../../../../bayesian_optimization/sample_structures6.txt', 'r') as fp:
        lines = fp.readlines()
    else:
      with open(self.structure_path, 'r') as fp:
        lines = fp.readlines()
    lines = [eval(line.strip()) for line in lines]
    for line in lines:
      row = []
      for ele in line:
        row += ele
      self.structures.append(row)
    # self.sample_arcs = self.structures[0]
    self.idx_arc = tf.get_variable("idx_arc", initializer=tf.constant(0), dtype=tf.int32)
    self.reset_op = self.idx_arc.assign(0)
    self.inc_op = self.idx_arc.assign(self.idx_arc + 1)
    # self.idx_arc  = tf.assign_add(self.idx_arc, tf.constant(1, dtype=tf.int32))
    self.sample_arc2 = tf.reshape(tf.gather(self.structures, self.idx_arc), [-1])
    # self.sample_arc3 = tf.get_variable("sample_arc3", shape=[len(self.structures[0])])
    self.sample_arc3 = tf.placeholder(tf.float32, shape=[len(self.structures[0])]) 
开发者ID:muhanzhang,项目名称:D-VAE,代码行数:24,代码来源:general_controller.py

示例6: apply_stats_eigen

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def apply_stats_eigen(self, eigen_list):
        """
        apply the update using the eigen values of the stats

        :param eigen_list: ([TensorFlow Tensor]) The list of eigen values of the stats
        :return: ([TensorFlow Tensor]) update operations
        """
        update_ops = []
        if self.verbose > 1:
            print(('updating %d eigenvalue/vectors' % len(eigen_list)))
        for _, (tensor, mark) in enumerate(zip(eigen_list, self.eigen_update_list)):
            stats_eigen_var = self.eigen_reverse_lookup[mark]
            update_ops.append(
                tf.assign(stats_eigen_var, tensor, use_locking=True))

        with tf.control_dependencies(update_ops):
            factor_step_op = tf.assign_add(self.factor_step, 1)
            update_ops.append(factor_step_op)
            if KFAC_DEBUG:
                update_ops.append(tf.Print(tf.constant(
                    0.), [tf.convert_to_tensor('updated kfac factors')]))
        return update_ops 
开发者ID:Stable-Baselines-Team,项目名称:stable-baselines,代码行数:24,代码来源:kfac.py

示例7: _apply_cond

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def _apply_cond(self, apply_fn, grad, var, *args, **kwargs):
    """Apply conditionally if counter is zero."""
    grad_acc = self.get_slot(var, "grad_acc")

    def apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs):
      total_grad = (grad_acc + grad) / tf.cast(self._n_t, grad.dtype)
      adam_op = apply_fn(total_grad, var, *args, **kwargs)
      with tf.control_dependencies([adam_op]):
        grad_acc_to_zero_op = grad_acc.assign(tf.zeros_like(grad_acc),
                                              use_locking=self._use_locking)
      return tf.group(adam_op, grad_acc_to_zero_op)

    def accumulate_gradient(grad_acc, grad):
      assign_op = tf.assign_add(grad_acc, grad, use_locking=self._use_locking)
      return tf.group(assign_op)  # Strip return value

    return tf.cond(
        tf.equal(self._get_iter_variable(), 0),
        lambda: apply_adam(grad_acc, apply_fn, grad, var, *args, **kwargs),
        lambda: accumulate_gradient(grad_acc, grad)) 
开发者ID:yyht,项目名称:BERT,代码行数:22,代码来源:multistep_optimizer.py

示例8: call_fake_controller

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def call_fake_controller(push_values, pop_values, read_values, output_values):
  """Mock a RNN controller from a set of expected outputs.

  Args:
    push_values: Expected controller push values.
    pop_values: Expected controller pop values.
    read_values: Expected controller read values.
    output_values: Expected controller output values.

  Returns:
    A callable which behaves like the call method of an NeuralStackCell.
  """
  def call(cell, inputs, state, batch_size):
    del inputs
    del batch_size
    next_step = tf.assign_add(cell.current_step, tf.constant(1))
    return (
        tf.slice(tf.constant(push_values), [next_step, 0], [1, -1]),
        tf.slice(tf.constant(pop_values), [next_step, 0], [1, -1]),
        tf.slice(tf.constant(read_values), [next_step, 0, 0], [1, -1, -1]),
        tf.slice(tf.constant(output_values), [next_step, 0, 0], [1, -1, -1]),
        state
    )
  return call 
开发者ID:yyht,项目名称:BERT,代码行数:26,代码来源:neural_stack_test.py

示例9: test_ref_assign_add

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def test_ref_assign_add(self):
        # Currently ngraph and tf have different assign semantics
        # eval(ng.assign(a, 1)) resturns None, but eval(tf.assign(a, 1)) returns
        # a which is 1.
        # TODO: fix this test after assign op / user_deps are fixed in ngraph
        # TODO: double assignments fails

        # tf placeholder
        a = tf.Variable(tf.constant(np.random.randn(2, 3), name="a"))
        b = tf.Variable(tf.constant(np.random.randn(2, 3), name="b"))
        init_op = tf.global_variables_initializer()
        a_update = tf.assign_add(a, b)

        # test
        tf_result = self.tf_run(a_update, tf_init_op=init_op)
        ng_result = self.ng_run(a)
        ng.testing.assert_allclose(tf_result, ng_result) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:19,代码来源:test_ops_variable.py

示例10: build_act

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def build_act(actor_num, env, q_func, num_actions, eps, scope="deepq", data_format=None, reuse=None, replay_queue=None, prioritized_replay_eps=None, gamma=None, replay_queue_capacity=None, multi_step_n=1, framestack=4, num_actor_steps=None):
    # Build the actor using \epsilon-greedy exploration
    # Environments are wrapped with a data gathering class
    with tf.variable_scope('deepq', reuse=reuse):
        with tf.device('/gpu:0'):
            act_obs = env.observation()
            q_values = q_func(act_obs, num_actions, scope="read_q_func", data_format=data_format)
        deterministic_actions = tf.argmax(q_values, axis=1, output_type=tf.int32)
        batch_size = deterministic_actions.get_shape()[0]
        with tf.device('/cpu:0'):
            random_actions = tf.random_uniform(tf.stack([batch_size]), minval=0, maxval=num_actions, dtype=tf.int32)
            chose_random = tf.random_uniform(tf.stack([batch_size]), minval=0, maxval=1, dtype=tf.float32) < eps
            output_actions = tf.where(chose_random, random_actions, deterministic_actions)

            q_t_selected = tf.reduce_sum(q_values * tf.one_hot(output_actions, num_actions), 1)

        with tf.control_dependencies([tf.assign_add(num_actor_steps, batch_size, use_locking=True)]):
            return env.step(output_actions, q_values=q_values, q_t_selected=q_t_selected) 
开发者ID:uber-research,项目名称:ape-x,代码行数:20,代码来源:apex.py

示例11: _apply_dense

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def _apply_dense(self, grad, var):
    train_op = super(MockAdamOptimizer, self)._apply_dense(grad, var)
    counter = self.get_slot(var, "adam_counter")
    return tf.group(train_op, tf.assign_add(counter, [1.0])) 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:6,代码来源:composite_optimizer_test.py

示例12: advance_counters

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def advance_counters(self, total):
    """Returns ops to advance the per-component step and total counters.

    Args:
      total: Total number of actions to increment counters by.

    Returns:
      tf.Group op incrementing 'step' by 1 and 'total' by total.
    """
    update_total = tf.assign_add(self._total, total, use_locking=True)
    update_step = tf.assign_add(self._step, 1, use_locking=True)
    return tf.group(update_total, update_step) 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:14,代码来源:component.py

示例13: accumulate_privacy_spending

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def accumulate_privacy_spending(self, unused_eps_delta,
                                  sigma, num_examples):
    """Accumulate privacy spending.

    In particular, accounts for privacy spending when we assume there
    are num_examples, and we are releasing the vector
    (sum_{i=1}^{num_examples} x_i) + Normal(0, stddev=l2norm_bound*sigma)
    where l2norm_bound is the maximum l2_norm of each example x_i, and
    the num_examples have been randomly selected out of a pool of
    self.total_examples.

    Args:
      unused_eps_delta: EpsDelta pair which can be tensors. Unused
        in this accountant.
      sigma: the noise sigma, in the multiples of the sensitivity (that is,
        if the l2norm sensitivity is k, then the caller must have added
        Gaussian noise with stddev=k*sigma to the result of the query).
      num_examples: the number of examples involved.
    Returns:
      a TensorFlow operation for updating the privacy spending.
    """
    q = tf.cast(num_examples, tf.float64) * 1.0 / self._total_examples

    moments_accum_ops = []
    for i in range(len(self._log_moments)):
      moment = self._compute_log_moment(sigma, q, self._moment_orders[i])
      moments_accum_ops.append(tf.assign_add(self._log_moments[i], moment))
    return tf.group(*moments_accum_ops) 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:30,代码来源:accountant.py

示例14: testDiet

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def testDiet(self):

    params = diet.diet_adam_optimizer_params()

    @diet.fn_with_diet_vars(params)
    def model_fn(x):
      y = tf.layers.dense(x, 10, use_bias=False)
      return y

    @diet.fn_with_diet_vars(params)
    def model_fn2(x):
      y = tf.layers.dense(x, 10, use_bias=False)
      return y

    x = tf.random_uniform((10, 10))
    y = model_fn(x) + 10.
    y = model_fn2(y) + 10.
    grads = tf.gradients(y, [x])
    with tf.control_dependencies(grads):
      incr_step = tf.assign_add(tf.train.get_or_create_global_step(), 1)

    train_op = tf.group(incr_step, *grads)
    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      orig_vals = sess.run(tf.global_variables())
      for _ in range(10):
        sess.run(train_op)
      new_vals = sess.run(tf.global_variables())

      different = []
      for old, new in zip(orig_vals, new_vals):
        try:
          self.assertAllClose(old, new)
        except AssertionError:
          different.append(True)
      self.assertEqual(len(different), len(tf.global_variables())) 
开发者ID:akzaidi,项目名称:fine-lm,代码行数:38,代码来源:diet_test.py

示例15: testStop

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import assign_add [as 别名]
def testStop(self):
    global_step = tf.train.create_global_step()
    tf.summary.scalar("global_step", global_step)
    incr_global_step = tf.assign_add(global_step, 1)

    ckpt_dir = self.ckpt_dir("stop")
    dummy = DummyHook(ckpt_dir, every_n_steps=10)
    with self.sess(dummy, ckpt_dir) as sess:
      for _ in range(20):
        sess.run(incr_global_step)

      # Summary files should now have 2 global step values in them
      self.flush()

      # Run for 10 more so that the hook gets triggered again
      for _ in range(10):
        sess.run(incr_global_step)

      # Check that the metrics have actually been collected.
      self.assertTrue("" in dummy.test_metrics)
      metrics = dummy.test_metrics[""]
      self.assertTrue("global_step_1" in metrics)
      steps, vals = metrics["global_step_1"]
      self.assertTrue(len(steps) == len(vals))
      self.assertTrue(len(steps) >= 2)

      # Run for 10 more so that the hook triggers stoppage
      for _ in range(10):
        sess.run(incr_global_step)

      with self.assertRaisesRegexp(RuntimeError, "after should_stop requested"):
        sess.run(incr_global_step) 
开发者ID:akzaidi,项目名称:fine-lm,代码行数:34,代码来源:metrics_hook_test.py


注:本文中的tensorflow.assign_add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。