美文网首页
手写 fully_connected全连接层

手写 fully_connected全连接层

作者: 猴子喜 | 来源:发表于2020-03-24 00:18 被阅读0次

    fully_connected用于生成全连接层,总体思路就是

    用_variable_with_weight_decay生成w×x+b中的w,并加上偏置biases。

    最后通过参数提供batch_norm和activation等功能

    def fully_connected(inputs,
                        num_outputs,
                        scope,
                        use_xavier=True,
                        stddev=1e-3,
                        weight_decay=0.0,
                        activation_fn=tf.nn.relu,
                        bn=False,
                        bn_decay=None,
                        is_training=None):
      """ Fully connected layer with non-linear operation.
    
      Args:
        inputs: 2-D tensor BxN
        num_outputs: int
    
      Returns:
        Variable tensor of size B x num_outputs.
      """
      with tf.variable_scope(scope) as sc:
    
        num_input_units = inputs.get_shape()[-1].value
        weights = _variable_with_weight_decay('weights',
                                              shape=[num_input_units, num_outputs],
                                              use_xavier=use_xavier,
                                              stddev=stddev,
                                              wd=weight_decay)
        outputs = tf.matmul(inputs, weights)
        biases = _variable_on_cpu('biases', [num_outputs],
                                 tf.constant_initializer(0.0))
        outputs = tf.nn.bias_add(outputs, biases)
    
    
        if bn:
          outputs = batch_norm_for_fc(outputs, is_training, bn_decay, 'bn')
    
        if activation_fn is not None:
          outputs = activation_fn(outputs)
    
        return outputs
    

    相关文章

      网友评论

          本文标题:手写 fully_connected全连接层

          本文链接:https://www.haomeiwen.com/subject/bwlnyhtx.html