allenai / allenai/scibert

Is it expected the same sentence gives different features?

Đang mở
#115 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
1.7k
Fork
232
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I'm a bit puzzled by something I encountered trying to encode sentences as embeddings. When I ran the sentences through the model one at a time, I got slightly different results from when I ran batches of sentences.

I've reduced an example down to:
```
from transformers import pipeline
import numpy as np

p = pipeline('feature-extraction', model='allenai/scibert_scivocab_uncased')
s = 'the scurvy dog walked home alone'.split()

for l in range(1,len(s)+1):
txt = ' '.join(s[:l])

res1 = p(txt)
res2 = p(txt)
res1_2 = p([txt, txt])
print(l, txt, len(res1[0]))
print(all( np.allclose(i, j) for i, j in zip(res1[0], res2[0])),
all( np.allclose(i, j) for i, j in zip(res2[0], res1_2[0])),
all( np.allclose(i, j) for i, j in zip(res1_2[0], res1_2[1])))
```
The output I get is:
```
1 the 3
True False True
2 the scurvy 6
True True True
3 the scurvy dog 7
True False False
4 the scurvy dog walked 9
True False True
5 the scurvy dog walked home 10
True True False
6 the scurvy dog walked home alone 11
True True True
```

So running a single sentence through the model seems to give the same output each time, but if I run a batch with the same sentence twice, it's sometimes different (between the two outputs, and compared to the single-sentence case)

Is this expected/explainable?

Further context, I'm running it on CPU (laptop), python 3.8.9, freshly installed venv.
The difference is usually just in a few indices of the embeddings, and can be up to 1e-3. The difference is negligible when comparing the embeddings with cosine distance. But I'd like to understand where it comes from before dismissing it.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.