gpt4 book ai didi

machine-learning - 如何在 TensorFlow 中执行可微分运算选择?

转载 作者:行者123 更新时间:2023-11-30 08:59:52 25 4
gpt4 key购买 nike

我正在尝试生成一个基于标量输入的数学运算选择神经网络模型。该操作是根据 nn 生成的 softmax 结果来选择的。然后必须将此操作应用于标量输入才能产生最终输出。到目前为止,我已经提出在 softmax 输出上应用 argmax 和 onehot 来生成一个掩码,然后将其应用于要执行的所有可能操作的连接值矩阵上(如下面的伪代码所示)。问题是 argmax 和 onehot 似乎都不可微分。我对此很陌生,所以任何人都会受到高度赞赏。提前致谢。

    #perform softmax    
logits = tf.matmul(current_input, W) + b
softmax = tf.nn.softmax(logits)

#perform all possible operations on the input
op_1_val = tf_op_1(current_input)
op_2_val = tf_op_2(current_input)
op_3_val = tf_op_2(current_input)
values = tf.concat([op_1_val, op_2_val, op_3_val], 1)

#create a mask
argmax = tf.argmax(softmax, 1)
mask = tf.one_hot(argmax, num_of_operations)

#produce the input, by masking out those operation results which have not been selected
output = values * mask

最佳答案

我认为这是不可能的。这类似于 paper 中描述的 Hard Attention 。图像字幕中使用硬注意力,使模型在每一步仅关注图像的特定部分。硬注意力是不可微分的,但有两种方法可以解决这个问题:

1- 使用强化学习 (RL):强化学习是为了训练做出决策的模型。尽管损失函数不会将任何梯度反向传播到用于决策的 softmax,但您可以使用 RL 技术来优化决策。举个简单的例子,您可以将损失视为惩罚,并向节点发送与 softmax 层中的最大值、与惩罚成比例的策略梯度,以便在决策不好时降低决策的分数(结果处于高损失状态)。

2- 使用软注意力之类的东西:不要只选择一种操作,而是将它们与基于 softmax 的权重混合。所以而不是:

output = values * mask

用途:

output = values * softmax

现在,这些操作将根据 softmax 选择它们的程度而收敛到零。与 RL 相比,这更容易训练,但如果必须从最终结果中完全删除未选择的操作(将它们完全设置为零),则它将不起作用。

这是另一个讨论硬注意力和软注意力的答案,您可能会觉得有帮助:https://stackoverflow.com/a/35852153/6938290

关于machine-learning - 如何在 TensorFlow 中执行可微分运算选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com