使用二维数据构造简单卷积神经网络

觉得有用的话,欢迎一起讨论相互学习~

图像和一些时序数据集都可以用二维数据的形式表现,我们此次使用随机分布的二位数据构造一个简单的CNN—网络卷积-最大池化-全连接

参考代码

# Implementing Different Layers
# ---------------------------------------
#
# We will illustrate how to use different types
# of layers in TensorFlow
#
# The layers of interest are:
# (1) Convolutional Layer卷积层
# (2) Activation Layer激活层
# (3) Max-Pool Layer池化层
# (4) Fully Connected Layer 全连接层
#
# We will generate two different data sets for this
# script, a 1-D data set (row of data) and
# a 2-D data set (similar to picture)
import tensorflow as tf
import matplotlib.pyplot as plt
import csv
import os
import random
import numpy as np
import random
from tensorflow.python.framework import ops # ---------------------------------------------------|
# -------------------2D-data-------------------------|
# ---------------------------------------------------| # Reset Graph 重置图模型
ops.reset_default_graph()
sess = tf.Session() # parameters for the run 运行参数
row_size = 10 # 2D图形高
col_size = 10 # 2D图形长
conv_size = 2
conv_stride_size = 2 # 卷积步长
maxpool_size = 2
maxpool_stride_size = 1 # 池化步长 # ensure reproducibility 确保复现性
seed = 13
np.random.seed(seed)
tf.set_random_seed(seed) # Generate 2D data生成随机二维数据
data_size = [row_size, col_size]
data_2d = np.random.normal(size=data_size) # --------Placeholder--------
x_input_2d = tf.placeholder(dtype=tf.float32, shape=data_size) # Convolution 卷积层
def conv_layer_2d(input_2d, my_filter, stride_size):
# TensorFlow's 'conv2d()' function only works with 4D arrays:
# [batch, height, width, channels], we have 1 batch, and
# 1 channel, but we do have width AND height this time.
# So next we create the 4D array by inserting dimension 1's.
# Tensorflow的卷积操作默认输入有四个维度[batch_size, height, width, channels]
# 此处我们将维度增加到四维
input_3d = tf.expand_dims(input_2d, 0)
input_4d = tf.expand_dims(input_3d, 3)
convolution_output = tf.nn.conv2d(input_4d, filter=my_filter,
strides=[1, stride_size, stride_size, 1], padding="VALID")
# filter表示卷积核,数据维度为[卷积核高度,卷积核长度,输入通道数,输出通道数]
# stride_size表示步长,数据维度为[批处理数据大小,步长高,步长宽,通道数],其中批处理数据大小和通道数一般跨度都为1,不需要修改。
# Get rid of unnecessary dimensions
# 将维数为1的维度去掉,保留数值数据。
conv_output_2d = tf.squeeze(convolution_output)
return (conv_output_2d) # Create Convolutional Filter
my_filter = tf.Variable(tf.random_normal(shape=[conv_size, conv_size, 1, 1]))
# Create Convolutional Layer
my_convolution_output = conv_layer_2d(x_input_2d, my_filter, stride_size=conv_stride_size) # --------Activation--------
def activation(input_1d):
return (tf.nn.relu(input_1d)) # Create Activation Layer 激活层
my_activation_output = activation(my_convolution_output) # --------Max Pool--------
def max_pool(input_2d, width, height, stride):
# Just like 'conv2d()' above, max_pool() works with 4D arrays.
# [batch_size=1, height=given, width=given, channels=1]
input_3d = tf.expand_dims(input_2d, 0)
input_4d = tf.expand_dims(input_3d, 3)
# Perform the max pooling with strides = [1,1,1,1]
# If we wanted to increase the stride on our data dimension, say by
# a factor of '2', we put strides = [1, 2, 2, 1]
pool_output = tf.nn.max_pool(input_4d, ksize=[1, height, width, 1],
strides=[1, stride, stride, 1],
padding='VALID')
# Get rid of unnecessary dimensions
pool_output_2d = tf.squeeze(pool_output)
return (pool_output_2d) # Create Max-Pool Layer 最大池化层
# 即选择窗口中的最大值作为输出,池化层的窗口格式定义和卷积核的不一样
# 其四维数组格式定义和卷积步长,池化步长一致,和输入格式相同,即
# [batch_size, height, width, channels]
my_maxpool_output = max_pool(my_activation_output,
width=maxpool_size, height=maxpool_size, stride=maxpool_stride_size) # --------Fully Connected--------
def fully_connected(input_layer, num_outputs):
# 扁平化/光栅化处理使最大池化层输出为一维向量形式
flat_input = tf.reshape(input_layer, [-1])
# 设定weight的形状
weight_shape = tf.squeeze(tf.stack([tf.shape(flat_input), [num_outputs]]))
# 初始化weight
weight = tf.random_normal(weight_shape, stddev=0.1)
# 初始化bias
bias = tf.random_normal(shape=[num_outputs])
# 将一维输入还原为二维
input_2d = tf.expand_dims(flat_input, 0)
# 计算输出
full_output = tf.add(tf.matmul(input_2d, weight), bias)
# 降维,去掉维度为1的维度,便于进行观察
full_output_2d = tf.squeeze(full_output)
return (full_output_2d) # Create Fully Connected Layer
my_full_output = fully_connected(my_maxpool_output, 5) # Run graph
# Initialize Variables
init = tf.global_variables_initializer()
sess.run(init) feed_dict = {x_input_2d: data_2d} print('\n>>>> 2D Data <<<<') # Convolution Output
print('Input = %s array'%(x_input_2d.shape.as_list()))
print('%s Convolution, stride size = [%d, %d] , results in the %s array'%
(my_filter.get_shape().as_list()[:2], conv_stride_size, conv_stride_size, my_convolution_output.shape.as_list()))
print(sess.run(my_convolution_output, feed_dict=feed_dict)) # Activation Output
print('\nInput = the above %s array'%(my_convolution_output.shape.as_list()))
print('ReLU element wise returns the %s array'%(my_activation_output.shape.as_list()))
print(sess.run(my_activation_output, feed_dict=feed_dict)) # Max Pool Output
print('\nInput = the above %s array'%(my_activation_output.shape.as_list()))
print('MaxPool, stride size = [%d, %d], results in %s array'%
(maxpool_stride_size, maxpool_stride_size, my_maxpool_output.shape.as_list()))
print(sess.run(my_maxpool_output, feed_dict=feed_dict)) # Fully Connected Output 全连接层
print('\nInput = the above %s array'%(my_maxpool_output.shape.as_list()))
# 全连接层输出除掉batch_size和channel维度后剩余维度为[4,4]
print('Fully connected layer on all %d rows results in %s outputs:'%
(my_maxpool_output.shape.as_list()[0], my_full_output.shape.as_list()[0]))
# 由于全连接层神经元为5个所以输出维度为5
print(sess.run(my_full_output, feed_dict=feed_dict)) '''对于卷积层输出维度[batch_size,output_size,output_size',channels]
其中output_size及output_size'表示为对应维度上通过(W-F+2P)/S+1得到的结果
W为数据维度,F为卷积核或池化窗口的宽或高,P为Padding大小,其中设置卷积为Valid时,Padding为0若设置为SAME卷积,则会有Padding,S是步长大小
本例子中卷积层计算公式为[(10-2)+0]/2+1=5,池化层计算公式为[(5-2)+0]/1+1=4''' # >>>> 2D Data <<<<
# Input = [10, 10] array
# [2, 2] Convolution, stride size = [2, 2] , results in the [5, 5] array
# [[ 0.14431179 0.72783369 1.51149166 -1.28099763 1.78439188]
# [-2.54503059 0.76156765 -0.51650006 0.77131093 0.37542343]
# [ 0.49345911 0.01592223 0.38653135 -1.47997665 0.6952765 ]
# [-0.34617192 -2.53189754 -0.9525758 -1.4357065 0.66257358]
# [-1.98540258 0.34398788 2.53760481 -0.86784822 -0.3100495 ]]
#
# Input = the above [5, 5] array
# ReLU element wise returns the [5, 5] array
# [[ 0.14431179 0.72783369 1.51149166 0. 1.78439188]
# [ 0. 0.76156765 0. 0.77131093 0.37542343]
# [ 0.49345911 0.01592223 0.38653135 0. 0.6952765 ]
# [ 0. 0. 0. 0. 0.66257358]
# [ 0. 0.34398788 2.53760481 0. 0. ]]
#
# Input = the above [5, 5] array
# MaxPool, stride size = [1, 1], results in [4, 4] array
# [[ 0.76156765 1.51149166 1.51149166 1.78439188]
# [ 0.76156765 0.76156765 0.77131093 0.77131093]
# [ 0.49345911 0.38653135 0.38653135 0.6952765 ]
# [ 0.34398788 2.53760481 2.53760481 0.66257358]]
#
# Input = the above [4, 4] array
# Fully connected layer on all 4 rows results in 5 outputs:
# [ 0.08245847 -0.16351229 -0.55429065 -0.24322605 -0.99900764]

