---
title: Spectral Angle Mapper
description: >-
  Spectral Angle Mapper determines the spectral similarity between image spectra
  and reference spectra.
canonical: 'https://lightning.ai/docs/torchmetrics/stable/image/spectral_angle_mapper'
---
# Spectral Angle Mapper {#spectral-angle-mapper}

## Module Interface {#module-interface}

### `class torchmetrics.image.SpectralAngleMapper(reduction='elementwise_mean', **kwargs)` {#torchmetrics.image.SpectralAngleMapper}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/sam.py#L31-L167)

[Spectral Angle Mapper](https://www.semanticscholar.org/paper/Discrimination-among-semi-arid-landscape-endmembers-Yuhas-Goetz/4dacd52af0ec8b2b5c0445ce91ecbfc1780001b2) determines the spectral similarity between image spectra and reference spectra.

It works by calculating the angle between the spectra, where small angles between indicate high similarity and high angles indicate low similarity.

As input to `forward` and `update` the metric accepts the following input

*   `preds` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Predictions from model of shape `(N,C,H,W)`
    
*   `target` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): Ground truth values of shape `(N,C,H,W)`
    

As output of forward and compute the metric returns the following output

*   `sam` ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")): if `reduction!='none'` returns float scalar tensor with average SAM value over sample else returns tensor of shape `(N,)` with SAM values per sample
    

Parameters:

*   **reduction** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`\]\]) –
    
    a method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'` or `None`: no reduction will be applied
        
    
*   **kwargs** ([`Any`](https://docs.python.org/3/library/typing.html#typing.Any "(in Python v3.15)")) – Additional keyword arguments, see [Advanced metric settings](/docs/torchmetrics/1.9.0/user-guide/overview.md#metric-kwargs) for more info.
    

Returns:

Tensor with SpectralAngleMapper score

Example

```python
>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> sam = SpectralAngleMapper()
>>> sam(preds, target)
tensor(0.5914)
```

#### `plot(val=None, ax=None)` {#torchmetrics.image.SpectralAngleMapper.plot}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/image/sam.py#L123-L167)

Plot a single or multiple values from the metric.

Parameters:

*   **val** (`Union`\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)"), [`Sequence`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.15)")\[[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")\], [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]) – Either a single result from calling metric.forward or metric.compute or a list of these results. If no value is provided, will automatically call metric.compute and plot that result.
    
*   **ax** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional "(in Python v3.15)")\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)")\]) – An matplotlib axis object. If provided will add plot to that axis
    

Return type:

[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple "(in Python v3.15)")\[[`Figure`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure "(in Matplotlib v3.11.2)"), `Union`\[[`Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes "(in Matplotlib v3.11.2)"), [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray "(in NumPy v2.5)")\]\]

Returns:

Figure and Axes object

Raises:

[**ModuleNotFoundError**](https://docs.python.org/3/builtins/exceptions.html#ModuleNotFoundError "(in Python v3.15)") – If matplotlib is not installed

```python
>>> # Example plotting single value
>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> metric = SpectralAngleMapper()
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()
```

[![../../\_images/spectral\_angle\_mapper-1.png](/docs/torchmetrics/1.9.0/static/_images/spectral_angle_mapper-1.png)](/docs/torchmetrics/1.9.0/static/_images/spectral_angle_mapper-1.png)

```python
>>> # Example plotting multiple values
>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> metric = SpectralAngleMapper()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)
```

[![../../\_images/spectral\_angle\_mapper-2.png](/docs/torchmetrics/1.9.0/static/_images/spectral_angle_mapper-2.png)](/docs/torchmetrics/1.9.0/static/_images/spectral_angle_mapper-2.png)

## Functional Interface {#functional-interface}

### `torchmetrics.functional.image.spectral_angle_mapper(preds, target, reduction='elementwise_mean')` {#torchmetrics.functional.image.spectral_angle_mapper}

[\[source\]](https://github.com/Lightning-AI/torchmetrics/blob/v1.9.0/src/torchmetrics/functional/image/sam.py#L81-L121)

Universal Spectral Angle Mapper.

Parameters:

*   **preds** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – estimated image
    
*   **target** ([`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")) – ground truth image
    
*   **reduction** ([`Literal`](https://docs.python.org/3/library/typing.html#typing.Literal "(in Python v3.15)")\[`'elementwise_mean'`, `'sum'`, `'none'`, `None`\]) –
    
    a method to reduce metric score over labels.
    
    *   `'elementwise_mean'`: takes the mean (default)
        
    *   `'sum'`: takes the sum
        
    *   `'none'` or `None`: no reduction will be applied
        
    

Return type:

[`Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")

Returns:

Tensor with Spectral Angle Mapper score

Raises:

*   [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError "(in Python v3.15)") – If `preds` and `target` don’t have the same data type.
    
*   [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If `preds` and `target` don’t have `BxCxHxW shape`.
    

Example

```python
>>> from torch import rand
>>> from torchmetrics.functional.image import spectral_angle_mapper
>>> preds = rand([16, 3, 16, 16],)
>>> target = rand([16, 3, 16, 16])
>>> spectral_angle_mapper(preds, target)
tensor(0.5914)
```

References

\[1\] Roberta H. Yuhas, Alexander F. H. Goetz and Joe W. Boardman, “Discrimination among semi-arid landscape endmembers using the Spectral Angle Mapper (SAM) algorithm” in PL, Summaries of the Third Annual JPL Airborne Geoscience Workshop, vol. 1, June 1, 1992.

---

**Navigation**

- Parent: [Image](/docs/torchmetrics/1.9.0/image.md)
