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

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

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

问题

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

  1. def trapezoid(self, X):
  2. Y = torch.zeros(X.shape)
  3. Y[X % (2 * pi) < (0.5 * pi)] = (X[X % (2 * pi) < (0.5 * pi)] % (2 * pi)) * 2 / pi
  4. Y[(X % (2 * pi) >= (0.5 * pi)) & (X % (2 * pi) < 1.5 * pi)] = 1.0
  5. Y[X % (2 * pi) >= (1.5 * pi)] = (X[X % (2 * pi) >= (1.5 * pi)] % (2 * pi)) * (-2 / pi) + 4
  6. 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.

  1. def trapezoid(self, X):
  2. Y = torch.zeros(X.shape)
  3. Y[X % (2 * pi) &lt; (0.5 * pi)] = (X[X % (2 * pi) &lt; (0.5 * pi)] % (2 * pi)) * 2 / pi
  4. Y[(X % (2 * pi) &gt;= (0.5 * pi)) &amp; (X % (2 * pi) &lt; 1.5 * pi)] = 1.0
  5. Y[X % (2 * pi) &gt;= (1.5 * pi)] = (X[X % (2 * pi) &gt;= (1.5 * pi)] % (2 * pi)) * (-2 / pi) + 4
  6. 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中编写整个函数:

  1. import torch
  2. import torch.nn.functional as F
  3. from torch import tensor
  4. from math import pi
  5. def trapezoid(X):
  6. # 左角位置、右角位置、高度
  7. a, b, h = tensor(0.5*pi), tensor(1.5*pi), tensor(1.0)
  8. # 对2*pi取余以实现周期性
  9. X = torch.remainder(X,2*pi)
  10. return h - F.relu(X-b)/a - F.relu(a-X)/a

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

  1. import matplotlib.pyplot as plt
  2. X = torch.linspace(-10,10,1000)
  3. Y = trapezoid(X)
  4. plt.plot(X,Y)
  5. 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:

  1. import torch
  2. import torch.nn.functional as F
  3. from torch import tensor
  4. from math import pi
  5. def trapezoid(X):
  6. # Left corner position, right corner position, height
  7. a, b, h = tensor(0.5*pi), tensor(1.5*pi), tensor(1.0)
  8. # Take remainder mod 2*pi for periodicity
  9. X = torch.remainder(X,2*pi)
  10. return h - F.relu(X-b)/a - F.relu(a-X)/a

Plotting to double check produces the correct picture:

  1. import matplotlib.pyplot as plt
  2. X = torch.linspace(-10,10,1000)
  3. Y = trapezoid(X)
  4. plt.plot(X,Y)
  5. 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:

确定