zoukankan      html  css  js  c++  java
  • tf.layers.dense

    https://stackoverflow.com/questions/45693020/is-tf-layers-dense-a-single-layer

    tf.layers.dense  is only one layer with a amount of nodes. You can check on TensorFlow web site about tf.layers.dense (tf.compat.v1.layers.dense)

    layer1 = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
    layer2 = tf.layers.dense(inputs=layer1, units=1024, activation=tf.nn.relu)
    
     tf.layers.dense()只是含有很多节点的一个层,是神经网络的一层。
     
    下面的内容来自:
     

    参考:https://cuiqingcai.com/5715.html
    tf.layers 模块提供的方法有:

    Input(…): 用于实例化一个输入 Tensor,作为神经网络的输入。
    average_pooling1d(…): 一维平均池化层
    average_pooling2d(…): 二维平均池化层
    average_pooling3d(…): 三维平均池化层
    batch_normalization(…): 批量标准化层
    conv1d(…): 一维卷积层
    conv2d(…): 二维卷积层
    conv2d_transpose(…): 二维反卷积层
    conv3d(…): 三维卷积层
    conv3d_transpose(…): 三维反卷积层
    dense(…): 全连接层
    dropout(…): Dropout层
    flatten(…): Flatten层,即把一个 Tensor 展平
    max_pooling1d(…): 一维最大池化层
    max_pooling2d(…): 二维最大池化层
    max_pooling3d(…): 三维最大池化层
    separable_conv2d(…): 二维深度可分离卷积层

    dense

    dense,即全连接网络,layers 模块提供了一个 dense() 方法来实现此操作,定义在 tensorflow/python/layers/core.py 中,下面我们来说明一下它的用法。

    dense(
        inputs,
        units,
        activation=None,
        use_bias=True,
        kernel_initializer=None,
        bias_initializer=tf.zeros_initializer(),
        kernel_regularizer=None,
        bias_regularizer=None,
        activity_regularizer=None,
        kernel_constraint=None,
        bias_constraint=None,
        trainable=True,
        name=None,
        reuse=None
    )
     

    参数说明如下:
    inputs:必需,即需要进行操作的输入数据。
    units:必须,即神经元的数量。
    activation:可选,默认为 None,如果为 None 则是线性激活。
    use_bias:可选,默认为 True,是否使用偏置。
    kernel_initializer:可选,默认为 None,即权重的初始化方法,如果为 None,则使用默认的 Xavier 初始化方法。
    bias_initializer:可选,默认为零值初始化,即偏置的初始化方法。
    kernel_regularizer:可选,默认为 None,施加在权重上的正则项。
    bias_regularizer:可选,默认为 None,施加在偏置上的正则项。
    activity_regularizer:可选,默认为 None,施加在输出上的正则项。
    kernel_constraint,可选,默认为 None,施加在权重上的约束项。
    bias_constraint,可选,默认为 None,施加在偏置上的约束项。
    trainable:可选,默认为 True,布尔类型,如果为 True,则将变量添加到 GraphKeys.TRAINABLE_VARIABLES 中。
    name:可选,默认为 None,卷积层的名称。
    reuse:可选,默认为 None,布尔类型,如果为 True,那么如果 name 相同时,会重复利用。
    返回值: 全连接网络处理后的 Tensor。

    下面我们用一个实例来感受一下它的用法:

    x = tf.layers.Input(shape=[32])
    print(x)
    y1 = tf.layers.dense(x, 16, activation=tf.nn.relu)
    print(y1)
    y2 = tf.layers.dense(y1, 5, activation=tf.nn.sigmoid)
    print(y2)
     

    首先我们用 Input 定义了 [?, 32] 的输入数据,然后经过第一层全连接网络,此时指定了神经元个数为 16,激活函数为 relu,接着输出结果经过第二层全连接网络,此时指定了神经元个数为 5,激活函数为 sigmoid,最后输出,结果如下:


    Tensor("input_layer_1:0", shape=(?, 32), dtype=float32)

    Tensor("dense/Relu:0", shape=(?, 16), dtype=float32)

    Tensor("dense_2/Sigmoid:0", shape=(?, 5), dtype=float32)

    可以看到输出结果的最后一维度就等于神经元的个数,这是非常容易理解的。



    作者:听风1996
    链接:https://www.jianshu.com/p/3b8b810a9e56
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    Git 几个常用操作
    Ubuntu16.04安装YouCompleteMe
    常用命令总结
    启动Kernel提示Bad Data CRC
    linux4.15.1编译init/mounts报错
    编译Linux-4.15.1内核时遇到:“error : openssl/bio.h :No such file or folder”
    添加mtdparts引起的问题
    arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression
    smartgit的安装
    ubuntu下安装wine
  • 原文地址:https://www.cnblogs.com/yibeimingyue/p/15110402.html
Copyright © 2011-2022 走看看