Tensorflow 2-如何直接在tf.Variable中有条件地更新值 问题:
请告知如何有条件地更新原始tf.Variable。这是一个与之不同的问题 背景Tensorflow 2-如何直接在tf.Variable中有条件地更新值 问题:,tensorflow,Tensorflow,请告知如何有条件地更新原始tf.Variable。这是一个与之不同的问题 背景 tf.Variable是可变的,assign方法将更新相同的内存区域。看起来assign方法在赋值时没有包含条件的选项。因此,我认为应该有条件地更新tf.Variable 在numpy中,可以使用索引直接更新numpy数组,但在Tensorflow中似乎没有这种方法 # Numpy conditional uddate with boolean indexing x = np.random.uniform(-1
tf.Variable
是可变的,assign
方法将更新相同的内存区域。看起来assign
方法在赋值时没有包含条件的选项。因此,我认为应该有条件地更新tf.Variable
# Numpy conditional uddate with boolean indexing
x = np.random.uniform(-1, 1, size=(3, 4))
x[x > 0] = 0
问题
由于tf.Variable
是可变的,预计tf.where
将对原始变量进行变异,但是如下所示,原始变量x
尚未更新
x = tf.Variable(np.random.uniform(-1, 1, size=(3,4)), dtype=tf.float32)
print(f"x:{x}\n")
print(f"x > 0:\n{x > 0}\n")
print(f"tf.where(x>0, 1, x):\n{tf.where(x>0, 1, x)}")
x # check if updated
结果:
# --------------------------------------------------------------------------------
# Original tf.Variable x
# --------------------------------------------------------------------------------
x is <tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0.8015974 , 0.8223503 , -0.2704468 , 0.01874248],
[ 0.46989247, 0.4753061 , -0.06808566, -0.57646054],
[ 0.07082719, 0.2924774 , -0.12741995, 0.3168819 ]],
dtype=float32)>
x > 0 is [[ True True False True]
[ True True False False]
[ True True False True]]
# --------------------------------------------------------------------------------
# Update using tf.where
# --------------------------------------------------------------------------------
tf.where(x>0, 1, x)=
[[ 1. 1. -0.2704468 1. ]
[ 1. 1. -0.06808566 -0.57646054]
[ 1. 1. -0.12741995 1. ]]
# --------------------------------------------------------------------------------
# The x is the same as before.
# --------------------------------------------------------------------------------
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[ 0.8015974 , 0.8223503 , -0.2704468 , 0.01874248],
[ 0.46989247, 0.4753061 , -0.06808566, -0.57646054],
[ 0.07082719, 0.2924774 , -0.12741995, 0.3168819 ]],
dtype=float32)>
对于张量,它没有像“assign”或“assign\u add”等方法
我认为创建一个包含
您的幻灯片的均值/方差值,并在
单元格的call()主体。一旦该层遍历所有时间步,您就可以
可以为TensorArray执行堆栈并将值赋回
变量本身。您不必连续写入变量
在处理timestep时,因为timestep t不应该影响
结果为t+1(如果我正确理解您的问题)
tf.Variable
,您需要调用assign
。请阅读指南中的更多内容
>>将tensorflow作为tf导入
>>>tf.random.set_种子(0)
>>>x=tf.Variable(tf.random.uniform((3,4),-1,1),dtype=tf.float32)
>>>x
>>>x.赋值(tf.其中(x>0,x,0))
>>>x
这里还有一些案例。主要的收获是要更新tf.Variable
中的内容,我们使用assign
。但请注意,它会急切地赋值
x = tf.Variable(range(5), shape=(5,), name="a")
x.numpy()
array([0, 1, 2, 3, 4], dtype=int32)
x.assign(tf.where(x > 2, x, -1)).numpy()
array([-1, -1, -1, 3, 4], dtype=int32)
x.assign(range(5,10)).numpy()
array([5, 6, 7, 8, 9], dtype=int32)
x.assign(tf.where(x < 8, tf.range(5), tf.where(x > 8 , x, -1))).numpy()
array([ 0, 1, 2, -1, 9], dtype=int32)
x=tf.Variable(范围(5),形状=(5),name=“a”)
x、 numpy()
数组([0,1,2,3,4],dtype=int32)
x、 赋值(tf.where(x>2,x,-1)).numpy()
数组([-1,-1,-1,3,4],dtype=int32)
x、 赋值(范围(5,10)).numpy()
数组([5,6,7,8,9],dtype=int32)
x、 赋值(tf.where(x<8,tf.range(5),tf.where(x>8,x,-1)).numpy()
数组([0,1,2,-1,9],dtype=int32)
var_slice = var[4:5]
var_slice.assign(math_ops.sub(var, const))
>>> import tensorflow as tf
>>> tf.random.set_seed(0)
>>> x = tf.Variable(tf.random.uniform((3,4),-1,1), dtype=tf.float32)
>>> x
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[-0.41604972, -0.5868671 , 0.07078147, 0.12251496],
[-0.16665101, 0.6156559 , -0.0135498 , 0.9962585 ],
[ 0.3934703 , -0.7492528 , 0.4196334 , 0.32483125]],
dtype=float32)>
>>> x.assign(tf.where(x>0,x,0))
<tf.Variable 'UnreadVariable' shape=(3, 4) dtype=float32, numpy=
array([[0. , 0. , 0.07078147, 0.12251496],
[0. , 0.6156559 , 0. , 0.9962585 ],
[0.3934703 , 0. , 0.4196334 , 0.32483125]], dtype=float32)>
>>> x
<tf.Variable 'Variable:0' shape=(3, 4) dtype=float32, numpy=
array([[0. , 0. , 0.07078147, 0.12251496],
[0. , 0.6156559 , 0. , 0.9962585 ],
[0.3934703 , 0. , 0.4196334 , 0.32483125]], dtype=float32)>
x = tf.Variable(range(5), shape=(5,), name="a")
x.numpy()
array([0, 1, 2, 3, 4], dtype=int32)
x.assign(tf.where(x > 2, x, -1)).numpy()
array([-1, -1, -1, 3, 4], dtype=int32)
x.assign(range(5,10)).numpy()
array([5, 6, 7, 8, 9], dtype=int32)
x.assign(tf.where(x < 8, tf.range(5), tf.where(x > 8 , x, -1))).numpy()
array([ 0, 1, 2, -1, 9], dtype=int32)