[英]Custom Layers in tensorflow
我正在尝试对tensorflow中的内置dropout函数进行一些更改。 这样做的最佳过程是什么?
我想对正向和反向传播步骤进行一些更改。 在Tensorflow实现中,我只能找到前向通过而不是后向通过。 我想同时修改正向和反向传递。
您可以使用tf.custom_gradient在单个方法中定义自己的前进和后退步骤。 这是一个简单的示例:
import tensorflow as tf
tf.InteractiveSession()
@tf.custom_gradient
def custom_multiply(a, x):
# Define your own forward step
y = a * x
# Define your own backward step
def grads(dy): return dy * x, dy * a + 100
# Return the forward result and the backward function
return y, grads
a, x = tf.constant(2), tf.constant(3)
y = custom_multiply(a, x)
dy_dx = tf.gradients(y, x)[0]
# It will print `dy/dx = 102` instead of 2 if the gradient is not customized
print('dy/dx =', dy_dx.eval())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.