Spaces:
Running on Zero

Ruurd commited on
Commit
6034d83
·
1 Parent(s): cfffc32

Smooth confidence guided noising

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -82,9 +82,13 @@ def confidence_guided_noising(input_ids, answer_start, confidences, threshold, e
82
  if num_to_noise == 0:
83
  return noised
84
 
85
- # Use 1 - confidence as sampling weights
86
- weights = 1.0 - np.array(confidences[answer_start:])
87
- weights /= weights.sum()
 
 
 
 
88
 
89
  indices = rng.choice(
90
  np.arange(answer_start, len(input_ids)),
@@ -104,6 +108,7 @@ def confidence_guided_noising(input_ids, answer_start, confidences, threshold, e
104
  return noised
105
 
106
 
 
107
  @spaces.GPU
108
  def generate_diffusion_text(input_ids, answer_start):
109
  with torch.no_grad():
 
82
  if num_to_noise == 0:
83
  return noised
84
 
85
+ # Avoid zero-probability weights
86
+ raw_weights = 1.0 - np.array(confidences[answer_start:])
87
+ raw_weights = np.clip(raw_weights, 1e-6, None) # prevent exact 0s
88
+ weights = raw_weights / raw_weights.sum()
89
+
90
+ if num_to_noise > len(weights):
91
+ num_to_noise = len(weights) # safety: can’t sample more than available
92
 
93
  indices = rng.choice(
94
  np.arange(answer_start, len(input_ids)),
 
108
  return noised
109
 
110
 
111
+
112
  @spaces.GPU
113
  def generate_diffusion_text(input_ids, answer_start):
114
  with torch.no_grad():