Pytorch: 甘贝尔噪声

创建于 2017-01-30  ·  3评论  ·  资料来源: pytorch/pytorch

你好,

我一直在尝试像在这里一样添加 Gumbel 噪音,但没有成功。
看起来仍然缺少几个模块(例如,基本变量函数,如 nn.Uniform()),还是我错了? 您将如何在 pytorch 中实现,例如以下行:

``` -- 创建噪声ε样本模块
本地噪声模块 = nn.Sequential()
no iseModule:add (nn.Uniform(0, 1)) -- 来自 U(0, 1) 的样本
-- 将均匀样本转换为 Gumbel 样本
no iseModule:add (nn.AddConstant(1e-9, true)) -- 提高数值稳定性
没有iseModule:add (nn.Log())
没有iseModule:add (nn.MulConstant(-1, true))
no iseModule:add (nn.AddConstant(1e-9, true)) -- 提高数值稳定性
没有iseModule:add (nn.Log())
没有iseModule:add (nn.MulConstant(-1, true))

-- 创建采样器 q(z) = G(z) = softmax((log(π) + ε)/τ)(重新参数化技巧)
本地采样器 = nn.Sequential()
本地采样器内部 = nn.ConcatTable()
样本rInternal:add (nn.Identity()) -- 非标准化对数概率 log(π)
sample rInternal:add (noiseModule) -- 创建噪声 ε
采样器:添加(采样器内部)
采样器:添加(nn.CAddTable())
self.temperature = nn.MulConstant(1 / self.tau, true) -- softmax 的温度 τ
采样器:添加(self.temperature)
sampler:add (nn.View(-1, self.k)) -- 调整大小以超过 k
采样器:添加(nn.SoftMax())
sampler:add (nn.View(-1, self.N * self.k)) -- 重新调整大小
```

最有用的评论

干得好。 更具可读性并且不需要模块:

import torch.nn.functional as F
from torch.autograd import Variable

def sampler(input, tau, temperature):
    noise = torch.rand(input.size())
    noise.add_(1e-9).log_().neg_()
    noise.add_(1e-9).log_().neg_()
    noise = Variable(noise)
    x = (input + noise) / tau + temperature
    x = F.softmax(x.view(input.size(0), -1))
    return x.view_as(input)

我们仅将 GitHub 用于错误报告,如果您有任何问题,请在我们的论坛上发布。

所有3条评论

干得好。 更具可读性并且不需要模块:

import torch.nn.functional as F
from torch.autograd import Variable

def sampler(input, tau, temperature):
    noise = torch.rand(input.size())
    noise.add_(1e-9).log_().neg_()
    noise.add_(1e-9).log_().neg_()
    noise = Variable(noise)
    x = (input + noise) / tau + temperature
    x = F.softmax(x.view(input.size(0), -1))
    return x.view_as(input)

我们仅将 GitHub 用于错误报告,如果您有任何问题,请在我们的论坛上发布。

感谢您的及时答复!当然,下次会在论坛上写。

感谢您的回答!

此页面是否有帮助?
0 / 5 - 0 等级