python - 模型输出在不同平台上略有不同是否正常?
问题描述
我正在使用 Huggingface 为文本生成 bert 嵌入,但在我的 Mac 和 Linux 平台上,它们对于相同的文本略有不同。例如,一对结果:
苹果电脑
[0.9832047820091248, 0.8559693694114685, 0.9794790744781494, 0.8379054069519043, 0.2829965651035309, 0.16260339319705963, -0.6263850927352905, -0.6327080130577087, 0.255816787481308, -0.993371307849884, 0.9957219958305359, 0.9640689492225647,
...
0.8542048931121826, -0.3760552406311035, 0.8242940902709961, 0.7799679636955261, -0.9965563416481018, -0.44975095987319946, -0.7121481895446777, 0.9866052269935608]
Ubuntu:
[0.98320472240448, 0.8559693694114685, 0.9794790148735046, 0.8379054069519043, 0.2829962372779846, 0.16260334849357605, -0.6263852715492249, -0.6327071189880371, 0.25581657886505127, -0.993371307849884, 0.9957219958305359, 0.9640689492225647,
...
-0.8621405363082886, -0.9272670745849609, 0.8125085234642029, 0.6777691841125488, 0.8542047739028931, -0.3760542571544647, 0.8242939114570618, 0.7799678444862366, -0.9965563416481018, -0.4497515857219696, -0.7121477127075195, 0.986605167388916]
如您所见,这些数字几乎相同,但每个浮点数的最后几位数字不同。在 Linux 上它使用多个 GPU,在 Mac 上它使用 CPU。
代码如下所示:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained("hfl/chinese-roberta-wwm-ext", model_max_length=128)
model = BertModel.from_pretrained("hfl/chinese-roberta-wwm-ext")
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = torch.nn.DataParallel(model)
#model = torch.nn.DataParallel(model, device_ids=[0, 1])
model = model.to(device)
inputs = tokenizer(names, truncation=True, padding=True, return_tensors="pt").to(device)
outputs = model(**inputs)
embeddings = outputs[1].cpu().detach().numpy().tolist()
对于那些小的差异,它不应该影响相似度计算吗?我问这个的原因是我在 Mac 和 Linux 上基于 Bert 嵌入得到了不同的相似性性能,但数据集并不完全相同。一个是另一个的子集。因此,差异可能是由于数据集本身或不同的嵌入向量造成的。
那么在不同平台(包括 CPU 和 GPU)上生成时,那些小的嵌入向量差异是正常的吗?
解决方案
推荐阅读
- swift - 通过 Segue 将对象从表视图单元传递到另一个视图控制器
- node.js - 无法从 Gitlab CI/CD 在 AWS RDS 数据库中运行迁移
- eclipse - Maven API 在服务器(Tomcat 7)上部署时不响应
- excel - 如何在另一列的一列中查找值并返回找到值的行中的单元格
- c# - 从 edmx 模型调用实体框架创建的实体时出错
- ios - ios 构建大小在设备上更大
- amazon-web-services - 如何将 kinesis 视频流存储到 S3 存储桶中?
- javascript - 为什么在将数组内容记录到控制台后未定义 javascript 数组对象?
- react-native - 我的项目没有在 android 模拟器上运行,但可以在 iphone 上运行
- java - KeyListener is firering wrong Events