Skip to content

Commit

Permalink
bias toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
YeongHyeon committed Sep 30, 2021
1 parent 4b0e0c9 commit fc78523
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Binary file added dist/whiteboxlayer-0.1.18-py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name = 'whiteboxlayer',
version = '0.1.17',
version = '0.1.18',
description = 'TensorFlow based custom layers',
author = 'YeongHyeon Park',
author_email = '[email protected]',
Expand Down
35 changes: 20 additions & 15 deletions whiteboxlayer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ def maxpool(self, x, ksize=2, strides=1, \

def conv1d(self, x, stride, \
filter_size=[3, 16, 32], dilations=[1, 1, 1], \
padding='SAME', batch_norm=False, trainable=True, activation=None, name='', verbose=True):
padding='SAME', batch_norm=False, trainable=True, activation=None, usebias=True, name='', verbose=True):

w = self.get_variable(shape=filter_size, \
trainable=trainable, name='%s_w' %(name))
b = self.get_variable(shape=[filter_size[-1]], \
if(usebias): b = self.get_variable(shape=[filter_size[-1]], \
trainable=trainable, name='%s_b' %(name))

wx = tf.nn.conv1d(
Expand All @@ -123,7 +123,8 @@ def conv1d(self, x, stride, \
name='%s_cv' %(name)
)

y = tf.math.add(wx, b, name='%s_add' %(name))
if(usebias): y = tf.math.add(wx, b, name='%s_add' %(name))
else: y = wx
if(verbose): print("Conv (%s)" %(name), x.shape, "->", y.shape)

if(batch_norm): y = self.batch_normalization(x=y, \
Expand All @@ -132,11 +133,11 @@ def conv1d(self, x, stride, \

def convt1d(self, x, stride, output_shape, \
filter_size=[3, 16, 32], dilations=[1, 1, 1], \
padding='SAME', batch_norm=False, trainable=True, activation=None, name='', verbose=True):
padding='SAME', batch_norm=False, trainable=True, activation=None, usebias=True, name='', verbose=True):

w = self.get_variable(shape=filter_size, \
trainable=trainable, name='%s_w' %(name))
b = self.get_variable(shape=[filter_size[-2]], \
if(usebias): b = self.get_variable(shape=[filter_size[-2]], \
trainable=trainable, name='%s_b' %(name))

wx = tf.nn.conv1d_transpose(
Expand All @@ -150,7 +151,8 @@ def convt1d(self, x, stride, output_shape, \
name='%s_cvt' %(name)
)

y = tf.math.add(wx, b, name='%s_add' %(name))
if(usebias): y = tf.math.add(wx, b, name='%s_add' %(name))
else: y = wx
if(verbose): print("ConvT (%s)" %(name), x.shape, "->", y.shape)

if(batch_norm): y = self.batch_normalization(x=y, \
Expand All @@ -159,11 +161,11 @@ def convt1d(self, x, stride, output_shape, \

def conv2d(self, x, stride, \
filter_size=[3, 3, 16, 32], dilations=[1, 1, 1, 1], \
padding='SAME', batch_norm=False, trainable=True, activation=None, name='', verbose=True):
padding='SAME', batch_norm=False, trainable=True, activation=None, usebias=True, name='', verbose=True):

w = self.get_variable(shape=filter_size, \
trainable=trainable, name='%s_w' %(name))
b = self.get_variable(shape=[filter_size[-1]], \
if(usebias): b = self.get_variable(shape=[filter_size[-1]], \
trainable=trainable, name='%s_b' %(name))

wx = tf.nn.conv2d(
Expand All @@ -176,7 +178,8 @@ def conv2d(self, x, stride, \
name='%s_cv' %(name)
)

y = tf.math.add(wx, b, name='%s_add' %(name))
if(usebias): y = tf.math.add(wx, b, name='%s_add' %(name))
else: y = wx
if(verbose): print("Conv (%s)" %(name), x.shape, "->", y.shape)

if(batch_norm): y = self.batch_normalization(x=y, \
Expand All @@ -185,11 +188,11 @@ def conv2d(self, x, stride, \

def convt2d(self, x, stride, output_shape, \
filter_size=[3, 3, 16, 32], dilations=[1, 1, 1, 1], \
padding='SAME', batch_norm=False, trainable=True, activation=None, name='', verbose=True):
padding='SAME', batch_norm=False, trainable=True, activation=None, usebias=True, name='', verbose=True):

w = self.get_variable(shape=filter_size, \
trainable=trainable, name='%s_w' %(name))
b = self.get_variable(shape=[filter_size[-2]], \
if(usebias): b = self.get_variable(shape=[filter_size[-2]], \
trainable=trainable, name='%s_b' %(name))

wx = tf.nn.conv2d_transpose(
Expand All @@ -203,25 +206,27 @@ def convt2d(self, x, stride, output_shape, \
name='%s_cvt' %(name)
)

y = tf.math.add(wx, b, name='%s_add' %(name))
if(usebias): y = tf.math.add(wx, b, name='%s_add' %(name))
else: y = wx
if(verbose): print("ConvT (%s)" %(name), x.shape, "->", y.shape)

if(batch_norm): y = self.batch_normalization(x=y, \
trainable=trainable, name='%s_bn' %(name), verbose=verbose)
return self.activation(x=y, activation=activation, name=name)

def fully_connected(self, x, c_out, \
batch_norm=False, trainable=True, activation=None, name='', verbose=True):
batch_norm=False, trainable=True, activation=None, usebias=True, name='', verbose=True):

c_in, c_out = x.get_shape().as_list()[-1], int(c_out)

w = self.get_variable(shape=[c_in, c_out], \
trainable=trainable, name='%s_w' %(name))
b = self.get_variable(shape=[c_out], \
if(usebias): b = self.get_variable(shape=[c_out], \
trainable=trainable, name='%s_b' %(name))

wx = tf.linalg.matmul(x, w, name='%s_mul' %(name))
y = tf.math.add(wx, b, name='%s_add' %(name))
if(usebias): y = tf.math.add(wx, b, name='%s_add' %(name))
else: y = wx
if(verbose): print("FC (%s)" %(name), x.shape, "->", y.shape)

if(batch_norm): y = self.batch_normalization(x=y, \
Expand Down

0 comments on commit fc78523

Please sign in to comment.