本文簡要介紹python語言中 torch.jit.Attribute 的用法。
用法:
class torch.jit.Attribute(value, type)value-要分配給屬性的初始值。
type-Python 類型
返回
value此方法是一個返回
value的 pass-through 函數,主要用於向 TorchScript 編譯器指示左側表達式是類型為type的類實例屬性。請注意,torch.jit.Attribute隻能在nn.Module子類的__init__方法中使用。雖然 TorchScript 可以推斷出大多數 Python 表達式的正確類型,但在某些情況下類型推斷可能會出錯,包括:
空容器,例如
[]和{},其中 TorchScript 假定為Tensor的容器可選類型,如
Optional[T]但分配了T類型的有效值,TorchScript 會假設它是類型T而不是Optional[T]
在 Eager 模式下,它隻是一個 pass-through 函數,它返回
value而沒有其他含義。例子:
import torch from typing import Dict class AttributeModule(torch.nn.Module): def __init__(self): super(M, self).__init__() self.foo = torch.jit.Attribute(0.1, float) # we should be able to use self.foo as a float here assert 0.0 < self.foo self.names_ages = torch.jit.Attribute({}, Dict[str, int]) self.names_ages["someone"] = 20 assert isinstance(self.names_ages["someone"], int) m = AttributeModule() # m will contain two attributes # 1. foo of type float # 2. names_ages of type Dict[str, int]
參數:
返回:
相關用法
- Python PyTorch AvgPool2d用法及代碼示例
- Python PyTorch AdaptiveAvgPool3d用法及代碼示例
- Python PyTorch AvgPool1d用法及代碼示例
- Python PyTorch AdaptiveMaxPool1d用法及代碼示例
- Python PyTorch AvgPool3d用法及代碼示例
- Python PyTorch AlphaDropout用法及代碼示例
- Python PyTorch AdaptiveAvgPool1d用法及代碼示例
- Python PyTorch AdaptiveMaxPool2d用法及代碼示例
- Python PyTorch AdaptiveAvgPool2d用法及代碼示例
- Python PyTorch AdaptiveMaxPool3d用法及代碼示例
- Python PyTorch frexp用法及代碼示例
- Python PyTorch jvp用法及代碼示例
- Python PyTorch cholesky用法及代碼示例
- Python PyTorch vdot用法及代碼示例
- Python PyTorch ELU用法及代碼示例
- Python PyTorch ScaledDotProduct.__init__用法及代碼示例
- Python PyTorch gumbel_softmax用法及代碼示例
- Python PyTorch get_tokenizer用法及代碼示例
- Python PyTorch saved_tensors_hooks用法及代碼示例
- Python PyTorch positive用法及代碼示例
- Python PyTorch renorm用法及代碼示例
- Python PyTorch MaxUnpool3d用法及代碼示例
- Python PyTorch Bernoulli用法及代碼示例
- Python PyTorch Tensor.unflatten用法及代碼示例
- Python PyTorch Sigmoid用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.jit.Attribute。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
