Sequential मॉडलों को समझना

Keras के साथ Machine Translation

Thushan Ganegedara

Data Scientist and Author

Time series इनपुट और sequential मॉडल

  • एक वाक्य एक time series इनपुट है
    • वर्तमान शब्द पर पिछले शब्दों का असर होता है
    • जैसे: He went to the pool for a ....
  • Encoder/decoder एक machine learning मॉडल इस्तेमाल करता है
    • ऐसे मॉडल जो time-series इनपुट से सीखते हैं
    • इन्हें sequential models कहा जाता है
Keras के साथ Machine Translation

Sequential मॉडल

  • Sequential मॉडल
    • इनपुट पर चलते हुए हर time step पर आउटपुट देता है

Sequential मॉडल आर्किटेक्चर

Keras के साथ Machine Translation

Encoder एक sequential मॉडल के रूप में

  • GRU - Gated Recurrent Unit

Gated recurrent units

Keras के साथ Machine Translation

GRU लेयर परिचय

Time step 1 पर, GRU लेयर,

  • इनपुट "We" लेती है
  • शुरुआती state (0,0) लेती है
  • नई state (0.8, 0.3) आउटपुट करती है

GRU 1

Keras के साथ Machine Translation

GRU लेयर परिचय

Time step 2 पर, GRU लेयर,

  • इनपुट "like" लेती है
  • शुरुआती state (0.8,0.3) लेती है
  • नई state (0.5, 0.9) आउटपुट करती है

Hidden state मॉडल की "memory" दर्शाता है

GRU 2

Keras के साथ Machine Translation

Keras (Functional API) ताज़ा-झलक

  • Keras में दो महत्वपूर्ण ऑब्जेक्ट होते हैं: Layer और Model.
  • Input लेयर
    • inp = keras.layers.Input(shape=(...))
  • Hidden लेयर
    • layer = keras.layers.GRU(...)
  • आउटपुट
    • out = layer(inp)
  • मॉडल
    • model = Model(inputs=inp, outputs=out)
Keras के साथ Machine Translation

डेटा के shape को समझना

  • Sequential डेटा 3-डायमेंशनल होता है
    • Batch डायमेंशन (जैसे batch = वाक्यों के समूह)
    • Time डायमेंशन - sequence length
    • Input डायमेंशन (जैसे onehot वेक्टर की लंबाई)
  • GRU मॉडल का इनपुट shape
    • (Batch, Time, Input)
    • (batch size, sequence length, onehot length)

इनपुट डेटा

Keras के साथ Machine Translation

Keras के साथ GRU लागू करना

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)
Keras के साथ Machine Translation

Keras के साथ GRU लागू करना

Keras मॉडल से prediction करना

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 ]]
Keras के साथ Machine Translation

Keras के साथ GRU लागू करना

ऐसा GRU जो बैच में मनचाही संख्या के सैंपल ले सके

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]]
Keras के साथ Machine Translation

GRU लेयर का return_state आर्ग्युमेंट

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

Keras के साथ Machine Translation

GRU लेयर का return_sequences आर्ग्युमेंट

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

Keras के साथ Machine Translation

अभ्यास करते हैं!

Keras के साथ Machine Translation

Preparing Video For Download...