簡體   English   中英

張量流中的自定義層

[英]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())

如果要自定義圖層 ,只需將tf.layers.Dropout.call使用的核心函數替換為自己的圖層即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM