njs03332 / ml_study

3 stars 0 forks source link

2024/01/05 ~ 2024/02/28 #79

Open njs03332 opened 10 months ago

njs03332 commented 10 months ago
njs03332 commented 10 months ago

assign roles -s 0104 -c 1 2 3

njs03332 commented 10 months ago
0 1 2
member 주선미 한단비 김유리
chapter 1 2 3
njs03332 commented 9 months ago

16.3.2 빔 검색

beam_width = 10
# 모든 (10개) 디코더 셀을 감싼 BeamSearchDecoder를 만듦
decoder = tfa.seq2seq.beam_search_decoder.BeamSearchDecoder(cell=decoder_cell, beam_width=beam_width, output_layer=output_layer)
# 각 디코더를 위해 인코더의 마지막 상태를 복사
decoder_initial_state = tfa.seq2seq.beam_search_decoder.tile_batch(encoder_state, multiplier=beam_width)
# 시작과 종료 토큰과 함께 이 상태를 디코더에게 전달
outputs, _, _ = decoder(embedding_decoder, start_tokens=start_tokens, end_token=end_token, initial_state=decoder_initial_state)
givitallugot commented 9 months ago

16.3 신경망 기계 번역을 위한 인코더-디코더 네트워크


기본적인 인코더-디코더 모델을 만드는 코드

import tensorflow_addons as tfa

encoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
decoder_inputs = keras.layers.Input(shape=[None], dtype=np.int32)
sequence_lengths = keras.layers.Input(shape=[], dtype=np.int32)

embeddings = keras.layers.Embedding(vocab_size, embed_size)
encoder_embeddings = embeddings(encoder_inputs)
decoder_embeddings = embeddings(decoder_inputs)

encoder = keras.layers.LSTM(512, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_embeddings)
encoder_state = [state_h, state_c]

sampler = tfa.seq2seq.sampler.TrainingSampler()

decoder_cell = keras.layers.LSTMCell(512)
output_layer = keras.layers.Dense(vocab_size)
decoder = tfa.seq2seq.basic_decoder.BasicDecoder(decoder_cell, sampler,
                                                 output_layer=output_layer)
final_outputs, final_state, final_sequence_lengths = decoder(
    decoder_embeddings, initial_state=encoder_state,
    sequence_length=sequence_lengths)
Y_proba = tf.nn.softmax(final_outputs.rnn_output)

model = keras.models.Model(
    inputs=[encoder_inputs, decoder_inputs, sequence_lengths],
    outputs=[Y_proba])
danbi5228 commented 8 months ago

16.3.1 양방향 RNN