forked from HirokiNakahara/GUINNESS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_binary_linear.py
63 lines (50 loc) · 2.25 KB
/
link_binary_linear.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import numpy
from chainer import link
import function_binary_linear
class BinaryLinear(link.Link):
"""Binary Linear layer (a.k.a. binary fully-connected layer).
This is a link that wraps the :func:`~chainer.functions.linear` function,
and holds a weight matrix ``W`` and optionally a bias vector ``b`` as
parameters.
The weight matrix ``W`` is initialized with i.i.d. Gaussian samples, each
of which has zero mean and deviation :math:`\\sqrt{1/\\text{in_size}}`. The
bias vector ``b`` is of size ``out_size``. Each element is initialized with
the ``bias`` value. If ``nobias`` argument is set to True, then this link
does not hold a bias vector.
Args:
in_size (int): Dimension of input vectors.
out_size (int): Dimension of output vectors.
wscale (float): Scaling factor of the weight matrix.
bias (float): Initial bias value.
nobias (bool): If True, then this function does not use the bias.
initialW (2-D array): Initial weight value. If ``None``, then this
function uses to initialize ``wscale``.
initial_bias (1-D array): Initial bias value. If ``None``, then this
function uses to initialize ``bias``.
.. seealso:: :func:`~chainer.functions.linear`
Attributes:
W (~chainer.Variable): Weight parameter.
b (~chainer.Variable): Bias parameter.
"""
def __init__(self, in_size, out_size, wscale=1, bias=0, nobias=False,
initialW=None, initial_bias=None):
super(BinaryLinear, self).__init__(W=(out_size, in_size))
if initialW is None:
initialW = numpy.random.normal(
0, wscale * numpy.sqrt(1. / in_size), (out_size, in_size))
self.W.data[...] = initialW
if nobias:
self.b = None
else:
self.add_param('b', out_size)
if initial_bias is None:
initial_bias = bias
self.b.data[...] = initial_bias
def __call__(self, x):
"""Applies the linear layer.
Args:
x (~chainer.Variable): Batch of input vectors.
Returns:
~chainer.Variable: Output of the linear layer.
"""
return function_binary_linear.binary_linear(x, self.W, self.b)