Ejemplo n.º 1
0
def create_initializer(init_type, scale=None, fillvalue=None):
    if init_type == 'identity':
        return initializers.Identity() if scale is None else initializers.Identity(scale=scale)
    if init_type == 'constant':
        return initializers.Constant(fillvalue)
    if init_type == 'zero':
        return initializers.Zero()
    if init_type == 'one':
        return initializers.One()
    if init_type == 'normal':
        return initializers.Normal() if scale is None else initializers.Normal(scale)
    if init_type == 'glorotNormal':
        return initializers.GlorotNormal() if scale is None else initializers.GlorotNormal(scale)
    if init_type == 'heNormal':
        return initializers.HeNormal() if scale is None else initializers.HeNormal(scale)
    if init_type == 'orthogonal':
        return initializers.Orthogonal(
            scale) if scale is None else initializers.Orthogonal(scale)
    if init_type == 'uniform':
        return initializers.Uniform(
            scale) if scale is None else initializers.Uniform(scale)
    if init_type == 'leCunUniform':
        return initializers.LeCunUniform(
            scale) if scale is None else initializers.LeCunUniform(scale)
    if init_type == 'glorotUniform':
        return initializers.GlorotUniform(
            scale) if scale is None else initializers.GlorotUniform(scale)
    if init_type == 'heUniform':
        return initializers.HeUniform(
            scale) if scale is None else initializers.HeUniform(scale)
    raise ValueError("Unknown initializer type: {0}".format(init_type))
