Capire i modelli sequenziali

Traduzione automatica con Keras

Thushan Ganegedara

Data Scientist and Author

Input temporali e modelli sequenziali

  • Una frase è un input di serie temporale
    • La parola corrente è influenzata dalle precedenti
    • Es. He went to the pool for a ....
  • L'encoder/decoder usa un modello di machine learning
    • Modelli che apprendono da input sequenziali
    • Questi modelli si chiamano modelli sequenziali
Traduzione automatica con Keras

Modelli sequenziali

  • Modelli sequenziali
    • Scorrono l'input producendo un output a ogni passo temporale

Architettura modello sequenziale

Traduzione automatica con Keras

Encoder come modello sequenziale

  • GRU - Gated Recurrent Unit

Unità ricorrenti con gate

Traduzione automatica con Keras

Introduzione al livello GRU

Al passo temporale 1, il livello GRU,

  • Consuma l'input "We"
  • Consuma lo stato iniziale (0,0)
  • Produce il nuovo stato (0.8, 0.3)

GRU 1

Traduzione automatica con Keras

Introduzione al livello GRU

Al passo temporale 2, il livello GRU,

  • Consuma l'input "like"
  • Consuma lo stato iniziale (0.8,0.3)
  • Produce il nuovo stato (0.5, 0.9)

Lo stato nascosto rappresenta la "memoria" di ciò che il modello ha visto

GRU 2

Traduzione automatica con Keras

Ripasso di Keras (Functional API)

  • Keras ha due oggetti importanti: oggetti Layer e 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)
Traduzione automatica con Keras

Capire la forma dei dati

  • I dati sequenziali sono 3D
    • Dimensione batch (es. batch = gruppi di frasi)
    • Dimensione tempo - lunghezza sequenza
    • Dimensione input (es. lunghezza one-hot)
  • Shape input del modello GRU
    • (Batch, Tempo, Input)
    • (dimensione batch, lunghezza sequenza, lunghezza one-hot)

Dati di input

Traduzione automatica con Keras

Implementare le GRU con Keras

Definire i livelli Keras

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

Definire un modello Keras

model = keras.models.Model(inputs=inp, outputs=gru_out)
Traduzione automatica con Keras

Implementare le GRU con Keras

Predire con il modello 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 ]]
Traduzione automatica con Keras

Implementare le GRU con Keras

Una GRU che accetta un numero arbitrario di campioni nel 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]]
Traduzione automatica con Keras

Argomento return_state del livello 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

Traduzione automatica con Keras

Argomento return_sequences del livello 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

Traduzione automatica con Keras

Passiamo alla pratica!

Traduzione automatica con Keras

Preparing Video For Download...