![](/img/trans.png)
[英]Tensorflow: how to keep batch dimension when using tf.where()?
[英]How to replace particular values based on condition by using tf.where()
我想替换条件下的值。
NumPy版本会像这样
intensity=np.where(
np.abs(intensity)<1e-4,
1e-4,
intensity)
但是TensorFlow对tf.where()的用法有些不同
当我尝试这个
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
1e-4,
intensity)
我得到这个错误
ValueError: Shapes must be equal rank, but are 0 and 4 for 'Select' (op: 'Select') with input shapes: [?,512,512,1], [], [?,512,512,1].
这是否意味着我应该为1e-4
4维张量?
以下代码传递了错误
# Create an array which has small value (1e-4),
# whose shape is (2,512,512,1)
small_val=np.full((2,512,512,1),1e-4).astype("float32")
# Convert numpy array to tf.constant
small_val=tf.constant(small_val)
# Use tf.where()
intensity=tf.where(
tf.math.abs(intensity)<1e-4,
small_val,
intensity)
# Error doesn't occur
print(intensity.shape)
# (2, 512, 512, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.