Pytorch中的高斯一维曲线

问题描述

我正在编写一个接受浮点标准偏差值并返回内核作为列向量的代码

但是,当我运行代码时,看起来高斯曲线是错误的。我知道一维高斯曲线的方程定义为:

Equation of Gaussian 1D

,其中我将内核大小k定义为4 * sigma + 1,平均值mu定义为k / 2。

错误的高斯曲线及其张量值在

this picture.

这是我的代码

def create_1D_Gaussian_kernel(standard_deviation: float) -> torch.FloatTensor:
    """Creates a 1D Gaussian kernel using the specified standard deviation.

    Note: ensure that the value of the kernel sums to 1.

    Args:
        standard_deviation (float): standard deviation of the gaussian

    Returns:
        torch.FloatTensor: required kernel as a column vector
    """

    kernel = torch.FloatTensor()
    torch.pi = torch.acos(torch.zeros(1)).item() *2 # define pi from torch


    kernel_size_k = 4 * standard_deviation + 1
    kernel = torch.arange(kernel_size_k)
    mean_mu = kernel_size_k / 2
    variance = standard_deviation ** 2
    normalization_Z = 1 / (torch.sqrt(torch.tensor(2 * torch.pi)) * standard_deviation)
    kernel = normalization_Z * torch.exp(-((kernel - mean_mu) ** 2) / (2 * variance))


    return kernel

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)