prompter: Show me an implementation of nucleus sampling written in python

assistant: Here is a basic implementation of nucleus sampling in Python:

```python
import random

def nucleus_sampling(input_text, p=0.9):
    input_words = input_text.split()
    prob = [p if random.random() < p else (1-p)/(len(input_words)-1) for word in input_words]
    filtered_words = [word for word, p in zip(input_words, prob) if random.random() < p]
    output_text = ' '.join(filtered_words)
    return output_text

input_text = "This is an example of nucleus sampling."
output_text = nucleus_sampling(input_text)
print(output_text)
```

In this implementation, the nucleus_sampling function takes an input text as input and applies nucleus sampling to it by assigning each word a probability of being kept. The probability of keeping a word is given by p, which is set to 0.9 by default. If a word is kept, its probability of being kept remains p, otherwise it becomes (1-p)/(len(input_words)-1). The words are then filtered based on their probabilities, and the filtered words are concatenated to form the output text.

assistant: Here's a basic implementation of nucleus sampling in Python:
import random
```python
def nucleus_sampling(items, k):
    """Performs nucleus sampling.

    Selects k items from items with a probability proportional
    to their score (items need to be (item, score) tuples or objects
    with a score attribute).

    items: List of (item, score) tuples or objects with a score attribute
    k: Number of items to select

    Returns:
        List of k selected items
    """
    scores = [item[1] for item in items]
    sumscores = sum(scores)

    chosen = []
    for  in range(k):
        pick = random.uniform(0, sum_scores)
        current = 0
        for item, score in items:
            current += score
            if current > pick:
                chosen.append(item)
                sum_scores -= score
                break
    return chosen
```
For example:
items = [("a", 10), ("b", 30), ("c", 40)]
sampled = nucleus_sampling(items, 2)
print(sampled)
# Might print ["b", "c"]

This selects 2 items (k=2) from the items list with a probability proportional to their scores. So the item with score 30 ("b") and the item with score 40 ("c") have a higher chance of being selected than the item with score 10 ("a").

assistant: Nucleus sampling is a technique used in natural language processing to control the diversity of generated text. It works by filtering out all but the most likely tokens given a certain probability threshold, also known as the "nucleus". Here's an implementation of nucleus sampling in Python:

import numpy as np

def nucleus_sampling(logits, p=0.9):
    """
    Implements nucleus sampling, a technique used in natural language processing to control the diversity of generated text.
    
    Args:
    - logits: an array of predicted logits for the next token
    - p: the probability threshold, also known as the "nucleus", to use for filtering out unlikely tokens
    
    Returns:
    - the index of the most likely token, sampled from the filtered set of tokens
    """
    sorted_logits = np.sort(logits)
    cumulative_probs = np.cumsum(np.exp(sorted_logits)) / np.sum(np.exp(sorted_logits))
    threshold_index = np.searchsorted(cumulative_probs, p)
    threshold_logits = sorted_logits[threshold_index]
    filtered_logits = np.where(logits >= threshold_logits, logits, -1e32)
    filtered_probs = np.exp(filtered_logits) / np.sum(np.exp(filtered_logits))

    return np.random.choice(np.arange(len(logits)), p=filtered_probs)


You can use this function to generate text by repeatedly making predictions for the next token given the current context and sampling from the filtered set of tokens using the nucleus sampling function.

assistant: Never