강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

올라라~~님의 프로필 이미지
올라라~~

작성한 질문수

<밑바닥부터 만들면서 배우는 LLM> 완독 챌린지

주간 독서 인증

코드 2-3 출력 결과 비교

해결된 질문

작성

·

9

0

안녕하세요.

코드 2-3의 SimpleTokenizerV1을 실행한 결과에 대해 문의드립니다.

아래 코드를 실행해보니 원문 text와 decode로 복원한 text가 조금 다릅니다.

원문과 복원한 text가 다르면 문제가 있을지 문의드립니다.

class SimpleTokenizerV1:
    def __init__(self, vocab):
        self.str_to_int = vocab
        self.int_to_str = {i:s for s,i in vocab.items()}

    def encode(self, text):
        preprocessed = re.split(r'([,.:;?_!"()\']|--|\s)', text) # 'hello,. world'

        preprocessed = [
            item.strip() for item in preprocessed if item.strip()
        ]
        ids = [self.str_to_int[s] for s in preprocessed]
        return ids

    def decode(self, ids):
        text = " ".join([self.int_to_str[i] for i in ids])
        # 구둣점 문자 앞의 공백을 삭제합니다.
        text = re.sub(r'\s+([,.?!"()\'])', r'\1', text)
        return text

tokenizer = SimpleTokenizerV1(vocab)

text = """"It's the last he painted, you know," Mrs. Gisburn said with pardonable pride."""
ids = tokenizer.encode(text)
print(text)
print(ids)
print(tokenizer.decode(ids))

 

"It's the last he painted, you know," Mrs. Gisburn said with pardonable pride.
[1, 56, 2, 850, 988, 602, 533, 746, 5, 1126, 596, 5, 1, 67, 7, 38, 851, 1108, 754, 793, 7]
" It' s the last he painted, you know," Mrs. Gisburn said with pardonable pride.

 

답변 1

1

박해선님의 프로필 이미지
박해선
지식공유자

안녕하세요. 이 토크나이저 클래스는 예시를 위해 간단히 만든 거라 완벽하게 복원되지 않는 것이 정상입니다. 실제 모델을 훈련할 때는 tiktoken 라이브러리를 사용하게 됩니다. 감사합니다!

올라라~~님의 프로필 이미지
올라라~~
질문자

감사합니다!

올라라~~님의 프로필 이미지
올라라~~

작성한 질문수

질문하기