[英]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.