通過從中減去 value
來更新 ref
。
用法
tf.compat.v1.assign_sub(
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_sub
主要與即刻執行和 tf.function
兼容。
要切換到原生 TF2 風格,可以使用 tf.Variable
的 'assign_sub' 方法:
如何映射參數
TF1 參數名稱 | TF2 參數名稱 | 注意 |
---|---|---|
ref |
self |
在assign_sub() 方法中 |
value |
value |
在assign_sub() 方法中 |
use_locking |
use_locking |
在assign_sub() 方法中 |
name |
name |
在assign_sub() 方法中 |
- | read_value
|
設置為 True 以複製行為(True 是默認值) |
使用示例之前和之後
前:
with tf.Graph().as_default():
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.Variable(1, dtype=tf.int64)
sess.run(a.initializer)
update_op = tf.compat.v1.assign_sub(a, 1)
res_a = sess.run(update_op)
res_a
0
後:
b = tf.Variable(1, dtype=tf.int64)
res_b = b.assign_sub(1)
res_b.numpy()
0
此操作在更新完成後輸出ref
。這使得鏈接需要使用重置值的操作更容易。與 tf.math.subtract
不同,此操作不廣播。 ref
和value
必須具有相同的形狀。
相關用法
- Python tf.compat.v1.assign_add用法及代碼示例
- 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_sub。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。