使用 Keras 進行機器翻譯
Thushan Ganegedara
Data Scientist and Author


將編碼器的情境向量重複 N 次

RepeatVector 層:
2 x 3)2 x 5 x 3)
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)
使用模型進行預測
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)
定義解碼器
de_inputs = RepeatVector(fr_len)(en_state)
decoder_gru = GRU(hsize, return_sequences=True)
設定解碼器的初始狀態
gru_outputs = decoder_gru(de_inputs, initial_state=en_state)
enc_dec = Model(inputs=en_inputs, outputs=gru_outputs)
使用 Keras 進行機器翻譯