10억 개 문장 쌍 학습과 384차원 벡터 매핑 구조
nreimers/MiniLM-L6-H384-uncased를 기반으로 미세 조정된 all-MiniLM-L6-v2는 10억 개의 문장 쌍 데이터셋과 대조 학습 기법을 거쳐 텍스트의 의미를 벡터로 변환한다. 학습 과정에는 구글의 TPU v3-8 7대와 수치 계산 라이브러리인 JAX, 신경망 라이브러리인 Flax가 투입됐다. 이 모델은 입력된 문장이나 단락을 384차원의 밀집 벡터 공간으로 매핑하며, 256개 워드 피스를 초과하는 입력 텍스트는 자동으로 절단 처리된다.
sentence-transformers 라이브러리를 활용하면 모델을 즉시 적용할 수 있다. 설치와 기본 실행 명령어는 다음과 같다.
bash
pip install -U sentence-transformers
python
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings)평균 풀링과 우회 구현 방식
sentence-transformers 없이 HuggingFace의 transformers 라이브러리만 사용하는 환경에서는 출력값에 평균 풀링 작업을 직접 적용해야 한다. 아래는 평균 풀링 함수와 토크나이저, 모델을 결합한 구체적인 구현 코드다.
python
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
sentences = ['This is an example sentence', 'Each sentence is converted']
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)벡터 데이터베이스 환경에서의 실무 선택 기준
384차원이라는 낮은 차원은 수백만 건의 문서 벡터를 저장하는 벡터 데이터베이스 환경에서 저장 비용을 낮추고 검색 속도를 높인다. 고객 센터 FAQ 시스템의 유사 답변 검색, 뉴스 기사 클러스터링, RAG 구조의 리트리버 역할에 활용할 수 있다. 개발자는 대형 언어 모델 중심의 파이프라인에서 1차 후보군을 빠르게 추려내는 경량 인코더가 필요할 때 이 384차원 구조와 토큰 절단 제약을 검토해야 한다.


