Tensorflow 剪裁(筛选)tf.placeholder值

Tensorflow 剪裁(筛选)tf.placeholder值,tensorflow,Tensorflow,我想更改tf.placeholder值,以便: 值

我想更改tf.placeholder值,以便:

它不是精确的剪裁,所以我不能使用:
tf.clip\u by\u value()

我尝试了这个建议,到目前为止我已经做到了:

x = tf.placeholder(tf.float32, None)
condition = tf.less(x, tf.constant(SmallConst))
tf.assign(x, tf.where(condition, tf.zeros_like(x), x))
然而,在运行此命令时,我得到一个错误,即

AttributeError: 'Tensor' object has no attribute 'assign'
似乎
tf.assign()
可以在
tf.Variable
上执行,但不能在
tf.placeholder
上执行

我还有别的办法吗


谢谢大家!

是的,这比你想象的还要容易:

x = tf.placeholder(tf.float32, None)
# create a bool tensor the same shape as x
condition = x < SmallConst 

# create tensor same shape as x, with values greater than SmallConst set to 0
to_remove = x*tf.to_float(condition)

# set all values of x less than SmallConst to 0
x_clipped = x - to_remove 
努比也是如此


只对变量有效,因为它将

通过给“ref”赋值来更新“ref”


tensorflow中的张量是不变的。对张量进行任何运算的结果都是另一个张量,但原始张量永远不会改变。然而,变量是可变的,您可以使用
tf.assign()

更改它们的值!谢谢。
x_clipped = x - x*tf.to_float(x < small_const)
a = tf.placeholder(tf.int32)
b = a + 1
c = a > 0
print(b) # gives "<tf.Tensor 'add:0' shape=<unknown> dtype=int32>"
print(c) # gives "<tf.Tensor 'Greater:0' shape=<unknown> dtype=bool>"
sess.run(b, {a: 1}) # gives scalar int32 numpy array with value 2
sess.run(c, {a: 1}) # gives scalar bool numpy array with value True