【问题标题】:Inverse transform逆变换
【发布时间】:2020-11-28 08:59:10
【问题描述】:

我无法找到一种有效的方法来为该函数提供批量输入并返回批量输出。我想在我的神经网络训练期间这样做。

Inverse_Norm = transforms.Normalize(
   mean = [-m/s for m, s in zip(mean, std)],
   std = [1/s for s in std]
)
inverse_norm_input = Inverse_Norm(input)

【问题讨论】:

    标签: python-3.x machine-learning deep-learning pytorch data-science


    【解决方案1】:

    假设一个形状为(B, C, ...) 的张量其中meanstd 是长度为C 的可迭代对象,那么您可以使用广播语义来跨批处理张量进行操作。例如

    import torch
    
    def batch_inverse_normalize(x, mean, std):
        # represent mean and std to 1, C, 1, ... tensors for broadcasting
        reshape_shape = [1, -1] + ([1] * (len(x.shape) - 2))
        mean = torch.tensor(mean, device=x.device, dtype=x.dtype).reshape(*reshape_shape)
        std = torch.tensor(std, device=x.device, dtype=x.dtype).reshape(*reshape_shape)
        return x * std + mean
    

    【讨论】:

    • @x2212 我意识到在返回这个答案时有一个错字。 / 应该是 *,这已经更正了。
    • 我很早就意识到了,但感谢您的更正。
    猜你喜欢
    • 2019-01-20
    • 2020-12-07
    • 1970-01-01
    • 2020-01-05
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多