我有下面的代码,我想将其翻译成 pytorch。在这种情况下,我正在寻找一种将 np.vectorize 转换为任何 pytorch 方式的方法

社会演员多 pytorch 472

原文标题I have the code below which I want to translate into pytorch. I’m looking for a way to translate np.vectorize to any pytorch way in this case

我需要将此代码翻译成 pytorch。下面给出的代码使用 np.vectorize。我正在寻找与此等效的 pytorch。

class SimplexPotentialProjection(object):
    def __init__(self, potential, inversePotential, strong_convexity_const, precision = 1e-10):
        self.inversePotential = inversePotential
        self.gradPsi = np.vectorize(potential)
        self.gradPsiInverse = np.vectorize(inversePotential)
        self.precision = precision
        self.strong_convexity_const = strong_convexity_const

原文链接:https://stackoverflow.com//questions/71553309/i-have-the-code-below-which-i-want-to-translate-into-pytorch-im-looking-for-a

回复

我来回复
  • Shai的头像
    Shai 评论

    numpy.vectorize的文档清楚地指出:

    vectorize功能主要是为了方便,而不是为了性能。该实现本质上是一个 for 循环。

    因此,为了将您的 numpycode 转换为 pytorch,您只需要在其张量参数上循环应用potentialinversePotential。但是,这可能非常低效。您最好重新实现您的函数以“本机”以矢量化方式运行张量。

    2年前 0条评论