The :class:`BinaryDense` class is a binary fully connected layer, which weights are either -1 or 1 while inferencing. Note that, the bias vector would not be binarized. Parameters ---------- n_units : int The number of units of this layer. act : activation function
| 15 | |
| 16 | |
| 17 | class BinaryDense(Layer): |
| 18 | """The :class:`BinaryDense` class is a binary fully connected layer, which weights are either -1 or 1 while inferencing. |
| 19 | |
| 20 | Note that, the bias vector would not be binarized. |
| 21 | |
| 22 | Parameters |
| 23 | ---------- |
| 24 | n_units : int |
| 25 | The number of units of this layer. |
| 26 | act : activation function |
| 27 | The activation function of this layer, usually set to ``tf.act.sign`` or apply :class:`Sign` after :class:`BatchNorm`. |
| 28 | use_gemm : boolean |
| 29 | If True, use gemm instead of ``tf.matmul`` for inference. (TODO). |
| 30 | W_init : initializer |
| 31 | The initializer for the weight matrix. |
| 32 | b_init : initializer or None |
| 33 | The initializer for the bias vector. If None, skip biases. |
| 34 | in_channels: int |
| 35 | The number of channels of the previous layer. |
| 36 | If None, it will be automatically detected when the layer is forwarded for the first time. |
| 37 | name : None or str |
| 38 | A unique layer name. |
| 39 | |
| 40 | """ |
| 41 | |
| 42 | def __init__( |
| 43 | self, |
| 44 | n_units=100, |
| 45 | act=None, |
| 46 | use_gemm=False, |
| 47 | W_init=tl.initializers.truncated_normal(stddev=0.05), |
| 48 | b_init=tl.initializers.constant(value=0.0), |
| 49 | in_channels=None, |
| 50 | name=None, #'binary_dense', |
| 51 | ): |
| 52 | super().__init__(name, act=act) |
| 53 | self.n_units = n_units |
| 54 | self.use_gemm = use_gemm |
| 55 | self.W_init = W_init |
| 56 | self.b_init = b_init |
| 57 | self.in_channels = in_channels |
| 58 | |
| 59 | if self.in_channels is not None: |
| 60 | self.build((None, self.in_channels)) |
| 61 | self._built = True |
| 62 | |
| 63 | logging.info( |
| 64 | "BinaryDense %s: %d %s" % |
| 65 | (self.name, n_units, self.act.__name__ if self.act is not None else 'No Activation') |
| 66 | ) |
| 67 | |
| 68 | def __repr__(self): |
| 69 | actstr = self.act.__name__ if self.act is not None else 'No Activation' |
| 70 | s = ('{classname}(n_units={n_units}, ' + actstr) |
| 71 | if self.in_channels is not None: |
| 72 | s += ', in_channels=\'{in_channels}\'' |
| 73 | if self.name is not None: |
| 74 | s += ', name=\'{name}\'' |
no outgoing calls
searching dependent graphs…