Ejemplo n.º 2
0
    def __init__(self, num_entities, num_relations, fast_eval, embedding_dim=200):
        super(DistMult, self).__init__()
        self.fast_eval = fast_eval
        self.num_entities = num_entities
        self.num_relations = num_relations
        self.embedding_dim = embedding_dim

        with self.init_scope():
            self.emb_e = L.EmbedID(
                    num_entities, embedding_dim, initialW=I.GlorotNormal())
            self.emb_rel = L.EmbedID(
                    num_relations, embedding_dim, initialW=I.GlorotNormal())
    def __init__(self, ch, k, l, m, n):
        super(Reduction_A, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn2_1 = ConvBN(ch,
                                    n,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    k,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(k,
                                    l,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)
            self.convbn3_3 = ConvBN(l,
                                    m,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)
Ejemplo n.º 4
0
    def __init__(self, num_entities, num_relations, fast_eval):
        super(ConvE, self).__init__()
        self.fast_eval = fast_eval
        self.num_entities = num_entities
        self.num_relations = num_relations
        self.embedding_dim = 200

        with self.init_scope():
            self.emb_e = L.EmbedID(
                    num_entities, self.embedding_dim, initialW=I.GlorotNormal())
            self.emb_rel = L.EmbedID(
                    num_relations, self.embedding_dim, initialW=I.GlorotNormal())
            self.conv1 = L.Convolution2D(1, 32, 3, stride=1, pad=0)
            self.bias = L.EmbedID(num_entities, 1)
            self.fc = L.Linear(10368, self.embedding_dim)
            self.bn0 = L.BatchNormalization(1)
            self.bn1 = L.BatchNormalization(32)
            self.bn2 = L.BatchNormalization(self.embedding_dim)
Ejemplo n.º 5
0
 def __call__(self, atoms_1, g_1, atoms_2, g_2):
     """
     :param atoms_1: atomic representation of molecule 1, with shape of (mb, N_1, hidden_dim)
     :param g_1: molecular representation of molecule 1, with shape of (mb, out_dim)
     :param atoms_2: atomic representation of molecule 2, with shape of (mb, N_2, hidden_dim)
     :param g_2: molecular representation of molecule 2, with shape of (mb, out_dim)
     :return:
     """
     mb = atoms_1.shape[0]
     N_1 = atoms_1.shape[1]
     N_2 = atoms_2.shape[1]
     # C: (mb, N_2, N_1)
     C = self.compute_attention(query=atoms_2, key=atoms_1)
     W1_data = self.xp.zeros(shape=(N_2, self.out_dim))
     if self.xp == cuda.cupy:
         W1_data = cuda.to_gpu(W1_data)
         initializers.GlorotNormal()(W1_data)
     self.W1 = chainer.Parameter(initializer=W1_data,
                                 shape=(N_2, self.out_dim),
                                 name="W1")
     self.register_persistent("W1")
     W2_data = self.xp.zeros(shape=(N_1, self.out_dim))
     if self.xp == cuda.cupy:
         W2_data = cuda.to_gpu(W2_data)
         initializers.GlorotNormal()(W2_data)
     self.W2 = chainer.Parameter(initializer=W2_data,
                                 shape=(N_1, self.out_dim),
                                 name="W2")
     self.register_persistent("W2")
     # attn_1: (mb, N_1, out_dim)
     attn_1 = functions.matmul(
         functions.transpose(C, (0, 2, 1)),
         functions.tile(functions.expand_dims(self.W1, axis=0),
                        reps=(mb, 1, 1)))
     # attn_2: (mb, N_2, out_dim)
     attn_2 = functions.matmul(
         C,
         functions.tile(functions.expand_dims(self.W2, axis=0),
                        reps=(mb, 1, 1)))
     # compact_1: (mb, out_dim)
     compact_1 = functions.sum(attn_1 * self.j_layer(atoms_1), axis=1)
     # compact_2: (mb, out_dim)
     compact_2 = functions.sum(attn_2 * self.j_layer(atoms_2), axis=1)
     return compact_1, compact_2
Ejemplo n.º 6
0
    def __init__(self, base, n_base_output, scales):
        super().__init__()
        with self.init_scope():
            self.base = base
            self.inner = chainer.ChainList()
            self.outer = chainer.ChainList()

        init = {'initialW': initializers.GlorotNormal()}
        for _ in range(n_base_output):
            self.inner.append(L.Convolution2D(256, 1, **init))
            self.outer.append(L.Convolution2D(256, 3, pad=1, **init))

        self.scales = scales
    def __init__(self, ch, scale=1.0):
        super(Inception_Resnet_A, self).__init__()
        self.scale = scale
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch,
                                  32,
                                  ksize=1,
                                  stride=1,
                                  initialW=initialW,
                                  pad=0)

            self.convbn2_1 = ConvBN(ch,
                                    32,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn2_2 = ConvBN(32,
                                    32,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)

            self.convbn3_1 = ConvBN(ch,
                                    32,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(32,
                                    48,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)
            self.convbn3_3 = ConvBN(48,
                                    64,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)

            self.conv4 = L.Convolution2D(128,
                                         ch,
                                         ksize=1,
                                         stride=1,
                                         initialW=initialW,
                                         pad=0)
    def __init__(self, ch):
        super(Reduction_B, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn2_1 = ConvBN(ch,
                                    256,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn2_2 = ConvBN(256,
                                    384,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    256,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(256,
                                    288,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)

            self.convbn4_1 = ConvBN(ch,
                                    256,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn4_2 = ConvBN(256,
                                    288,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)

            self.convbn4_3 = ConvBN(288,
                                    320,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)
Ejemplo n.º 9
0
    def __init__(self, ch):
        super(Inception_A, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch,
                                  96,
                                  ksize=1,
                                  stride=1,
                                  initialW=initialW,
                                  pad=0)

            self.convbn2_1 = ConvBN(ch,
                                    96,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    64,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(64,
                                    96,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)

            self.convbn4_1 = ConvBN(ch,
                                    64,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn4_2 = ConvBN(64,
                                    96,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)
            self.convbn4_3 = ConvBN(96,
                                    96,
                                    ksize=3,
                                    stride=1,
                                    initialW=initialW,
                                    pad=1)
Ejemplo n.º 10
0
 def __init__(self, ch):
     super(Stem, self).__init__()
     initialW = initializers.GlorotNormal()
     with self.init_scope():
         self.convbn1 = ConvBN(ch, 32, ksize=3, stride=2,
                               initialW=initialW, pad=0)
         self.convbn2 = ConvBN(32, 32, ksize=3, stride=1,
                               initialW=initialW, pad=0)
         self.convbn3 = ConvBN(32, 64, ksize=3, stride=1,
                               initialW=initialW, pad=1)
         self.convbn4 = ConvBN(64, 80, ksize=1, stride=1,
                               initialW=initialW, pad=0)
         self.convbn5 = ConvBN(80, 192, ksize=3, stride=1,
                               initialW=initialW, pad=0)
         self.convbn6 = ConvBN(192, 256, ksize=3, stride=2,
                               initialW=initialW, pad=0)
Ejemplo n.º 11
0
    def __init__(self, base, n_base_output, scales):
        super(FPN, self).__init__()
        with self.init_scope():
            self.base = base
            self.inner = chainer.ChainList()
            self.outer = chainer.ChainList()

        init = {'initialW': initializers.GlorotNormal()}
        for _ in range(n_base_output):
            self.inner.append(L.Convolution2D(256, 1, **init))
            self.outer.append(L.Convolution2D(256, 3, pad=1, **init))

        self.scales = scales
        # hacks
        self.n_base_output = n_base_output
        self.n_base_output_minus1 = n_base_output - 1
        self.scales_minus_n_base_output = len(scales) - n_base_output
Ejemplo n.º 12
0
 def __init__(self, ch, scale):
     super(Inception_Resnet_C, self).__init__()
     self.scale = scale
     initialW = initializers.GlorotNormal()
     with self.init_scope():
         self.convbn1 = ConvBN(ch, 192, ksize=1, stride=1,
                               initialW=initialW, pad=0)
         
         self.convbn2_1 = ConvBN(ch, 192, ksize=1, stride=1,
                                 initialW=initialW, pad=0)
         self.convbn2_2 = ConvBN(192, 192, ksize=(1, 3), stride=1,
                                 initialW=initialW, pad=(0, 1))
         self.convbn2_3 = ConvBN(192, 192, ksize=(3, 1), stride=1,
                                 initialW=initialW, pad=(1, 0))
         
         self.conv3 = L.Convolution2D(384, ch, ksize=1, stride=1,
                                      initialW=initialW, pad=0)
Ejemplo n.º 13
0
 def __init__(self,
              in_size,
              n_units,
              dropout,
              activation=F.tanh,
              activation_on_final=False):
     n_units = [
         in_size,
     ] + n_units
     params = {}
     for i, n in enumerate(pairwise(n_units)):
         # Initial W according the paper
         params['l' + str(i)] = L.Linear(*n, initialW=I.GlorotNormal())
     super(MLP, self).__init__(**params)
     self.add_persistent('_dropout', dropout)
     self.add_persistent('_activation_on_final', activation_on_final)
     self._activation = activation
Ejemplo n.º 14
0
    def __init__(self, ch, scale=1.):
        super(Inception_Resnet_B, self).__init__()
        self.scale = scale
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch, 128, ksize=1, stride=1,
                                  initialW=initialW, pad=0)
            
            self.convbn2_1 = ConvBN(ch, 128, ksize=1, stride=1,
                                    initialW=initialW, pad=0)
            self.convbn2_2 = ConvBN(128, 128, ksize=(1, 7), stride=1,
                                    initialW=initialW, pad=(0, 3))

            self.convbn2_3 = ConvBN(128, 128, ksize=(7, 1), stride=1,
                                    initialW=initialW, pad=(3, 0))
            
            self.conv3 = L.Convolution2D(256, ch, ksize=1, stride=1,
                                         initialW=initialW, pad=0)
Ejemplo n.º 15
0
    def __init__(self, ch):
        super(Reduction_B, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn2_1 = ConvBN(ch,
                                    192,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn2_2 = ConvBN(192,
                                    192,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    256,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(256,
                                    256,
                                    ksize=(1, 7),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(0, 3))
            self.convbn3_3 = ConvBN(256,
                                    320,
                                    ksize=(7, 1),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(3, 0))
            self.convbn3_4 = ConvBN(320,
                                    320,
                                    ksize=3,
                                    stride=2,
                                    initialW=initialW,
                                    pad=0)
Ejemplo n.º 16
0
    def __init__(self, ch):
        super(Stem, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch,
                                  32,
                                  ksize=3,
                                  stride=2,
                                  initialW=initialW,
                                  pad=0)
            self.convbn2 = ConvBN(32,
                                  32,
                                  ksize=3,
                                  stride=1,
                                  initialW=initialW,
                                  pad=0)
            self.convbn3 = ConvBN(32,
                                  64,
                                  ksize=3,
                                  stride=1,
                                  initialW=initialW,
                                  pad=1)
            self.convbn4 = ConvBN(64,
                                  96,
                                  ksize=3,
                                  stride=2,
                                  initialW=initialW,
                                  pad=0)

            self.convbn5_1x1_1 = ConvBN(160,
                                        64,
                                        ksize=1,
                                        stride=1,
                                        initialW=initialW,
                                        pad=0)

            self.convbn5_7x1 = ConvBN(64,
                                      64,
                                      ksize=(7, 1),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(3, 0))

            self.convbn5_1x7 = ConvBN(64,
                                      64,
                                      ksize=(1, 7),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(0, 3))
            self.convbn5_3x3_1 = ConvBN(64,
                                        96,
                                        ksize=3,
                                        stride=1,
                                        initialW=initialW,
                                        pad=0)

            self.convbn5_1x1_2 = ConvBN(160,
                                        64,
                                        ksize=1,
                                        stride=1,
                                        initialW=initialW,
                                        pad=0)
            self.convbn5_3x3_2 = ConvBN(64,
                                        96,
                                        ksize=3,
                                        stride=1,
                                        initialW=initialW,
                                        pad=0)

            self.convbn6 = ConvBN(192,
                                  192,
                                  ksize=3,
                                  stride=2,
                                  initialW=initialW,
                                  pad=0)
Ejemplo n.º 17
0
    def initialize_parameters(self):
        G_init = initializers.GlorotNormal()

        #initializers.init_weight(self.W_predict.W.data, G_init)
        initializers.init_weight(self.W_candidate.W.data, G_init)
        self.initialize_LSTM(self.LSTM, G_init)
Ejemplo n.º 18
0
 def setUp(self):
     self.initializer = initializers.GlorotNormal(scale=0.1)
Ejemplo n.º 19
0
    def __init__(self, ch):
        super(Inception_C, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch,
                                  256,
                                  ksize=1,
                                  stride=1,
                                  initialW=initialW,
                                  pad=0)

            self.convbn2_1 = ConvBN(ch,
                                    256,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    384,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2_1 = ConvBN(384,
                                      256,
                                      ksize=(1, 3),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(0, 1))
            self.convbn3_2_2 = ConvBN(384,
                                      256,
                                      ksize=(3, 1),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(1, 0))

            self.convbn4_1 = ConvBN(ch,
                                    384,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn4_2 = ConvBN(384,
                                    448,
                                    ksize=(1, 3),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(0, 1))
            self.convbn4_3 = ConvBN(448,
                                    512,
                                    ksize=(3, 1),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(1, 0))
            self.convbn4_4_1 = ConvBN(512,
                                      256,
                                      ksize=(3, 1),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(1, 0))
            self.convbn4_4_2 = ConvBN(512,
                                      256,
                                      ksize=(1, 3),
                                      stride=1,
                                      initialW=initialW,
                                      pad=(0, 1))
Ejemplo n.º 20
0
    def __init__(self, ch):
        super(Inception_B, self).__init__()
        initialW = initializers.GlorotNormal()
        with self.init_scope():
            self.convbn1 = ConvBN(ch,
                                  128,
                                  ksize=1,
                                  stride=1,
                                  initialW=initialW,
                                  pad=0)

            self.convbn2_1 = ConvBN(ch,
                                    384,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)

            self.convbn3_1 = ConvBN(ch,
                                    192,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn3_2 = ConvBN(192,
                                    224,
                                    ksize=(1, 7),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(0, 3))
            self.convbn3_3 = ConvBN(224,
                                    256,
                                    ksize=(7, 1),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(3, 0))

            self.convbn4_1 = ConvBN(ch,
                                    192,
                                    ksize=1,
                                    stride=1,
                                    initialW=initialW,
                                    pad=0)
            self.convbn4_2 = ConvBN(192,
                                    192,
                                    ksize=(1, 7),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(0, 3))
            self.convbn4_3 = ConvBN(192,
                                    224,
                                    ksize=(7, 1),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(3, 0))
            self.convbn4_4 = ConvBN(224,
                                    224,
                                    ksize=(1, 7),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(0, 3))
            self.convbn4_5 = ConvBN(224,
                                    256,
                                    ksize=(7, 1),
                                    stride=1,
                                    initialW=initialW,
                                    pad=(3, 0))