---
# System prepended metadata

title: softmax
tags: [softmax]

---

---
tags: softmax
---
# softmax
[softmax解決](https://www.codelast.com/%E5%8E%9F%E5%88%9B-%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2softmax%E5%87%BD%E6%95%B0%E4%B8%8A%E6%BA%A2%E5%87%BAoverflow%E5%92%8C%E4%B8%8B%E6%BA%A2%E5%87%BAunderflow/)
[softmax vs logsoftmax](https://blog.csdn.net/lanchunhui/article/details/51248184)
## 解決softmax overflow,underflow
```python=
import numpy as np
def softmax(x):
    return np.exp(x)/np.sum(np.exp(x))

softmax([1000,2000,3000])
```
數值太大導致overflow
![](https://i.imgur.com/N1Wf3Gy.png)
解決方法

```python=
def evalute_softmax(x):
    maxV = np.max(x)
    return np.exp(x-maxV)/np.sum(np.exp(x-maxV))
```

## 解決 logsoftmax
[log softmax](https://www.codelast.com/%E5%8E%9F%E5%88%9B-%E5%A6%82%E4%BD%95%E9%98%B2%E6%AD%A2softmax%E5%87%BD%E6%95%B0%E4%B8%8A%E6%BA%A2%E5%87%BAoverflow%E5%92%8C%E4%B8%8B%E6%BA%A2%E5%87%BAunderflow/)
```python=
def log_softmax(x):
    maxV = np.max(x)
    return (x-maxV)-np.log(np.sum(np.exp(x-maxV)))
```
