我正在尝试按照本文所述实现"feed-forward convolutional/deconvolutional residual encoder":https://arxiv.org/abs/1511.06085
在网络架构中,他们使用二值化层,在这里他们首先使用具有tanh激活的标准全连接层来生成具有连续区间[-1,1]中的分量的向量 . 然后,他们概率地将每个组件映射到-1或1.现在的问题是反向传播不是通常用于第二步 . 经过一些推理,他们说他们通过这个阶段的梯度不变 .
现在我的问题是,如何在tensorflow中实现这一点?有没有简单的方法来定义操作的自定义渐变?一个简单的例子将非常感激 .
EDIT:
以下代码是否符合我的要求?
def binarization(x):
g = tf.get_default_graph()
with ops.name_scope("Binarization") as name:
with g.gradient_override_map({"Ceil": "Identity",
"Sub": "CustomGrad",
"Div": "CustomGrad",
"Add": "CustomGrad",
"Mul": "CustomGrad"}):
scaled_x = (x + 1) / 2
binary_x = tf.ceil(scaled_x - tf.random_uniform(tf.shape(x)), name=name)
return (binary_x * 2) - 1
@ops.RegisterGradient("CustomGrad")
def customGrad(op, grad):
return [grad, tf.zeros(tf.shape(op.inputs[1]))]