十位数自定义图层

2024-05-29 04:44:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对tensorflow中内置的dropout函数进行一些更改。这样做的最佳程序是什么?在

我想在前推和后推步骤中做一些更改。在Tensorflow Implementation中,我只能找到向前传球而不是向后传球。 我想修改前后传球。在


Tags: 函数程序tensorflow步骤内置implementationdropout传球
1条回答
网友
1楼 · 发布于 2024-05-29 04:44:27

您可以使用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())

如果您想customize your own layer,只需将tf.layers.Dropout.call中使用的核心函数替换为您自己的核心函数

相关问题 更多 >

    热门问题