Я пытался реализовать этот фрагмент кода Python в Google Colab. Фрагмент предназначен для определения сходства предложений. Код работает нормально, но я обнаружил, что вложения и расстояния меняются каждый раз, когда я его запускаю, что не идеально для моего предполагаемого варианта использования.
Код: Выделить всё
import torch
from scipy.spatial.distance import cosine
from transformers import AutoModel, AutoTokenizer
# Import our models. The package will take care of downloading the models automatically
tokenizer = AutoTokenizer.from_pretrained("qiyuw/pcl-bert-base-uncased")
model = AutoModel.from_pretrained("qiyuw/pcl-bert-base-uncased")
# Tokenize input texts
texts = [
"There's a kid on a skateboard.",
"A kid is skateboarding.",
"A kid is inside the house."
]
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt")
# Get the embeddings
with torch.no_grad():
embeddings = model(**inputs, output_hidden_states=True, return_dict=True).pooler_output
# Calculate cosine similarities
# Cosine similarities are in [-1, 1]. Higher means more similar
cosine_sim_0_1 = 1 - cosine(embeddings[0], embeddings[1])
cosine_sim_0_2 = 1 - cosine(embeddings[0], embeddings[2])
print("Cosine similarity between \"%s\" and \"%s\" is: %.3f" % (texts[0], texts[1], cosine_sim_0_1))
print("Cosine similarity between \"%s\" and \"%s\" is: %.3f" % (texts[0], texts[2], cosine_sim_0_2))
Код: Выделить всё
Some weights of RobertaModel were not initialized from the model checkpoint at qiyuw/pcl-roberta-large and are newly initialized: ['roberta.pooler.dense.bias', 'roberta.pooler.dense.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
- Могу ли я сделать вывод воспроизводимым, инициализируя/заполнив модель по-другому?
- Если Я не могу сделать выходные данные воспроизводимыми. Есть ли способ повысить точность и уменьшить различия между запусками?
- Есть ли способ поиска таких моделей с обнимающими лицами? который будет инициализировать веса пулера, чтобы я мог найти модель, которая соответствует моим целям?
Подробнее здесь: https://stackoverflow.com/questions/786 ... ransformer