Skip to content

Commit 92bf29a

Browse files
authored
Merge pull request #259 from 1985312383/main
Update features.md
2 parents ac29d97 + 44a7a95 commit 92bf29a

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

docs/en/core/features.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,38 @@ sequence_feature = SequenceFeature(
5555

5656
Parameters: `name`, `vocab_size`, `embed_dim` (auto if None), `pooling` (mean/sum/concat), `shared_with`, `padding_idx`, `initializer`.
5757

58+
## Feature Instances and Embedding Ownership
59+
60+
> **Warning**: `SparseFeature` and `SequenceFeature` cache the `nn.Embedding` created by `get_embedding_layer()`. If the **same Feature instance** is passed to multiple models, those models use the same embedding parameters. Training or loading weights into one model therefore changes the embedding observed by the others.
61+
62+
The two EmbeddingLayer instances below unintentionally share the `city` embedding:
63+
64+
```python
65+
from torch_rechub.basic.features import SparseFeature
66+
from torch_rechub.basic.layers import EmbeddingLayer
67+
68+
features = [SparseFeature(name="city", vocab_size=100, embed_dim=16)]
69+
70+
embedding_a = EmbeddingLayer(features)
71+
embedding_b = EmbeddingLayer(features)
72+
73+
assert embedding_a.embed_dict["city"] is embedding_b.embed_dict["city"]
74+
```
75+
76+
For independent model comparisons, cross-validation, or ensemble training, create new Feature instances for every model:
77+
78+
```python
79+
def build_features():
80+
return [SparseFeature(name="city", vocab_size=100, embed_dim=16)]
81+
82+
embedding_a = EmbeddingLayer(build_features())
83+
embedding_b = EmbeddingLayer(build_features())
84+
85+
assert embedding_a.embed_dict["city"] is not embedding_b.embed_dict["city"]
86+
```
87+
88+
Copying only the list does not help: `features.copy()` still contains the original Feature instances. This accidental cross-model sharing is different from explicitly using `shared_with` to share an embedding inside one model; the latter is intentional.
89+
5890
## Usage Example
5991

6092
```python

docs/zh/core/features.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ sequence_feature = SequenceFeature(
7373
- `padding_idx`:填充索引,在InputMask层中会被掩码为0
7474
- `initializer`:嵌入层权重初始化器
7575

76+
## Feature 实例与 Embedding 所有权
77+
78+
> **注意**`SparseFeature``SequenceFeature` 会缓存通过 `get_embedding_layer()` 创建的 `nn.Embedding`。如果将**同一个 Feature 实例**传给多个模型,这些模型会使用同一份 Embedding 参数;训练或加载其中一个模型的权重时,其他模型看到的 Embedding 也会随之变化。
79+
80+
下面的两个 EmbeddingLayer 会意外共享 `city` 的 Embedding:
81+
82+
```python
83+
from torch_rechub.basic.features import SparseFeature
84+
from torch_rechub.basic.layers import EmbeddingLayer
85+
86+
features = [SparseFeature(name="city", vocab_size=100, embed_dim=16)]
87+
88+
embedding_a = EmbeddingLayer(features)
89+
embedding_b = EmbeddingLayer(features)
90+
91+
assert embedding_a.embed_dict["city"] is embedding_b.embed_dict["city"]
92+
```
93+
94+
进行独立的模型对比、交叉验证或集成训练时,请为每个模型重新创建 Feature 实例:
95+
96+
```python
97+
def build_features():
98+
return [SparseFeature(name="city", vocab_size=100, embed_dim=16)]
99+
100+
embedding_a = EmbeddingLayer(build_features())
101+
embedding_b = EmbeddingLayer(build_features())
102+
103+
assert embedding_a.embed_dict["city"] is not embedding_b.embed_dict["city"]
104+
```
105+
106+
仅复制列表并不能解决该问题:`features.copy()` 仍然包含原来的 Feature 实例。这里所说的跨模型意外共享,也不同于在同一个模型内使用 `shared_with` 显式共享 Embedding;后者是预期行为。
107+
76108
## 特征使用示例
77109

78110
```python

0 commit comments

Comments
 (0)