【问题标题】:constants in Pytorch Linear Module Class DefinitionPytorch 线性模块类定义中的常量
【发布时间】:2019-12-22 16:05:18
【问题描述】:

pytorch中class Linear(Module):中的__constants__https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html中定义是什么?

它的功能是什么?为什么要使用它?

我一直在四处寻找,但没有找到任何文档。请注意,这并不意味着火炬脚本中的__constants__

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    您所说的__constants__ 实际上是与TorchScript 相关的那个。您可以使用 GitHub 上的git blame(添加时间和添加者) 来确认。例如,对于torch/nn/modules/linear.py,请检查其git blame

    TorchScript 还提供了一种使用 Python 中定义的常量的方法。这些可用于将超参数硬编码到函数中,或定义通用常量。

    -- ScriptModule 的属性可以通过将它们列为类的 constants 属性的成员来标记为常量:

    class Foo(torch.jit.ScriptModule):
        __constants__ = ['a']
    
        def __init__(self):
            super(Foo, self).__init__(False)
            self.a = 1 + 4
    
       @torch.jit.script_method
       def forward(self, input):
           return self.a + input
    

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 2019-05-01
      • 2021-03-07
      • 1970-01-01
      • 2020-03-19
      • 2020-04-01
      • 2016-02-24
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多