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))
|
|
|