Update README.md
Browse files
README.md
CHANGED
|
@@ -172,7 +172,29 @@ with torch.no_grad():
|
|
| 172 |
outputs = model(**enc, output_hidden_states=True)
|
| 173 |
|
| 174 |
last_hidden = outputs.hidden_states[-1] # (batch, seq_len, 1024)
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
```
|
| 177 |
|
| 178 |
---
|
|
|
|
| 172 |
outputs = model(**enc, output_hidden_states=True)
|
| 173 |
|
| 174 |
last_hidden = outputs.hidden_states[-1] # (batch, seq_len, 1024)
|
| 175 |
+
|
| 176 |
+
# By construction the TechTokens are always enclosed between the last two [SEP] tokens
|
| 177 |
+
# Find the positions of the last two [SEP] tokens and extract the embeddings for the tokens in between
|
| 178 |
+
|
| 179 |
+
tech_token_embeddings = []
|
| 180 |
+
input_ids = enc['input_ids']
|
| 181 |
+
|
| 182 |
+
sep_token_id = tokenizer.sep_token_id
|
| 183 |
+
|
| 184 |
+
for i, ids in enumerate(input_ids):
|
| 185 |
+
ids = ids.tolist()
|
| 186 |
+
sep_indices = [idx for idx, x in enumerate(ids) if x == sep_token_id]
|
| 187 |
+
# tech tokens are between the last two [SEP]
|
| 188 |
+
if len(sep_indices) < 2:
|
| 189 |
+
tech_token_embeddings.append(torch.empty((0, last_hidden.size(-1))))
|
| 190 |
+
continue
|
| 191 |
+
start = sep_indices[-2] + 1
|
| 192 |
+
end = sep_indices[-1]
|
| 193 |
+
emb = last_hidden[i, start:end, :] # shape: (# tech tokens, hidden_dim)
|
| 194 |
+
tech_token_embeddings.append(emb)
|
| 195 |
+
|
| 196 |
+
# Now tech_token_embeddings is a list, one per sample in batch, containing
|
| 197 |
+
# a tensor of shape (num_tech_tokens, hidden_size)
|
| 198 |
```
|
| 199 |
|
| 200 |
---
|