TFLearn Input_data 与 fully_connected

# Input_data

[input_data解释](http://tflearn.org/layers/core/#input-data)

[input_data源码] (https://github.com/tflearn/tflearn/blob/master/tflearn/layers/core.py)

def input_data(shape=None, placeholder=None, dtype=tf.float32,
               data_preprocessing=None, data_augmentation=None,
               name="InputData"):
    """ Input Data.
  
    Input:
        List of `int` (Shape), to create a new placeholder.
            Or
        `Tensor` (Placeholder), to use an existing placeholder.

    Output:
        Placeholder Tensor with given shape.
    """ 

## input_data.shape

input_data 函数用于定义输入层, 作为一个占位符,表示一个模型中输入数据的结构。一般主要涉及参数shape与name,其余参数默认即可。

例如 input_data 定义了一个  第一维不限制,第二维为1的矩阵

g = tflearn.input_data(shape=[None, 1])
g = tflearn.fully_connected(g, 128, activation='linear')
g = tflearn.fully_connected(g, 128, activation='linear')
g = tflearn.fully_connected(g, 1, activation='sigmoid')

又例如 input_data定义了  None * 32 * 32 *3 的一个四维矩阵,对输入参数做了一个限制。

# Build network
network = input_data(shape=[None, 32, 32, 3], dtype=tf.float32)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

## input_data.name

name参数表示输入层的名字

network = tflearn.input_data(shape=[None, 784], name="input")
network = self.make_core_network(network)
network = regression(network, optimizer='adam', learning_rate=0.01,
                     loss='categorical_crossentropy', name='target')

# fully_connected 全连接层

[fully_connected解释](http://tflearn.org/layers/core/#fully-connected)

[fully_connected源码] (https://github.com/tflearn/tflearn/blob/master/tflearn/layers/core.py)

def fully_connected(incoming, n_units, activation='linear', bias=True,
                    weights_init='truncated_normal', bias_init='zeros',
                    regularizer=None, weight_decay=0.001, trainable=True,
                    restore=True, reuse=False, scope=None,
                    name="FullyConnected"):

## 全连接层的解读

**全连接层**:每一个结点都与上一层的所有结点相连,用来把前边提取到的特征综合起来。

**全连接层的作用**: 在卷积神经网络的最后,往往会出现一两层全连接层,全连接一般会把卷积输出的二维特征图转化成一维的一个向量。 因为传统的网络我们的输出都是分类,也就是几个类别的概率甚至就是一个数--类别号,那么全连接层就是高度提纯的特征了,方便交给最后的分类器或者回归。

**全连接层的特征**:

MLP是fully connected layer. 而CNN是local connected layer.
所以我们可以说fully connected layer的特点就是没有weights share.
连接方式是点对点的
初学者认为 1x1 的filter size就是fully connected

ä¸ç»´å¨è¿æ¥å±


## 如何判定是否全连接层
* 对于neuron的连接(点对点的连接)都是fully connected.(这里其实就是MLP)
* 对于有filter的network,不是看filter的size,而是看output的feature map的size。如果output feature map的size是1x1xN的话这个layer就是fully connected layer。

## 细化第二个判断方法:
1. 1x1的filter size不一定是fully connected。比如input size是10x10x100,filter size是1x1x100,重复50次,则该layer的总weights是:1x1x100x50。

2. 1x1的filter size如果要是fully connected,则input size必须是1x1。关于这一点我们在前面使用matconvnet的时候有介绍过,fc的input size如果不是1x1就会出错。

3. input size是10x10的时候却是fully connected的情况:这里我们的output size肯定是1x1,且我们的filter size肯定是10x10。

因此我们也可以将第二点总结为:
filter size等于input size则是fully connected。

## 为何说 1*1 不一定是 fully connected 
* Convolution中当filter size等于input size时,就是fully connected,此时的output size为1x1xN。
* 当1x1不等于input size的时候,1x1一样具备weights share的能力。

[fully connected 和 local connected参考文章](https://blog.csdn.net/qq_20259459/article/details/70598929)

[解释一下全连接层](https://blog.csdn.net/u011021773/article/details/78121359)

猜你喜欢

转载自blog.csdn.net/ajian0051/article/details/82291160