본문 바로가기
기계학습/자연어 처리 머신러닝

글자레벨 번역기 코드 - 딥러닝을 이용한 자연어처리 입문

by tryotto 2020. 1. 31.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
 
from google.colab import drive
drive.mount('/content/drive')
 
import pandas as pd
lines= pd.read_csv('/content/drive/My Drive/eng_fra/fra.txt', names=['src''tar','attribute'], sep='\t')
len(lines)
 
lines = lines[0:60000# 6만개만 저장
lines.sample(10)
 
lines.tar = lines.tar.apply(lambda x : '\t '+ x + ' \n')
lines.sample(10)
 
# 글자 집합 구축
src_vocab=set()
for line in lines.src: # 1줄씩 읽음
    for char in line: # 1개의 글자씩 읽음
        src_vocab.add(char)
 
tar_vocab=set()
for line in lines.tar:
    for char in line:
        tar_vocab.add(char)
 
src_vocab_size = len(src_vocab)+1
tar_vocab_size = len(tar_vocab)+1
print(src_vocab_size)
print(tar_vocab_size)
 
src_vocab = sorted(list(src_vocab))
tar_vocab = sorted(list(tar_vocab))
print(src_vocab[45:75])
print(tar_vocab[45:75])
 
src_to_index = dict([(word, i+1for i, word in enumerate(src_vocab)])
tar_to_index = dict([(word, i+1for i, word in enumerate(tar_vocab)])
print(src_to_index)
print(tar_to_index)
 
encoder_input = []
print_input = []
for line in lines.src: #입력 데이터에서 1줄씩 문장을 읽음
    temp_X = []
    printW = []
    for w in line: #각 줄에서 1개씩 글자를 읽음
      temp_X.append(src_to_index[w]) # 글자를 해당되는 정수로 변환
      printW.append(w)
 
    print_input.append(printW)
    encoder_input.append(temp_X)
 
print(print_input[100:105])
print(encoder_input[100:105])
 
decoder_input = []
tar_print = []
for line in lines.tar:
    temp_X = []
    tar_tmp = []
 
    for w in line:
      temp_X.append(tar_to_index[w])
      tar_tmp.append(w)
    
    tar_print.append(tar_tmp)
    decoder_input.append(temp_X)
 
print(tar_print[:5])
print(decoder_input[:5])
 
decoder_target = []
tar_print = []
for line in lines.tar:
    temp_X = []
    tar_tmp = []
 
    t=0
    for w in line:
      if t>0:
        temp_X.append(tar_to_index[w])
        tar_tmp.append(w)
      t=t+1
 
    tar_print.append(tar_tmp)
    decoder_target.append(temp_X)
 
print(tar_print[:5])
print(decoder_target[:5])
 
max_src_len = max([len(line) for line in lines.src])
max_tar_len = max([len(line) for line in lines.tar])
print(max_src_len)
print(max_tar_len)
 
from tensorflow.keras.preprocessing.sequence import pad_sequences
encoder_input = pad_sequences(encoder_input, maxlen=max_src_len, padding='post')
decoder_input = pad_sequences(decoder_input, maxlen=max_tar_len, padding='post')
decoder_target = pad_sequences(decoder_target, maxlen=max_tar_len, padding='post')
 
print(encoder_input[:3])
 
from tensorflow.keras.utils import to_categorical
encoder_input = to_categorical(encoder_input)
decoder_input = to_categorical(decoder_input)
decoder_target = to_categorical(decoder_target)
 
print(encoder_input[:1])
 
from tensorflow.keras.layers import Input, LSTM, Embedding, Dense
from tensorflow.keras.models import Model
 
encoder_inputs = Input(shape=(None, src_vocab_size))
encoder_lstm = LSTM(units=256, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_inputs)
# encoder_outputs도 같이 리턴받기는 했지만 여기서는 필요없으므로 이 값은 버림.
encoder_states = [state_h, state_c]
# LSTM은 바닐라 RNN과는 달리 상태가 두 개. 바로 은닉 상태와 셀 상태.
 
print(state_c)
print(state_h)
print(encoder_outputs)
print(encoder_states)
 
decoder_inputs = Input(shape=(None, tar_vocab_size))
decoder_lstm = LSTM(units=256, return_sequences=True, return_state=True)
decoder_outputs, _, _= decoder_lstm(decoder_inputs, initial_state=encoder_states)
# 디코더의 첫 상태를 인코더의 은닉 상태, 셀 상태로 합니다.
decoder_softmax_layer = Dense(tar_vocab_size, activation='softmax')
decoder_outputs = decoder_softmax_layer(decoder_outputs)
 
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(optimizer="rmsprop", loss="categorical_crossentropy")
 
model.fit(x=[encoder_input, decoder_input], y=decoder_target, batch_size=64, epochs=50, validation_split=0.2)
 
encoder_model = Model(inputs=encoder_inputs, outputs=encoder_states)
 
decoder_state_input_h = Input(shape=(256,))
decoder_state_input_c = Input(shape=(256,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
# 문장의 다음 단어를 예측하기 위해서 초기 상태를 이전 상태로 사용
decoder_states = [state_h, state_c]
# 이번에는 훈련 과정에서와 달리 은닉 상태와 셀 상태인 state_h와 state_c를 버리지 않음.
decoder_outputs = decoder_softmax_layer(decoder_outputs)
decoder_model = Model(inputs=[decoder_inputs] + decoder_states_inputs, outputs=[decoder_outputs] + decoder_states)
index_to_src = dict(
    (i, char) for char, i in src_to_index.items())
index_to_tar = dict(
    (i, char) for char, i in tar_to_index.items())
 
def decode_sequence(input_seq):
    # 입력으로부터 인코더의 상태를 얻음
    states_value = encoder_model.predict(input_seq)
    # <SOS>에 해당하는 원-핫 벡터 생성
    target_seq = np.zeros((11, tar_vocab_size))
    target_seq[00, tar_to_index['\t']] = 1.
 
    stop_condition = False
    decoded_sentence = ""
    while not stop_condition: #stop_condition이 True가 될 때까지 루프 반복
        output_tokens, h, c = decoder_model.predict([target_seq] + states_value)
        sampled_token_index = np.argmax(output_tokens[0-1, :])
        sampled_char = index_to_tar[sampled_token_index]
        decoded_sentence += sampled_char
 
        # <sos>에 도달하거나 최대 길이를 넘으면 중단.
        if (sampled_char == '\n' or
           len(decoded_sentence) > max_tar_len):
            stop_condition = True
 
        # 길이가 1인 타겟 시퀀스를 업데이트 합니다.
        target_seq = np.zeros((11, tar_vocab_size))
        target_seq[00, sampled_token_index] = 1.
 
        # 상태를 업데이트 합니다.
        states_value = [h, c]
 
    return decoded_sentence
import numpy as np
 
for seq_index in [3,50,100,300,1001]: # 입력 문장의 인덱스
    input_seq = encoder_input[seq_index: seq_index + 1]
    decoded_sentence = decode_sequence(input_seq)
    print(35 * "-")
    print('입력 문장:', lines.src[seq_index])
    print('정답 문장:', lines.tar[seq_index][1:len(lines.tar[seq_index])-1]) # '\t'와 '\n'을 빼고 출력
    print('번역기가 번역한 문장:', decoded_sentence[:len(decoded_sentence)-1]) # '\n'을 빼고 출력
 
 
cs