torchrec.models¶
torchrec.models.deepfm¶
- 類別 torchrec.models.deepfm.DenseArch(in_features: int, hidden_layer_size: int, embedding_dim: int)¶
基底:
Module
處理 DeepFMNN 模型的密集特徵。輸出層的大小調整為 EmbeddingBagCollection 嵌入的 embedding_dimension。
- 參數::
in_features (int) – 密集輸入特徵的維度。
hidden_layer_size (int) – DenseArch 中隱藏層的大小。
embedding_dim (int) – 與 sparseArch 的 embedding_dimension 大小相同。
device (torch.device) – 預設計算裝置。
範例
B = 20 D = 3 in_features = 10 dense_arch = DenseArch( in_features=in_features, hidden_layer_size=10, embedding_dim=D ) dense_arch_input = torch.rand((B, in_features)) dense_embedded = dense_arch(dense_arch_input)
- forward(features: Tensor) Tensor ¶
- 參數::
features (torch.Tensor) – 大小為 B X num_features。
- 傳回::
大小為 B X D 的輸出張量。
- 傳回類型::
torch.Tensor
- training: bool¶
- 類別 torchrec.models.deepfm.FMInteractionArch(fm_in_features: int, sparse_feature_names: List[str], deep_fm_dimension: int)¶
基底:
Module
處理 SparseArch (sparse_features) 和 DenseArch (dense_features) 的輸出,並根據 DeepFM 論文的外部來源應用一般的 DeepFM 交互:https://arxiv.org/pdf/1703.04247.pdf
預期輸出維度為 dense_features、D 的串聯。
- 參數::
fm_in_features (int) – DeepFM 中 dense_module 的輸入維度。例如,如果輸入嵌入為 [randn(3, 2, 3), randn(3, 4, 5)],則 fm_in_features 應為:2 * 3 + 4 * 5。
sparse_feature_names (List[str]) – F 的長度。
deep_fm_dimension (int) – DeepFM 架構中深度交互 (DI) 的輸出。
範例
D = 3 B = 10 keys = ["f1", "f2"] F = len(keys) fm_inter_arch = FMInteractionArch(sparse_feature_names=keys) dense_features = torch.rand((B, D)) sparse_features = KeyedTensor( keys=keys, length_per_key=[D, D], values=torch.rand((B, D * F)), ) cat_fm_output = fm_inter_arch(dense_features, sparse_features)
- forward(dense_features: Tensor, sparse_features: KeyedTensor) Tensor ¶
- 參數::
dense_features (torch.Tensor) – 大小為 B X D 的張量。
sparse_features (KeyedJaggedTensor) – 大小為 F * D X B 的 KJT。
- 傳回::
大小為 B X (D + DI + 1) 的輸出張量。
- 傳回類型::
torch.Tensor
- training: bool¶
- 類別 torchrec.models.deepfm.OverArch(in_features: int)¶
基底:
Module
最後的架構 - 簡單的 MLP。輸出只有一個目標。
- 參數::
in_features (int) – 交互架構的輸出維度。
範例
B = 20 over_arch = OverArch() logits = over_arch(torch.rand((B, 10)))
- forward(features: Tensor) Tensor ¶
- 參數::
features (torch.Tensor) –
- 傳回::
大小為 B X 1 的輸出張量。
- 傳回類型::
torch.Tensor
- training: bool¶
- 類別 torchrec.models.deepfm.SimpleDeepFMNN(num_dense_features: int, embedding_bag_collection: EmbeddingBagCollection, hidden_layer_size: int, deep_fm_dimension: int)¶
基底:
Module
具有 DeepFM 架構的基本推薦系統模組。透過學習每個特徵的池化嵌入來處理稀疏特徵。透過將密集特徵投影到相同的嵌入空間中來學習密集特徵和稀疏特徵之間的關係。透過本論文中提出的 deep_fm 學習這些密集特徵和稀疏特徵之間的交互:https://arxiv.org/pdf/1703.04247.pdf
該模組假設所有稀疏特徵都具有相同的嵌入維度(即,每個 EmbeddingBagConfig 使用相同的 embedding_dim)
以下符號在整個模型文件中使用
F:稀疏特徵的數量
D:稀疏特徵的 embedding_dimension
B:批次大小
num_features:密集特徵的數量
- 參數::
num_dense_features (int) – 輸入密集特徵的數量。
embedding_bag_collection (EmbeddingBagCollection) – 用於定義 SparseArch 的嵌入袋集合。
hidden_layer_size (int) – 密集模組中使用的隱藏層大小。
deep_fm_dimension (int) – deep_fm 的深度交互模組中使用的輸出層大小。
範例
B = 2 D = 8 eb1_config = EmbeddingBagConfig( name="t1", embedding_dim=D, num_embeddings=100, feature_names=["f1", "f3"] ) eb2_config = EmbeddingBagConfig( name="t2", embedding_dim=D, num_embeddings=100, feature_names=["f2"], ) ebc = EmbeddingBagCollection(tables=[eb1_config, eb2_config]) sparse_nn = SimpleDeepFMNN( embedding_bag_collection=ebc, hidden_layer_size=20, over_embedding_dim=5 ) features = torch.rand((B, 100)) # 0 1 # 0 [1,2] [4,5] # 1 [4,3] [2,9] # ^ # feature sparse_features = KeyedJaggedTensor.from_offsets_sync( keys=["f1", "f3"], values=torch.tensor([1, 2, 4, 5, 4, 3, 2, 9]), offsets=torch.tensor([0, 2, 4, 6, 8]), ) logits = sparse_nn( dense_features=features, sparse_features=sparse_features, )
- forward(dense_features: Tensor, sparse_features: KeyedJaggedTensor) Tensor ¶
- 參數::
dense_features (torch.Tensor) – 密集特徵。
sparse_features (KeyedJaggedTensor) – 稀疏特徵。
- 傳回::
大小為 B X 1 的 logits。
- 傳回類型::
torch.Tensor
- training: bool¶
- class torchrec.models.deepfm.SparseArch(embedding_bag_collection: EmbeddingBagCollection)¶
基底:
Module
處理 DeepFMNN 模型的稀疏特徵。對每個集合的所有 EmbeddingBag 和嵌入特徵進行嵌入查找。
- 參數::
embedding_bag_collection (EmbeddingBagCollection) – 表示一個池化嵌入的集合。
範例
eb1_config = EmbeddingBagConfig( name="t1", embedding_dim=3, num_embeddings=10, feature_names=["f1"] ) eb2_config = EmbeddingBagConfig( name="t2", embedding_dim=4, num_embeddings=10, feature_names=["f2"] ) ebc = EmbeddingBagCollection(tables=[eb1_config, eb2_config]) # 0 1 2 <-- batch # 0 [0,1] None [2] # 1 [3] [4] [5,6,7] # ^ # feature features = KeyedJaggedTensor.from_offsets_sync( keys=["f1", "f2"], values=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7]), offsets=torch.tensor([0, 2, 2, 3, 4, 5, 8]), ) sparse_arch(features)
- forward(features: KeyedJaggedTensor) KeyedTensor ¶
- 參數::
features (KeyedJaggedTensor) –
- 傳回::
大小為 F * D X B 的輸出 KJT。
- 傳回類型::
- training: bool¶