有没有方法在PyTorch中为张量生成分段函数?

huangapple go评论121阅读模式
英文:

Is there any method to generate a piecewise function for tensors in pytorch?

问题

我想要在PyTorch中为张量创建一个分段函数,类似这样。但我不知道如何定义它。我使用了一种非常愚蠢的方法来实现它,但似乎在我的代码中不起作用。

def trapezoid(self, X):
    Y = torch.zeros(X.shape)
    Y[X % (2 * pi) < (0.5 * pi)] = (X[X % (2 * pi) < (0.5 * pi)] % (2 * pi)) * 2 / pi
    Y[(X % (2 * pi) >= (0.5 * pi)) & (X % (2 * pi) < 1.5 * pi)] = 1.0
    Y[X % (2 * pi) >= (1.5 * pi)] = (X[X % (2 * pi) >= (1.5 * pi)] % (2 * pi)) * (-2 / pi) + 4
    return Y

你能帮我找出如何设计函数trapezoid,以便对于张量X,我可以直接使用trapezoid(X) 来获得结果吗?

英文:

有没有方法在PyTorch中为张量生成分段函数?

I want to get a piecewise function like this for tensors in pytorch. But I don't know how to define it. I use a very stupid method to do it, but it seems not to work in my code.

    def trapezoid(self, X):
        Y = torch.zeros(X.shape)
        Y[X % (2 * pi) &lt; (0.5 * pi)] = (X[X % (2 * pi) &lt; (0.5 * pi)] % (2 * pi)) * 2 / pi
        Y[(X % (2 * pi) &gt;= (0.5 * pi)) &amp; (X % (2 * pi) &lt; 1.5 * pi)] = 1.0
        Y[X % (2 * pi) &gt;= (1.5 * pi)] = (X[X % (2 * pi) &gt;= (1.5 * pi)] % (2 * pi)) * (-2 / pi) + 4
        return Y

could do you help me find out how to design the function trapezoid, so that for tensor X, I can get the result directly using trapezoid(X)?

答案1

得分: 1

由于您的函数具有2π的周期,我们可以关注[0,2π]。由于它是分段线性的,可以将其表示为在[0,2π]上的小型ReLU网络,如下所示:

trapezoid(x) = 1 - relu(x-1.5π)/0.5π - relu(0.5π-x)/0.5π

因此,我们可以像这样在Pytorch中编写整个函数:

import torch
import torch.nn.functional as F
from torch import tensor 
from math import pi

def trapezoid(X):
  # 左角位置、右角位置、高度
  a, b, h = tensor(0.5*pi), tensor(1.5*pi), tensor(1.0)

  # 对2*pi取余以实现周期性
  X = torch.remainder(X,2*pi)

  return h - F.relu(X-b)/a - F.relu(a-X)/a

绘图以进行双重检查并生成正确的图像:

import matplotlib.pyplot as plt

X = torch.linspace(-10,10,1000)
Y = trapezoid(X)
plt.plot(X,Y)
plt.title('Pytorch Trapezoid Function')

有没有方法在PyTorch中为张量生成分段函数?

英文:

Since your function has period 2π we can focus on [0,2π]. Since it's piecewise linear, it's possible to express it as a mini ReLU network on [0,2π] given by:

trapezoid(x) = 1 - relu(x-1.5π)/0.5π - relu(0.5π-x)/0.5π

Thus, we can code the whole function in Pytorch like so:

import torch
import torch.nn.functional as F
from torch import tensor 
from math import pi

def trapezoid(X):
  # Left corner position, right corner position, height
  a, b, h = tensor(0.5*pi), tensor(1.5*pi), tensor(1.0)

  # Take remainder mod 2*pi for periodicity
  X = torch.remainder(X,2*pi)

  return h - F.relu(X-b)/a - F.relu(a-X)/a

Plotting to double check produces the correct picture:

import matplotlib.pyplot as plt

X = torch.linspace(-10,10,1000)
Y = trapezoid(X)
plt.plot(X,Y)
plt.title(&#39;Pytorch Trapezoid Function&#39;)

有没有方法在PyTorch中为张量生成分段函数?

huangapple
  • 本文由 发表于 2020年1月3日 20:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定