tensorflow - 使用 TensorFlow 函数,如何将小于 0.05 的值设置为零?

标签 tensorflow

我需要一个自定义激活,将小于 0.05 的张量值设置为零。我如何使用 TensorFlow 操作执行此操作?

最佳答案

您希望您的激活充当软阈值还是硬阈值? 让我解释一下:

  • 如果您想要的是软阈值,那么 ReLU 可以做到这一点:

    tf.nn.relu(x-0.05)
    

    然后对于域x>0.05,您的激活将不是身份,但会返回x-0.05

  • 如果您想要一个硬阈值,那么也许您可以使用 tf.sign(x-0.05) 来创建您的激活。可能有更简洁的方法来执行此操作,但以下代码可以做到这一点:

    x = tf.placeholder(tf.float32, [None, 1])
    hard_threshold = 0.5*(1+tf.sign(x-0.05))*x
    xx = np.array([[1.], [0.02], [-1.], [2]]) #test data
    with tf.Session() as session:
        print(session.run(hard_threshold, feed_dict={x: xx}))
    

关于tensorflow - 使用 TensorFlow 函数,如何将小于 0.05 的值设置为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963129/

相关文章:

python - 在 tensorflow 中,如何将索引列表转换为指标向量?

python - 错误 : ValueError: The last dimension of the inputs to `Dense` should be defined. 发现 `None`

tensorflow - 如何在 TensorFlow 的多 GPU 设置中使用批量归一化?

python - 在TensorFlow中显示图形图像?

python - keras.models load_model 中的 TypeError ('Keyword argument not understood:' , 'groups' )

python - 为什么 TensorFlow WHL 文件不包含所有必需的依赖项?

python - 如何在 tensorflow 中复制 numpy.choose() ?

python - Tensorflow c++,张量和馈送问题

machine-learning - Tensorflow 对于简单的 softmax 模型参数值没有变化

tensorflow - tensorflow /keras中输入的自相关