ทำความเข้าใจ Sequential Models

Machine Translation ด้วย Keras

Thushan Ganegedara

Data Scientist and Author

ข้อมูลอนุกรมเวลาและ Sequential Models

  • ประโยคคือข้อมูลอนุกรมเวลา
    • คำปัจจุบันได้รับอิทธิพลจากคำก่อนหน้า
    • เช่น He went to the pool for a ....
  • Encoder/decoder ใช้โมเดล machine learning
    • โมเดลที่เรียนรู้จากข้อมูลอนุกรมเวลาได้
    • เรียกว่า sequential models
Machine Translation ด้วย Keras

Sequential Models

  • Sequential models
    • ประมวลผลข้อมูลทีละขั้นและสร้างผลลัพธ์ในแต่ละ time step

สถาปัตยกรรม Sequential model

Machine Translation ด้วย Keras

Encoder ในฐานะ Sequential Model

  • GRU - Gated Recurrent Unit

Gated recurrent units

Machine Translation ด้วย Keras

แนะนำเลเยอร์ GRU

ที่ time step 1 เลเยอร์ GRU

  • รับอินพุต "We"
  • รับ initial state (0,0)
  • ส่งออก state ใหม่ (0.8, 0.3)

GRU 1

Machine Translation ด้วย Keras

แนะนำเลเยอร์ GRU

ที่ time step 2 เลเยอร์ GRU

  • รับอินพุต "like"
  • รับ initial state (0.8,0.3)
  • ส่งออก state ใหม่ (0.5, 0.9)

Hidden state แทน "หน่วยความจำ" ของสิ่งที่โมเดลเคยเห็น

GRU 2

Machine Translation ด้วย Keras

ทบทวน Keras (Functional API)

  • Keras มีออบเจกต์สำคัญ 2 ตัว: Layer และ Model
  • Input layer
    • inp = keras.layers.Input(shape=(...))
  • Hidden layer
    • layer = keras.layers.GRU(...)
  • Output
    • out = layer(inp)
  • Model
    • model = Model(inputs=inp, outputs=out)
Machine Translation ด้วย Keras

ทำความเข้าใจรูปร่างของข้อมูล

  • ข้อมูล sequential มี 3 มิติ
    • มิติ batch (เช่น batch = กลุ่มประโยค)
    • มิติ time - ความยาวลำดับ
    • มิติ input (เช่น ความยาว onehot vector)
  • รูปร่าง input ของโมเดล GRU
    • (Batch, Time, Input)
    • (batch size, sequence length, onehot length)

ข้อมูล Input

Machine Translation ด้วย Keras

การสร้าง GRU ด้วย Keras

กำหนดเลเยอร์ Keras

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out = keras.layers.GRU(10)(inp)

กำหนดโมเดล Keras

model = keras.models.Model(inputs=inp, outputs=gru_out)
Machine Translation ด้วย Keras

การสร้าง GRU ด้วย Keras

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

x = np.random.normal(size=(2,3,4))
y = model.predict(x)
print("shape (y) =", y.shape, "\ny = \n", y)
shape (y) = (2, 10) 
y = 
[[ 0.2576233   0.01215531  ... -0.32517594  0.4483121 ],
 [ 0.54189587 -0.63834655  ... -0.4339783   0.4043917 ]]
Machine Translation ด้วย Keras

การสร้าง GRU ด้วย Keras

GRU ที่รับจำนวนตัวอย่างในแต่ละ batch ได้ไม่จำกัด

inp = keras.layers.Input(shape=(3,4))
gru_out = keras.layers.GRU(10)(inp)
model = keras.models.Model(inputs=inp, outputs=gru_out)
x = np.random.normal(size=(5,3,4))
y = model.predict(x)
print("y = \n", y)
y = 
 [[-1.3941444e-02 -3.3123985e-02 ... 6.5081201e-02  1.1245312e-01]
 [ 1.1409521e-03  3.6983326e-01 ... -3.4610277e-01 -3.4792548e-01]
 [ 2.5911796e-01 -3.9517123e-01 ... 5.8505309e-01  3.6908010e-01]
 [-2.8727052e-01 -5.1150680e-02 ... -1.9637148e-01 -1.5587148e-01]
 [ 3.1303680e-01  2.3338445e-01 ... 9.1499090e-04 -2.0590121e-01]]
Machine Translation ด้วย Keras

อาร์กิวเมนต์ return_state ของเลเยอร์ GRU

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out2, gru_state = keras.layers.GRU(10, return_state=True)(inp)
print("gru_out2.shape = ", gru_out2.shape)
print("gru_state.shape = ", gru_state.shape)
gru_out2.shape =  (2, 10)
gru_state.shape =  (2, 10)

GRU return_state

Machine Translation ด้วย Keras

อาร์กิวเมนต์ return_sequences ของเลเยอร์ GRU

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out3 = keras.layers.GRU(10, return_sequences=True)(inp)
print("gru_out3.shape = ", gru_out2.shape)
gru_out3.shape =  (2, 3, 10)

GRU return_sequences

Machine Translation ด้วย Keras

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

Machine Translation ด้วย Keras

Preparing Video For Download...