인코더 구현하기

Keras로 배우는 Machine Translation

Thushan Ganegedara

Data Scientist and Author

데이터 이해하기

데이터셋 일부 출력하기

for en_sent, fr_sent in zip(en_text[:3], fr_text[:3]):
  print("English: ", en_sent)
  print("\tFrench: ", fr_sent)
English:  new jersey is sometimes quiet during autumn , and it is snowy in april .
    French:  new jersey est parfois calme pendant l' automne , et il est neigeux en avril .

English:  the united states is usually chilly during july , and it is usually freezing in november .
    French:  les états-unis est généralement froid en juillet , et il gèle habituellement en novembre .

English:  california is usually quiet during march , and it is usually hot in june .
    French:  california est généralement calme en mars , et il est généralement chaud en juin .
Keras로 배우는 Machine Translation

문장 토크나이징

토크나이징

  • 문장/구를 개별 토큰(예: 단어)으로 나누는 과정

문장 내 단어 토크나이징

first_sent = en_text[0]
print("First sentence: ", first_sent)
first_words = first_sent.split(" ")
print("\tWords: ", first_words)
First sentence:  new jersey is sometimes quiet during autumn , and it is snowy in april .
    Words:  ['new', 'jersey', 'is', 'sometimes', 'quiet', 'during', 'autumn', ',', 
             'and', 'it', 'is', 'snowy', 'in', 'april', '.']
Keras로 배우는 Machine Translation

문장 길이 계산하기

문장 평균 길이와 어휘 크기 계산(영어)

sent_lengths = [len(en_sent.split(" ")) for en_sent in en_text]
mean_length = np.mean(sent_lengths)
print('(English) Mean sentence length: ', mean_length)
(English) Mean sentence length:  13.20662
Keras로 배우는 Machine Translation

어휘 크기 계산하기

all_words = []
for sent in en_text:
    all_words.extend(sent.split(" "))
vocab_size = len(set(all_words))
print("(English) Vocabulary size: ", vocab_size)
  • set 객체는 고유 항목만 포함하며 중복이 없습니다
(English) Vocabulary size:  228
Keras로 배우는 Machine Translation

인코더

인코더

Keras로 배우는 Machine Translation

Keras로 인코더 구현하기

  • 입력 레이어
    en_inputs = Input(shape=(en_len, en_vocab))
    
  • GRU 레이어
    en_gru = GRU(hsize, return_state=True)
    en_out, en_state = en_gru(en_inputs)
    
  • Keras 모델
    encoder = Model(inputs=en_inputs, outputs=en_state)
    
Keras로 배우는 Machine Translation

Keras 모델 요약 이해하기

print(encoder.summary())
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 15, 150)           0         
_________________________________________________________________
gru (GRU)                    [(None, 48), (None, 48)]  28656     
=================================================================
Total params: 28,656
Trainable params: 28,656
Non-trainable params: 0
_________________________________________________________________
Keras로 배우는 Machine Translation

연습해 봅시다!

Keras로 배우는 Machine Translation

Preparing Video For Download...