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))
def __init__(self, n_class: int, med_dim: int=1024): """Initialize.""" super(LBAL, self).__init__() with self.init_scope(): self.med_fc = links.Linear( None, med_dim, initialW=initializers.Normal(scale=0.01)) self.bn = links.BatchNormalization(med_dim) self.tail_fc = links.Linear( med_dim, n_class, initialW=initializers.Normal(scale=0.01))
def __init__(self, n_class: int, med_dim: int=1024, drop_rate: float=0.5) -> None: """Initialize.""" super(LADL, self).__init__() with self.init_scope(): self.med_fc = links.Linear( None, med_dim, initialW=initializers.Normal(scale=0.01)) self.tail_fc = links.Linear( med_dim, n_class, initialW=initializers.Normal(scale=0.01)) self.drop_rate = drop_rate
def __init__(self, n_in, n_out): super(QNetNout, self).__init__( lin=L.Linear(INPUT_LAYER_SIZE, MID1_LAYER_SIZE, initialW=initializers.Normal(scale=0.01)), lm1=L.Linear(MID1_LAYER_SIZE, MID2_LAYER_SIZE, initialW=initializers.Normal(scale=0.01)), lout=L.Linear(MID2_LAYER_SIZE, NUM_SLOTS, initialW=initializers.Normal(scale=0.01)), lq=L.Linear(2, 1, initialW=initializers.One()))
def __init__(self): super(QNet, self).__init__( lin=L.Linear(INPUT_LAYER_SIZE, MID1_LAYER_SIZE, initialW=initializers.Normal(scale=0.01)), lm1=L.Linear(MID1_LAYER_SIZE, MID2_LAYER_SIZE, initialW=initializers.Normal(scale=0.01)), lout=L.Linear(MID2_LAYER_SIZE, OUTPUT_LAYER_SIZE, initialW=initializers.Normal(scale=0.01)), )
def __init__(self, bert): super(BertPretrainer, self).__init__() with self.init_scope(): self.bert = bert self.masked_lm_dense = L.Linear(None, self.bert.config.hidden_size, initialW=initializers.Normal(scale=self.bert.config.initializer_range)) self.activate = get_activation('gelu') self.mask_bias = variable.Parameter(initializers.Zero(), shape=self.bert.config.vocab_size) self.next_sentence_weights = variable.Parameter( initializers.Normal(scale=self.bert.config.initializer_range), shape=(2, self.bert.config.hidden_size)) self.next_sentence_bias = variable.Parameter(initializers.Zero(), shape=2) self.layer_norm = LayerNormalization3D(None)
def __init__(self, insize, outsize): super(NN, self).__init__( # bn1=L.BatchNormalization(insize), layer1=L.Linear(insize, 110, initialW=initializers.Normal(scale=0.05)), layer2=L.Linear(110, 100, initialW=initializers.Normal(scale=0.05)), layer3=L.Linear(100, outsize, initialW=np.zeros((outsize, 100), dtype=np.float32)))
def __init__(self, vocab_size, hidden_size, dropout_ratio, ignore_label): super(NStepLSTMLanguageModel, self).__init__() with self.init_scope(): self.embed_word = L.EmbedID(vocab_size, hidden_size, initialW=initializers.Normal(1.0), ignore_label=ignore_label) self.embed_img = L.Linear(hidden_size, initialW=initializers.Normal(0.01)) self.lstm = L.NStepLSTM(1, hidden_size, hidden_size, dropout_ratio) self.decode_caption = L.Linear(hidden_size, vocab_size, initialW=initializers.Normal(0.01)) self.dropout_ratio = dropout_ratio
def __init__(self, in_ch, out_ch): w = initializers.Normal(0.02) super(ResBlock, self).__init__() with self.init_scope(): self.cbg0 = C1BG(in_ch, out_ch) self.c0 = L.Convolution1D(in_ch, in_ch, 3, 1, 1, initialW=w) self.bn0 = L.BatchNormalization(in_ch)
def __init__(self, dim_in, dim_out, kernel_size=3, activ=F.relu, wscale=0.02): super(pad_conv_norm, self).__init__() self.activ = activ w = I.Normal(wscale) with self.init_scope(): if kernel_size == 1: self.conv = L.Convolution2D(dim_in, dim_out, ksize=kernel_size, stride=1, pad=0, nobias=True, initialW=w) else: self.conv = L.Convolution2D(dim_in, dim_out, ksize=kernel_size, stride=1, pad=1, nobias=True, initialW=w) self.bn = L.BatchNormalization(dim_out, initial_gamma=I.Constant( np.random.normal(loc=1., scale=wscale, size=dim_out)))
def __init__(self, scale, cur_dim, num_resblock, wscale=0.02): super(Scale_Block, self).__init__() self.scale = scale self.num_resblock = num_resblock w = I.Normal(wscale) with self.init_scope(): if self.scale != 4: self.upsample = Up_Sapmling(cur_dim, cur_dim) if self.scale in reduce_dim_at: self.red_conv = pad_conv_norm(cur_dim, cur_dim // 2) cur_dim = cur_dim // 2 for i in range(self.num_resblock): setattr(self, f"resblock_{i}", ResnetBlock(cur_dim)) if self.scale in img_scales: self.tensor2img = L.Convolution2D(cur_dim, 3, ksize=3, stride=1, pad=1, initialW=w, initial_bias=w)
def __init__(self, n_units, n_out): super().__init__() with self.init_scope(): w = I.Normal(scale=1.0) self.l1 = L.Linear(None, n_units, initialW=w) self.l2 = L.Linear(None, n_units, initialW=w) self.l3 = L.Linear(None, n_out, initialW=w)
def __init__(self, sent_dim, noise_dim, row, col, channel, wscale=0.02, activ=None): super(Sent2FeatMap, self).__init__() self.row = row self.col = col self.channel = channel self.activ = activ w = I.Normal(wscale) with self.init_scope(): out_dim = row * col * channel self.l1_dense = L.Linear(sent_dim + noise_dim, out_dim, nobias=True, initialW=w) self.l1_bn = L.BatchNormalization(out_dim, initial_gamma=I.Constant( np.random.normal( loc=1., scale=wscale, size=out_dim)))
def __init__(self, in_ch, out_ch): w = initializers.Normal(0.02) super(RRDB,self).__init__() with self.init_scope(): self.dense0 = Dense_Block(in_ch, out_ch) self.dense1 = Dense_Block(out_ch, out_ch) self.dense2 = Dense_Block(out_ch, out_ch)
def __init__(self, base=64): w = initializers.Normal(0.02) super(Discriminator, self).__init__() with self.init_scope(): self.c0 = CBR(3, base, 4, 2, 1, down=True, activation=F.leaky_relu) self.c1 = CBR(base, base * 2, 4, 2, 1, down=True, activation=F.leaky_relu) self.c2 = CBR(base * 2, base * 4, 4, 2, 1, down=True, activation=F.leaky_relu) self.c3 = CBR(base * 4, base * 8, 4, 2, 1, down=True, activation=F.leaky_relu) self.c4 = L.Convolution2D(base * 8, 1, 1, 1, 0, initialW=w)
class BboxHead(chainer.Chain): _prior = 0.01 _conf_last_init = { 'initialW': initializers.Normal(0.01), 'initial_bias': initializers.Constant(-np.log((1 - _prior) / _prior)) } def __init__(self, n_fg_class, ratios, scales): super(BboxHead, self).__init__() with self.init_scope(): self.loc = create_fcn( (256, 256, 256, 256, 4 * len(ratios) * len(scales))) self.conf = create_fcn( (256, 256, 256, 256, (n_fg_class) * len(ratios) * len(scales)), [None, None, None, None, self._conf_last_init]) self._n_fg_class = n_fg_class def forward(self, hs): b = hs[0].shape[0] locs = [ F.reshape(F.transpose(self.loc(h), (0, 2, 3, 1)), (b, -1, 4)) for h in hs ] confs = [ F.reshape(F.transpose(self.conf(h), (0, 2, 3, 1)), (b, -1, self._n_fg_class)) for h in hs ] locs = F.concat(locs, axis=1) confs = F.concat(confs, axis=1) return locs, confs
def __init__(self, n_class: int, drop_rate: float=0.5): """Initialize.""" super(DropoutLinear, self).__init__() with self.init_scope(): self.tail_fc = links.Linear( None, n_class, initialW=initializers.Normal(scale=0.01)) self.drop_rate = drop_rate
def __init__(self, in_channels, out_channels, ksize, pooling, zoneout, bias, wgain=1.): """ Initialize ConvLSTM cell. Parameters ---------- input_size: (int, int) Height and width of input tensor as (height, width). input_dim: int Number of channels of input tensor. out_channels: int Number of channels of hidden state. ksize: (int, int, int) Size of the convolutional kernel. bias: bool Whether or not to add the bias. """ super(ConvQRNN, self).__init__() self.num_split = len(pooling) + 1 self._pooling = pooling # f or fo or ifo self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = ksize # typical is (3, 1, 1) self.bias = bias self._zoneout = zoneout self._using_zoneout = True if self._zoneout > 0 else False self.padding = (ksize[0]-1, ksize[1]//2, ksize[2]//2) wstd = math.sqrt(wgain / in_channels / ksize[0]) with self.init_scope(): self.conv = L.ConvolutionND(ndim=3, in_channels=self.in_channels, out_channels=self.num_split * self.out_channels, ksize=self.kernel_size, stride=1, pad=self.padding, nobias=not self.bias, initialW=initializers.Normal(wstd))
def __init__(self, n_class, scales): super(BboxHead, self).__init__() fc_init = { 'initialW': Caffe2FCUniform(), 'initial_bias': Caffe2FCUniform(), } with self.init_scope(): self.fc1 = L.Linear(1024, **fc_init) self.fc2 = L.Linear(1024, **fc_init) self.loc = L.Linear(n_class * 4, initialW=initializers.Normal(0.001)) self.conf = L.Linear(n_class, initialW=initializers.Normal(0.01)) self._n_class = n_class self._scales = scales
def __init__(self, n_units, n_vocab, encoder, max_memory, hops): super(MemNN, self).__init__() with self.init_scope(): self.embeds = chainer.ChainList() self.temporals = chainer.ChainList() normal = initializers.Normal() # Shares both embeded matrixes in adjacent layres for _ in six.moves.range(hops + 1): self.embeds.append(L.EmbedID(n_vocab, n_units, initialW=normal)) self.temporals.append( L.EmbedID(max_memory, n_units, initialW=normal)) self.memories = [ Memory(self.embeds[i], self.embeds[i + 1], self.temporals[i], self.temporals[i + 1], encoder) for i in six.moves.range(hops) ] # The question embedding is same as the input embedding of the # first layer self.B = self.embeds[0] # The answer prediction matrix W is same as the final output layer self.W = lambda u: F.linear(u, self.embeds[-1].W) self.encoder = encoder self.n_units = n_units self.max_memory = max_memory self.hops = hops
def __init__(self, emb_dim, n_out, depth=[2, 2, 2, 2]): super(VDCNN, self).__init__() with self.init_scope(): self.embed = L.EmbedID(emb_dim, 16, initializers.Normal(), ignore_label=-1) self.conv1 = L.ConvolutionND(1, 16, 64, 3, 1, 1, initialW=initializers.HeUniform(), initial_bias=initializers.Uniform( np.sqrt(6 / (16 * 3)))) self.cb2 = BuildBlock(depth[0], 64, 128) self.cb3 = BuildBlock(depth[1], 128, 256) self.cb4 = BuildBlock(depth[2], 256, 512) self.cb5 = BuildBlock(depth[3], 512, 512) self.fc6 = L.Linear( 4096, 2048, initialW=initializers.Uniform(1 / np.sqrt(4096)), initial_bias=initializers.Uniform(1 / np.sqrt(4096))) self.fc7 = L.Linear( 2048, 2048, initialW=initializers.Uniform(1 / np.sqrt(2048)), initial_bias=initializers.Uniform(1 / np.sqrt(2048))) self.fc8 = L.Linear( 2048, n_out, initialW=initializers.Uniform(1 / np.sqrt(2048)), initial_bias=initializers.Uniform(1 / np.sqrt(2048)))
def __init__(self, ch_in, ch_out, stride=1, activation=F.relu, skip_ratio=0): initializer = initializers.Normal(scale=math.sqrt(2.0 / (ch_out * 3 * 3))) super(PyramidBlock, self).__init__( conv1=L.Convolution2D(ch_in, ch_out, 3, stride, 1, initialW=initializer), conv2=L.Convolution2D(ch_out, ch_out, 3, 1, 1, initialW=initializer), bn1=L.BatchNormalization(ch_in), bn2=L.BatchNormalization(ch_out), bn3=L.BatchNormalization(ch_out), ) self.activation = activation self.skip_ratio = skip_ratio
def __init__(self): super().__init__() init_w = initializers.Normal(0.1) init_b = initializers.Constant(0.1) with self.init_scope(): self.fc1 = L.Linear(7*7*48, 100, initialW=init_w, initial_bias=init_b) self.fc2 = L.Linear(100, 1, initialW=init_w, initial_bias=init_b)
def __init__(self, n_layer, n_class=None, pretrained_model=None, mean=None, initialW=None, fc_kwargs={}, arch='fb'): if arch == 'fb': stride_first = False conv1_no_bias = True elif arch == 'he': stride_first = True # Kaiming He uses bias only for ResNet50 conv1_no_bias = n_layer != 50 else: raise ValueError('arch is expected to be one of [\'he\', \'fb\']') blocks = self._blocks[n_layer] param, path = utils.prepare_pretrained_model( { 'n_class': n_class, 'mean': mean }, pretrained_model, self._models[arch][n_layer], { 'n_class': 1000, 'mean': _imagenet_mean }) self.mean = param['mean'] if initialW is None: initialW = initializers.HeNormal(scale=1., fan_option='fan_out') if 'initialW' not in fc_kwargs: fc_kwargs['initialW'] = initializers.Normal(scale=0.01) if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. initialW = initializers.constant.Zero() fc_kwargs['initialW'] = initializers.constant.Zero() kwargs = {'initialW': initialW, 'stride_first': stride_first} super(ResNet, self).__init__() with self.init_scope(): self.conv1 = Conv2DBNActiv(None, 64, 7, 2, 3, nobias=conv1_no_bias, initialW=initialW) self.pool1 = lambda x: F.max_pooling_2d(x, ksize=3, stride=2) self.res2 = ResBlock(blocks[0], None, 64, 256, 1, **kwargs) self.res3 = ResBlock(blocks[1], None, 128, 512, 2, **kwargs) self.res4 = ResBlock(blocks[2], None, 256, 1024, 2, **kwargs) self.res5 = ResBlock(blocks[3], None, 512, 2048, 2, **kwargs) self.pool5 = lambda x: F.average(x, axis=(2, 3)) self.fc6 = L.Linear(None, param['n_class'], **fc_kwargs) self.prob = F.softmax if path: chainer.serializers.load_npz(path, self)
def __init__(self, base=128): w = initializers.Normal(0.02) super(GeneratorWithCIN, self).__init__() with self.init_scope(): self.c0 = L.Convolution2D(1, base, (15, 5), 1, (7, 2), initialW=w) self.cbg0 = C2BG(int(base / 2), base * 2, down=True) self.cbg1 = C2BG(base, base * 4, down=True) self.c1 = L.Convolution1D(2304, base * 2, 1, 1, 0, initialW=w) self.bn1 = L.BatchNormalization(base * 2) self.res0 = ResBlock(base * 2, base * 4) self.res1 = ResBlock(base * 2, base * 4) self.res2 = ResBlock(base * 2, base * 4) self.res3 = ResBlock(base * 2, base * 4) self.res4 = ResBlock(base * 2, base * 4) self.res5 = ResBlock(base * 2, base * 4) self.res6 = ResBlock(base * 2, base * 4) self.res7 = ResBlock(base * 2, base * 4) self.res8 = ResBlock(base * 2, base * 4) self.c2 = L.Convolution1D(base * 2, 2304, 1, 1, 0, initialW=w) self.bn2 = L.BatchNormalization(2304) self.cbg2 = C2BG(base * 2, base * 8, up=True) self.cbg3 = C2BG(base * 4, 72, up=True) self.c3 = L.Convolution2D(36, 1, 3, 1, 1, initialW=w)
def __init__(self, in_size, out_size): super(Linear, self).__init__() with self.init_scope(): self.W = chainer.Parameter( initializers.Normal(1. / math.sqrt(in_size)), (out_size, in_size)) self.b = chainer.Parameter(0, (out_size, ))
def __init__(self, n_in, n_out): super(NN, self).__init__( L1=L.Linear(n_in, 100), L2=L.Linear(100, 100), L3=L.Linear(100, 100), Q_value=L.Linear(100, n_out, initialW=initializers.Normal(scale=0.05)) )
def __init__(self, in_ch, out_ch, kernel, stride, padding, activation=F.relu, down=False, up=False): w = initializers.Normal(0.02) super(CBR, self).__init__() self.activ = activation self.down = down self.up = up with self.init_scope(): self.c = L.Convolution2D(in_ch, out_ch, kernel, stride, padding, initialW=w) self.bn = L.BatchNormalization(out_ch)
def Convolution(in_channels, out_channels, ksize, stride, pad): return L.Convolution2D(in_channels, out_channels, ksize, stride, pad, initialW=initializers.Normal(0.02))
def __init__(self, n_units, n_out): w = I.Normal(scale=0.05) # モデルパラメータの初期化 super(MLP, self).__init__( conv1=L.Convolution2D(1, 16, 5, 1, 0), # 1層目の畳み込み層(フィルタ数は16) conv2=L.Convolution2D(16, 32, 5, 1, 0), # 2層目の畳み込み層(フィルタ数は32) l3=L.Linear(None, n_out, initialW=w), #クラス分類用 )