通过添加value 来更新ref。
用法
tf.compat.v1.assign_add(
ref, value, use_locking=None, name=None
)参数
-
ref一个可变的Tensor。必须是以下类型之一:float32,float64,int64,int32,uint8,uint16,int16,int8,complex64,complex128,qint8,quint8,qint32,half。应该来自Variable节点。 -
value一个Tensor。必须具有与ref相同的形状和 dtype。要添加到变量的值。 -
use_locking可选的bool。默认为False。如果为 True,则添加将受锁保护;否则行为是未定义的,但可能表现出较少的争用。 -
name操作的名称(可选)。
返回
-
与
ref相同。为方便在变量更新后想要使用新值的操作而返回。
迁移到 TF2
警告:这个 API 是为 TensorFlow v1 设计的。继续阅读有关如何从该 API 迁移到本机 TensorFlow v2 等效项的详细信息。见TensorFlow v1 到 TensorFlow v2 迁移指南有关如何迁移其余代码的说明。
tf.compat.v1.assign_add 主要与即刻执行和 tf.function 兼容。
要切换到原生 TF2 风格,可以使用 tf.Variable 的 'assign_add' 方法:
如何映射参数
| TF1 参数名称 | TF2 参数名称 | 注意 |
|---|---|---|
ref |
self |
在assign_add() 方法中 |
value |
value |
在assign_add() 方法中 |
use_locking |
use_locking |
在assign_add() 方法中 |
name |
name |
在assign_add() 方法中 |
| - | read_value
|
设置为 True 以复制行为(True 是默认值) |
使用示例之前和之后
前:
with tf.Graph().as_default():
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.Variable(0, dtype=tf.int64)
sess.run(a.initializer)
update_op = tf.compat.v1.assign_add(a, 1)
res_a = sess.run(update_op)
res_a
1
后:
b = tf.Variable(0, dtype=tf.int64)
res_b = b.assign_add(1)
res_b.numpy()
1
此操作在更新完成后输出ref。这使得链接需要使用重置值的操作更容易。与 tf.math.add 不同,此操作不广播。 ref 和value 必须具有相同的形状。
相关用法
- Python tf.compat.v1.assign_sub用法及代码示例
- Python tf.compat.v1.assign用法及代码示例
- Python tf.compat.v1.assert_none_equal用法及代码示例
- Python tf.compat.v1.assert_near用法及代码示例
- Python tf.compat.v1.assert_negative用法及代码示例
- Python tf.compat.v1.assert_less_equal用法及代码示例
- Python tf.compat.v1.assert_non_positive用法及代码示例
- Python tf.compat.v1.assert_integer用法及代码示例
- Python tf.compat.v1.assert_greater用法及代码示例
- Python tf.compat.v1.assert_non_negative用法及代码示例
- Python tf.compat.v1.assert_rank_at_least用法及代码示例
- Python tf.compat.v1.assert_less用法及代码示例
- Python tf.compat.v1.assert_rank_in用法及代码示例
- Python tf.compat.v1.assert_greater_equal用法及代码示例
- Python tf.compat.v1.assert_rank用法及代码示例
- Python tf.compat.v1.assert_positive用法及代码示例
- Python tf.compat.v1.assert_equal用法及代码示例
- Python tf.compat.v1.autograph.to_graph用法及代码示例
- Python tf.compat.v1.arg_max用法及代码示例
- Python tf.compat.v1.arg_min用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.assign_add。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
