# eval\_KNNplot

## Function: eval\_KNNplot

```python
def eval_KNNplot(pipe_data_test, embedding_model, samples=2, device='cuda'):
```

### Description

`eval_KNNplot` is a function that applies k-Nearest Neighbors (k-NN) on the embedding space of an input dataset and plots the nearest neighbors for a given number of randomly chosen samples.

It first uses the given model to embed the data. Then, it fits a NearestNeighbors model on these embeddings. For a random selection of the test samples, it finds the nearest neighbors, reads the corresponding images from disk, and plots them along with their labels. Images with labels matching the original sample's label are indicated with a black title, while those that do not match are indicated with a red title.

### Parameters

* `pipe_data_test` (PipeDataset): The test data as a PipeDataset object.
* `embedding_model` (torch.nn.Module): The model to create embeddings.
* `device` (str, optional): The device to perform computations on. Default is 'cuda'.
* `samples` (int, optional): The number of images to plot nearest neighbors for.

### Returns

This function does not return any value. It performs computations and generates plots as side effects.

### Example

Here's an example of how to use the `eval_KNNplot` function:

```python
pipe_data_test = PipeDataset(...)
embedding_model = torch.nn.Embedding(...)
eval_KNNplot(pipe_data_test, embedding_model, samples=3, device='cuda')
```

In this example, the `eval_KNNplot` function is used to find the nearest neighbors for 3 randomly chosen samples from `pipe_data_test` in the embedding space created by `embedding_model`. It plots these neighbors and indicates whether their labels match the original sample's label.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://autossl.gitbook.io/autossl/api/evaluate/eval_knnplot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
