Yuchan commited on
Commit
26b3b8c
ยท
verified ยท
1 Parent(s): 5e06e87

Update Inference.py

Browse files
Files changed (1) hide show
  1. 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 = 150 # ๊ธฐ์กด ์ฝ”๋“œ์—์„œ 200์œผ๋กœ ์„ค์ •๋จ
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/chat_model.weights.h5')
196
  print("๋ชจ๋ธ ๊ฐ€์ค‘์น˜ ๋กœ๋“œ ์™„๋ฃŒ!")
197
  # =======================
198
  # 6) ์ถ”๋ก  ํ•จ์ˆ˜ (๊ธฐ์กด ์ฝ”๋“œ ์œ ์ง€)
199
- # =======================
 
 
 
200
 
201
- def generate_text_topp(model, prompt, max_len=150, max_gen=100, p=0.9, temperature=0.8, min_len=20):
202
- # ์ธ์ฝ”๋” ์ž…๋ ฅ์€ <start> Prompt <sep> ๋งŒ ์‚ฌ์šฉ
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
- current_len = len(generated)
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
- # ๋ชจ๋ธ์ด 1 epoch๋งŒ ํ•™์Šต๋˜์—ˆ์œผ๋ฏ€๋กœ ์˜๋ฏธ ์žˆ๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
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))