def dense(x,
num_outputs,
scope=None,
activation=None,
reuse=None,
bn=False,
post_bn=False,
phase=None):
with tf.variable_scope(scope, 'dense', reuse=reuse):
# convert x to 2-D tensor
dim = np.prod(x._shape_as_list()[1:])
x = tf.reshape(x, [-1, dim])
weights_shape = (x.get_shape().dims[-1], num_outputs)
# dense layer
weights = tf.get_variable('weights', weights_shape,
initializer=variance_scaling_initializer())
biases = tf.get_variable('biases', [num_outputs],
initializer=tf.zeros_initializer)
output = tf.matmul(x, weights) + biases
if bn: output = batch_norm(output, phase, scope='bn')
if activation: output = activation(output)
if post_bn: output = batch_norm(output, phase, scope='post_bn')
return output
评论列表
文章目录