def BinarizedSpatialConvolution(nOutputPlane, kW, kH, dW=1, dH=1,
padding='VALID', bias=True, reuse=None, name='BinarizedSpatialConvolution'):
def b_conv2d(x, is_training=True):
nInputPlane = x.get_shape().as_list()[3]
with tf.variable_op_scope([x], None, name, reuse=reuse):
w = tf.get_variable('weight', [kH, kW, nInputPlane, nOutputPlane],
initializer=tf.contrib.layers.xavier_initializer_conv2d())
bin_w = binarize(w)
bin_x = binarize(x)
'''
Note that we use binarized version of the input and the weights. Since the binarized function uses STE
we calculate the gradients using bin_x and bin_w but we update w (the full precition version).
'''
out = tf.nn.conv2d(bin_x, bin_w, strides=[1, dH, dW, 1], padding=padding)
if bias:
b = tf.get_variable('bias', [nOutputPlane],initializer=tf.zeros_initializer)
out = tf.nn.bias_add(out, b)
return out
return b_conv2d
评论列表
文章目录