การกำหนด decoder

Machine Translation ด้วย Keras

Thushan Ganegedara

Data Scientist and Author

โมเดล encoder-decoder

  • Encoder รับคำภาษาอังกฤษทีละคำ
  • สุดท้ายสร้าง context vector
  • Decoder รับ context vector เป็น initial state
  • Decoder สร้างคำภาษาฝรั่งเศสทีละคำ

โมเดล encoder decoder

Machine Translation ด้วย Keras

Input ของ decoder

  • Decoder สร้างด้วย Keras GRU layer
  • โมเดล GRU ต้องการ input 2 ส่วน
    • input แบบ time-series (???)
    • hidden state

โมเดล encoder decoder

Machine Translation ด้วย Keras

Input ของ decoder

ทำซ้ำ context vector จาก encoder จำนวน N ครั้ง

  • หากต้องการสร้างประโยคภาษาฝรั่งเศส 10 คำ ให้ทำซ้ำ context vector 10 ครั้ง

Encoder decoder model repeat vector

Machine Translation ด้วย Keras

ทำความเข้าใจ RepeatVector layer

RepeatVector layer:

  • รับ argument เดียวที่กำหนดความยาว sequence ของ output
  • รับ input ขนาด (batch_size, input size) (เช่น input ขนาด 2 x 3)
  • ส่งออกข้อมูลขนาด (batch_size, sequence length, input size) (เช่น output ขนาด 2 x 3 x 3)

การทำงานของ repeat vector

Machine Translation ด้วย Keras

การกำหนด RepeatVector layer

from tensorflow.keras.layers import RepeatVector
rep = RepeatVector(5)
r_inp = Input(shape=(3,))
r_out = rep(r_inp)
repeat_model = Model(inputs=r_inp, outputs=r_out)
  • สังเกตว่าสองแบบต่อไปนี้ให้ผลเหมือนกัน
rep = RepeatVector(5)
r_out = rep(r_inp)
r_out = RepeatVector(5)(r_inp)
Machine Translation ด้วย Keras

การพยากรณ์ด้วยโมเดล

การพยากรณ์ด้วยโมเดล

x = np.array([[0,1,2],[3,4,5]])
y = repeat_model.predict(x)
print('x.shape = ',x.shape,'\ny.shape = ',y.shape)
x.shape =  (2, 3) 
y.shape =  (2, 5, 3)
Machine Translation ด้วย Keras

การ implement decoder

การกำหนด decoder

de_inputs = RepeatVector(fr_len)(en_state)
decoder_gru = GRU(hsize, return_sequences=True)

กำหนด initial state ของ decoder

gru_outputs = decoder_gru(de_inputs, initial_state=en_state)
Machine Translation ด้วย Keras

การกำหนดโมเดล

enc_dec = Model(inputs=en_inputs, outputs=gru_outputs)
Machine Translation ด้วย Keras

มาฝึกกันเถอะ!

Machine Translation ด้วย Keras

Preparing Video For Download...