参考文献

TensorFlow机器学习实战指南

TensorflowTutorial_二维数据构造简单CNN的更多相关文章

  1. TensorflowTutorial_一维数据构造简单CNN

    使用一维数据构造简单卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 神经网络对于一维数据非常重要,时序数据集.信号处理数据集和一些文本嵌入数据集都是一维数据,会频繁的使用到神经网 ...

  2. 一个不错的PHP二维数组排序函数简单易用存用

    一个不错的PHP二维数组排序函数简单易用存用 传入数组,传入排序的键,传入排序顺序 public function array_sort($arr,$keys,$type='asc') { $keys ...

  3. PHP二维数据排序,二维数据模糊查询

    一.因为项目中的一个报表需要合并三个表的数据,所以分表查询再合并数据,利用PHP数组函数进行排序,搜索.三表合并后的数组结构如下: Array ( [0] => Array ( [history ...

  4. 妙用Excel数据透视表和透视图向导,将二维数据转换为一维数据

    项目中,每年都会有各种经销商的各种产品目标数据导入,经销商和产品过多,手工操作过于单调和复杂.那有没有一种方式可以将复杂的二维数据转换为一维数据呢? 有,强大的Excel就支持此功能. 常用Excel ...

  5. PCA 实例演示二维数据降成1维

    import numpy as np # 将二维数据降成1维 num = [(2.5, 2.4), (0.5, 0.7), (2.2, 2.9), (1.9, 2.2), (3.1, 3.0), (2 ...

  6. 【Excle数据透视】二维数据如何创建数据透视表

    二维数据在创建数据透视表的时候,可能会给你带来一些麻烦,没法创建,会丢失维度,那怎么办呢? 解决办法:使用数据透视表和数据透视图向导即可创建 具体操作如下: 按下[Alt+D+P],出现如下界面 选择 ...

  7. php 二维数据排序 排行榜

    php 二维数据排序 排行榜 $rateCount = array(); foreach($groupUsers as $user){ $rateCount[] = $user['rate']; } ...

  8. python练习 - 系统基本信息获取(sys标准库)+ 二维数据表格输出(tabulate库)

    系统基本信息获取 描述 获取系统的递归深度.当前执行文件路径.系统最大UNICODE编码值等3个信息,并打印输出.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

  9. c#中使用NetCDF存储二维数据的读写操作简单应用

                      [DllImport(                   [DllImport(                  [DllImport(             ...

随机推荐

  1. Windows10 环境下安装 ElasticSearch

    环境与版本 操作系统:windows 10 Elasticsearch 版本:6.1.1 Java 版本:9.0.1 ik 分词器版本:6.1.1 安装步骤 前置要求 操作系统中需要安装有 java ...

  2. CSS3对于盒中容纳不下的内容的显示——overflow属性

    overflow属性 1.如果将overflow属性值设定为hidden,则超出容纳范围的文字将被隐藏起来. div{ overflow:hidden; } 2.如果将overflow属性值设定为sc ...

  3. 51 nod 1211 数独 DLX

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1211 调了挺久的,自己的一份舞蹈链模板…… 算是在网上见到的模 ...

  4. [bzoj2638] 黑白染色

    比较蛋疼的是我们可以先染个底色,再在底色上染别的东西. 由ccz大爷的题解可得..将目标状态里相同颜色的联通块缩点后,枚举起点,生成树里的最大节点深度就是需要的次数了, 如果最大深度是白色的话记得-1 ...

  5. Zigbee Class 直播公告2016-10-10

    周一我们将开始本期课程的首次直播, 详情如下: 场次 第一场 阶段 入门 开始时间 2016-10-10  19:00 结束时间 不定 斗鱼地址 douyu.com/zigbeeclass 内容 本期 ...

  6. Python 3 利用 Dlib 19.7 实现人脸识别和剪切

    0.引言 利用python开发,借助Dlib库进行人脸识别,然后将检测到的人脸剪切下来,依次排序显示在新的图像上: 实现的效果如下图所示,将图1原图中的6张人脸检测出来,然后剪切下来,在图像窗口中依次 ...

  7. Node类型知识大全

    Node类型 1.节点关系 每个节点都有一个childNodes属性,其中保存着一个NodeList对象.NodeList是一种类数组对象,用于保存一组有序的节点,可以通过位置来访问这些节点.请注意, ...

  8. EhCache 在集群环境中使用缓存系统

    EhCache 分布式缓存/缓存集群  EhCache提供了很多种解决方案 这里只介绍一种最常用而且简单的RMI方式分布式缓存决绝方案 Automatic Peer Discovery 自动成员发现方 ...

  9. 关于Struts传递json给easyui的随笔

    今天在公司写测试代码,由于公司用的是ssh框架做的商城项目,我想先实现下简单的增删改查,奈何没有很好的后台页面(毕竟不能测试代码直接在他的项目里改啊) 所以想到了淘淘商城中有这个后台的管理页面,打算一 ...

  10. tree conflict svn 怎么解决

    如果自己和其他人修改了同一个文件,而他已经更新到SVN,你commit时就会出现冲突,如何解决呢? 方法/步骤 使用SVN时,更新一个自己修改的文件到服务器,出现冲突,因为其他同事也修改了这个文件并且 ...