1.tensorflow是静态流程图,先搭建好模型图,然后再让数据流通,流通的过程中才能得到中间值。
2.cnn原本是对图片进行特征提取的,在自然语言处理中,文本通过word2vec技术也可以转换成“图片”的样子。
3.目标:理解一下cnn的工作,以及在tensorflow里怎么调用实现。
4.cnn可是使用GPU提速,os.environ["CUDA_VISIBLE_DEVICES"]=2 # 设置使用的GPU,但本文不适用GPU。
1)数据集准备
黑白图片:[w, h] 彩色图片:[w, h, c]
X=[batch_size, w, h, c] 有batch_size张图片,图片形状是[w, h, c]
Y=[batch_size, y] batch_size张图片都对应一个标签y,目前此处为二分类
其中,np.random.uniform(low, high, size) 从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high。 np.random.randint(low, high, size) [0,2)整数,即二分类:0,1 [0,10)整数,即10分类:0,1,2,3,4,5,6,7,8,9

2)图模型搭建---模型输入,占位符建立

3)模型搭建---定义cnn相关的参数初始化、卷积、池化函数

4)模型搭建---cnn模型具体化
创建一个会话 with tf.Session() as sess: 就可以边搭建模型,边查看每个中间变量的shape和value了。 下图是一个卷积+激活函数处理的过程。

池化处理

扁平化处理+全连接处理

网友评论