Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions keras_nlp/samplers/contrastive_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def __init__(
k=5,
alpha=0.6,
seed=None,
**kwargs,
):
super().__init__()
super().__init__(**kwargs)
self.k = k
self.alpha = alpha
self.seed = seed
Expand Down Expand Up @@ -133,7 +134,7 @@ def cond(prompt, cache, index, logits, hidden_states):

def body(prompt, cache, index, logits, hidden_states):
# Compute the softmax distribution for the next token.
probabilities = keras.activations.softmax(logits)
probabilities = keras.activations.softmax(logits / self.temperature)

# Replicate for `self.k` times to find the best token in top-k
# candidates.
Expand Down
4 changes: 2 additions & 2 deletions keras_nlp/samplers/contrastive_sampler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for Top-K sampler."""
"""Tests for Contrastive Sampler."""

import tensorflow as tf
from absl.testing import parameterized
Expand Down Expand Up @@ -45,7 +45,7 @@ def next(prompt, cache, index):
return logits, hidden_states, cache

self.next = next
self.sampler = ContrastiveSampler(k=5, alpha=0.2)
self.sampler = ContrastiveSampler(k=5, alpha=0.2, temperature=1.0)

def join_as_string(self, x):
return ["".join([self.int_lookup[i] for i in s]) for s in x.numpy()]
Expand Down