> For the complete documentation index, see [llms.txt](https://baozoulin.gitbook.io/tensorflow/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://baozoulin.gitbook.io/tensorflow/gao-jie-nei-rong/cnn-juan-ji-shen-jing-wang-luo-1.md).

# CNN 卷积神经网络 1

## 定义卷积层的 weight bias

```python
mnist=input_data.read_data_sets('MNIST_data',one_hot=true)
```

输入**shape**，返回变量的参数。

**tf.truncted\_normal**产生随机变量来进行初始化:

```python
def weight_variable(shape): 
    inital=tf.truncted_normal(shape,stddev=0.1)
    return tf.Variable(initial)
```

```python
def bias_variable(shape): 
    initial=tf.constant(0.1,shape=shape) 
    return tf.Variable(initial)
```

**定义卷积**

**tf.nn.conv2d**函数是tensoflow里面的二维的卷积函数，**x**是图片的所有参数，**W**是此卷积层的权重

步长**strides**的值：

**strides\[0]**&#x548C;**strides\[3]**&#x7684;两个1是默认值，中间两个1代表**padding**时在x方向运动一步，y方向运动一步

padding采用的方式是**SAME**

```python
def conv2d(x,W):
    return tf.nn.conv2d(x,W,strides=[1,1,1,1]，padding='SAME')
```

## 定义 pooling

采用**池化pooling**来稀疏化参数

一种是最大值池化，一种是平均值池化

池化的核函数大小为2x2，因此ksize=\[1,2,2,1]:

```python
def max_poo_2x2(x): 
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1])
```
