Yuchan
commited on
Update Inference.py
Browse files- Inference.py +12 -51
Inference.py
CHANGED
|
@@ -13,7 +13,7 @@ tf.get_logger().setLevel("ERROR")
|
|
| 13 |
SEED = 42
|
| 14 |
tf.random.set_seed(SEED)
|
| 15 |
np.random.seed(SEED)
|
| 16 |
-
max_len =
|
| 17 |
batch_size = 128
|
| 18 |
|
| 19 |
# TPU 초기화 (기존 코드와 동일)
|
|
@@ -186,83 +186,44 @@ model = ReLM(
|
|
| 186 |
d_model=256,
|
| 187 |
n_layers=1
|
| 188 |
)
|
| 189 |
-
dummy_input = {
|
| 190 |
-
"enc_inputs": tf.zeros((1, max_len), dtype=tf.int32),
|
| 191 |
-
"dec_inputs": tf.zeros((1, max_len), dtype=tf.int32)
|
| 192 |
-
}
|
| 193 |
-
_ = chat_model(dummy_input)
|
| 194 |
|
| 195 |
-
chat_model.load_weights('/kaggle/working/
|
| 196 |
print("모델 가중치 로드 완료!")
|
| 197 |
# =======================
|
| 198 |
# 6) 추론 함수 (기존 코드 유지)
|
| 199 |
-
#
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
def generate_text_topp(model, prompt, max_len=
|
| 202 |
-
|
| 203 |
-
model_input = text_to_ids(f"<start> {prompt} <sep>")
|
| 204 |
model_input = model_input[:max_len]
|
| 205 |
generated = list(model_input)
|
| 206 |
-
|
| 207 |
for step in range(max_gen):
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
# 현재까지 생성된 시퀀스를 입력으로 사용
|
| 211 |
-
if current_len > max_len:
|
| 212 |
input_seq = generated[-max_len:]
|
| 213 |
else:
|
| 214 |
input_seq = generated
|
| 215 |
-
|
| 216 |
-
# 패딩
|
| 217 |
input_padded = np.pad(input_seq, (0, max_len - len(input_seq)), constant_values=pad_id)
|
| 218 |
input_tensor = tf.convert_to_tensor([input_padded])
|
| 219 |
-
|
| 220 |
-
# 모델 추론 (enc_inputs, dec_inputs 모두 동일한 시퀀스를 사용)
|
| 221 |
-
dummy_input = {
|
| 222 |
-
"enc_inputs": input_tensor,
|
| 223 |
-
"dec_inputs": input_tensor
|
| 224 |
-
}
|
| 225 |
-
logits = model(dummy_input, training=False)
|
| 226 |
-
|
| 227 |
-
# 다음 토큰의 로짓은 시퀀스의 마지막 토큰 위치에서 가져옴 (0-based index: current_len - 1)
|
| 228 |
-
# 하지만 패딩 후 input_tensor의 실제 시퀀스 길이는 len(input_seq)
|
| 229 |
next_token_logits = logits[0, len(input_seq) - 1].numpy()
|
| 230 |
-
|
| 231 |
-
# 특수 토큰 생성 억제
|
| 232 |
next_token_logits[end_id] -= 5.0
|
| 233 |
next_token_logits[pad_id] -= 10.0
|
| 234 |
-
|
| 235 |
probs = tf.nn.softmax(next_token_logits / temperature).numpy()
|
| 236 |
sorted_indices = np.argsort(probs)[::-1]
|
| 237 |
sorted_probs = probs[sorted_indices]
|
| 238 |
-
|
| 239 |
-
# Top-p (Nucleus) Sampling
|
| 240 |
cumulative_probs = np.cumsum(sorted_probs)
|
| 241 |
cutoff = np.searchsorted(cumulative_probs, p)
|
| 242 |
top_indices = sorted_indices[:cutoff + 1]
|
| 243 |
top_probs = sorted_probs[:cutoff + 1]
|
| 244 |
top_probs /= np.sum(top_probs)
|
| 245 |
next_token_id = np.random.choice(top_indices, p=top_probs)
|
| 246 |
-
|
| 247 |
if next_token_id == end_id and len(generated) >= min_len:
|
| 248 |
break
|
| 249 |
-
|
| 250 |
generated.append(int(next_token_id))
|
| 251 |
-
|
| 252 |
-
# <start> 토큰 제거 및 <sep> 이전 부분 제거
|
| 253 |
-
try:
|
| 254 |
-
sep_index = generated.index(sep_id)
|
| 255 |
-
# <sep> 이후부터 <end> 이전까지의 응답만 반환
|
| 256 |
-
result_ids = generated[sep_index + 1:]
|
| 257 |
-
try:
|
| 258 |
-
end_index = result_ids.index(end_id)
|
| 259 |
-
result_ids = result_ids[:end_index]
|
| 260 |
-
except ValueError:
|
| 261 |
-
pass
|
| 262 |
-
return ids_to_text(result_ids)
|
| 263 |
-
except ValueError:
|
| 264 |
-
return ids_to_text(generated) # <sep>이 없으면 전체 반환
|
| 265 |
|
| 266 |
print("\n\n===== 생성 결과 =====")
|
| 267 |
-
|
| 268 |
-
print(generate_text_topp(chat_model, "제가 이따가 버스를 타야 해서 준비 좀 해야겠어요. 재미있는 대화였습니다!", p=0.9))
|
|
|
|
| 13 |
SEED = 42
|
| 14 |
tf.random.set_seed(SEED)
|
| 15 |
np.random.seed(SEED)
|
| 16 |
+
max_len = 512 # 기존 코드에서 200으로 설정됨
|
| 17 |
batch_size = 128
|
| 18 |
|
| 19 |
# TPU 초기화 (기존 코드와 동일)
|
|
|
|
| 186 |
d_model=256,
|
| 187 |
n_layers=1
|
| 188 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
chat_model.load_weights('/kaggle/working/model.weights.h5')
|
| 191 |
print("모델 가중치 로드 완료!")
|
| 192 |
# =======================
|
| 193 |
# 6) 추론 함수 (기존 코드 유지)
|
| 194 |
+
# 더미 인풋으로 모델 초기화
|
| 195 |
+
dummy_input = np.zeros((1, max_len), dtype=np.int32)
|
| 196 |
+
_ = model(dummy_input)
|
| 197 |
+
model.summary()
|
| 198 |
|
| 199 |
+
def generate_text_topp(model, prompt, max_len=512, max_gen=512, p=0.9, temperature=0.8, min_len=20):
|
| 200 |
+
model_input = text_to_ids(f"<start> {prompt}")
|
|
|
|
| 201 |
model_input = model_input[:max_len]
|
| 202 |
generated = list(model_input)
|
|
|
|
| 203 |
for step in range(max_gen):
|
| 204 |
+
if len(generated) > max_len:
|
|
|
|
|
|
|
|
|
|
| 205 |
input_seq = generated[-max_len:]
|
| 206 |
else:
|
| 207 |
input_seq = generated
|
|
|
|
|
|
|
| 208 |
input_padded = np.pad(input_seq, (0, max_len - len(input_seq)), constant_values=pad_id)
|
| 209 |
input_tensor = tf.convert_to_tensor([input_padded])
|
| 210 |
+
logits = model(input_tensor, training=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
next_token_logits = logits[0, len(input_seq) - 1].numpy()
|
|
|
|
|
|
|
| 212 |
next_token_logits[end_id] -= 5.0
|
| 213 |
next_token_logits[pad_id] -= 10.0
|
|
|
|
| 214 |
probs = tf.nn.softmax(next_token_logits / temperature).numpy()
|
| 215 |
sorted_indices = np.argsort(probs)[::-1]
|
| 216 |
sorted_probs = probs[sorted_indices]
|
|
|
|
|
|
|
| 217 |
cumulative_probs = np.cumsum(sorted_probs)
|
| 218 |
cutoff = np.searchsorted(cumulative_probs, p)
|
| 219 |
top_indices = sorted_indices[:cutoff + 1]
|
| 220 |
top_probs = sorted_probs[:cutoff + 1]
|
| 221 |
top_probs /= np.sum(top_probs)
|
| 222 |
next_token_id = np.random.choice(top_indices, p=top_probs)
|
|
|
|
| 223 |
if next_token_id == end_id and len(generated) >= min_len:
|
| 224 |
break
|
|
|
|
| 225 |
generated.append(int(next_token_id))
|
| 226 |
+
return ids_to_text(generated)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
print("\n\n===== 생성 결과 =====")
|
| 229 |
+
print(generate_text_topp(model, "지난 2년 동안", p=0.9))
|
|
|