TensorFlow 中的硬限制/阈值激活函数 [英] Hard limiting / threshold activation function in TensorFlow

查看:52
本文介绍了TensorFlow 中的硬限制/阈值激活函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 TensorFlow 0.9 中实现一个基本的二进制 Hopfield Network.不幸的是,我很难让激活功能正常工作.我正在寻找非常简单的 If net[i] <0, output[i] = 0, else output[i] = 1 但我尝试过的一切似乎都消除了渐变,即在尝试实现训练操作.

I'm trying to implement a basic, binary Hopfield Network in TensorFlow 0.9. Unfortunately I'm having a very hard time getting the activation function working. I'm looking to get the very simple If net[i] < 0, output[i] = 0, else output[i] = 1 but everything I've tried seems to remove the gradient, i.e. I get the "No gradients provided for any variable" exception when trying to implement the training op.

例如,我尝试将 tf.less() 转换为 float,我尝试按照

For example, I tried casting tf.less() to float, I tried doing something along the lines of

tf.maximum(tf.minimum(net, 0) + 1, 0)

但我忘记了小十进制值.我终于做到了

but I forgot about small decimal values. Finally I did

tf.maximum(tf.floor(tf.minimum(net, 0) + 1), 0)

tf.floor 不注册渐变.我还尝试将地板替换为转换为 int 的转换,然后转换回浮动但同样的操作.

but tf.floor doesn't register gradients. I also tried replacing the floor with a cast to int and then a cast back to float but same deal.

对我能做什么有什么建议吗?

Any suggestions on what I could do?

推荐答案

有点晚了,但如果有人需要它,我使用了这个定义

a bit late, but if anyone needs it, I used this definition

def binary_activation(x):

    cond = tf.less(x, tf.zeros(tf.shape(x)))
    out = tf.where(cond, tf.zeros(tf.shape(x)), tf.ones(tf.shape(x)))

    return out

x 是张量

这篇关于TensorFlow 中的硬限制/阈值激活函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