# 使用cross-entropy分类手写数字

本节使用network2.py。\
network2.py不仅包含cross-entropy，还有其他一些改进。\
使用`help(network2.Network.SGD)`可以获取文档信息。

通过代码验证发现使用cross-entropy的识别率要高一些。\
但就此认定cross-entropy要优于二次代价函数是不严谨的。\
还需要结合超参数的选择。

## 代码

### cross entropy代价函数

`class CrossEntropyCost`来实现cross entropy代价函数。

因为代价函数涉及到了两处不同的计算改变，因此将这两处的函数封装到一个类中。

1. cost的计算 &#x20;

```python
@staticmethod
def fn(a, y):
    return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))
```

1. error的计算

```python
@staticmethod
def delta(z, a, y):
    return (a-y)
```

### 二次代价函数

相应的，二次代价函数也封装成了一个类

```python
class QuadraticCost(object):

    @staticmethod
    def fn(a, y):
        return 0.5*np.linalg.norm(a-y)**2

    @staticmethod
    def delta(z, a, y):
        return (a-y) * sigmoid_prime(z)
```


---

# 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://windmising.gitbook.io/nielsen-nndl/introduction-2/crossentropy-dai-jia-han-shu/3.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.
