def __init__(self, **kwargs): super().__init__(**kwargs) self.nfeat_factor = kwargs.get("nfeat_factor") self.cfiltsz = kwargs.get("cfiltsz", kwargs.get("filtsz", [3])) self.max_feat = kwargs.get("max_feat", 30) self.gating = kwargs.get("gating", "skip") self.num_gates = kwargs.get("num_gates", 1) self.activation = kwargs.get("activation", "tanh") self.wsz = kwargs.get("wsz", 30) self.projsz = kwargs.get("projsz", 0) self.pdrop = kwargs.get("pdrop", 0.5) self.filtsz, self.nfeats = calc_nfeats(self.cfiltsz, self.nfeat_factor, self.max_feat, self.wsz) self.conv_outsz = int(np.sum(self.nfeats)) self.outsz = self.conv_outsz if self.projsz > 0: self.outsz = self.projsz self.proj = pytorch_linear(self.conv_outsz, self.outsz) self.embeddings = LookupTableEmbeddings(**kwargs) self.char_comp = WithDropout( ParallelConv(self.embeddings.output_dim, self.nfeats, self.filtsz, self.activation), self.pdrop) GatingConnection = SkipConnection if self.gating == "skip" else Highway self.gating_seq = nn.Sequential( OrderedDict([("gate-{}".format(i), GatingConnection(self.char_comp.output_dim)) for i in range(self.num_gates)]))
def __init__(self, trainable=True, name=None, dtype=tf.float32, **kwargs): trainable = kwargs.get("finetune", trainable) super().__init__(trainable=trainable, name=name, dtype=dtype) self._name = name self.scope = kwargs.get("scope", "CharConv") self.finetune = kwargs.get("finetune", trainable) self.nfeat_factor = kwargs.get("nfeat_factor", None) self.cfiltsz = kwargs.get("cfiltsz", kwargs.get("filtsz", [3])) self.max_feat = kwargs.get("max_feat", 30) self.gating = kwargs.get("gating", "skip") self.num_gates = kwargs.get("num_gates", 1) self.activation = kwargs.get("activation", "tanh") self.wsz = kwargs.get("wsz", 30) self.projsz = kwargs.get("projsz") self.x = None # These are the actual final filter sizes and num features self.filtsz, self.nfeats = calc_nfeats(self.cfiltsz, self.nfeat_factor, self.max_feat, self.wsz) self.conv_outsz = np.sum(self.nfeats) self.outsz = self.conv_outsz if self.projsz is not None: self.outsz = self.projsz self.proj = tf.keras.layers.Dense( self.outsz, bias_initializer=tf.constant_initializer(0.0)) self.embed = LookupTableEmbeddings(name=f"{self.name}/CharLUT", finetune=self.finetune, **kwargs)
def __init__(self, trainable=True, name=None, dtype=tf.float32, **kwargs): trainable = kwargs.get("finetune", trainable) super().__init__(trainable=trainable, name=name, dtype=dtype, **kwargs) self.cpu_placement = bool(kwargs.get('cpu_placement', False)) self.scope = kwargs.get("scope", "CharConv") self.finetune = kwargs.get("finetune", trainable) self.nfeat_factor = kwargs.get("nfeat_factor", None) self.cfiltsz = kwargs.get("cfiltsz", kwargs.get("filtsz", [3])) self.max_feat = kwargs.get("max_feat", 30) gating = kwargs.get("gating", "skip") num_gates = kwargs.get("num_gates", 1) activation = kwargs.get("activation", "tanh") self.wsz = kwargs.get("wsz", 30) self.projsz = kwargs.get("projsz") self.x = None # These are the actual final filter sizes and num features filtsz, nfeats = calc_nfeats(self.cfiltsz, self.nfeat_factor, self.max_feat, self.wsz) self.embed = LookupTableEmbeddings(name=f"{self.name}/CharLUT", finetune=self.finetune, **kwargs) dsz = self.embed.output_dim self.parallel_conv = ParallelConv(dsz, nfeats, filtsz, activation) self.gating_fns = tf.keras.Sequential() for _ in range(num_gates): if gating == 'skip': self.gating_fns.add(SkipConnection(self.parallel_conv.output_dim, activation)) else: self.gating_fns.add(Highway(self.parallel_conv.output_dim)) self.outsz = self.parallel_conv.output_dim if self.projsz is not None: self.outsz = self.projsz self.proj = tf.keras.layers.Dense(self.outsz, bias_initializer=tf.constant_initializer(0.0))
def test_feat_factor_capped(): gold_filtsz = [1, 2, 3, 4, 5] feat_factor = 10 gold_nfeats = [10, 20, 30, 30, 30] filtsz, nfeats = calc_nfeats(gold_filtsz, feat_factor, 30) assert filtsz == gold_filtsz assert nfeats == gold_nfeats
def test_feat_factor_manual(): gold_filtsz = [1, 2, 3, 4, 5] feat_factor = 10 gold_nfeats = [10, 20, 30, 40, 50] filtsz, nfeats = calc_nfeats(gold_filtsz, feat_factor, float("Infinity")) assert filtsz == gold_filtsz assert nfeats == gold_nfeats
def test_feat_factor(): gold_filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] feat_factor = random.randint(10, 25) max_feat = random.randint(30, 40) gold_nfeats = np.minimum(np.array(gold_filtsz) * feat_factor, max_feat) filtsz, nfeats = calc_nfeats(gold_filtsz, feat_factor, max_feat) np.testing.assert_equal(filtsz, gold_filtsz) np.testing.assert_equal(nfeats, gold_nfeats)
def test_extract_tuple(): filt_feat = [(random.randint(1, 10), random.randint(10, 20)) for _ in range(random.randint(2, 6))] gold_filtsz = tuple(filter_and_size[0] for filter_and_size in filt_feat) gold_nfeats = tuple(filter_and_size[1] for filter_and_size in filt_feat) filtsz, nfeats = calc_nfeats(filt_feat) assert filtsz == gold_filtsz assert nfeats == gold_nfeats
def test_feat_factor_max_none(): filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] feat_factor = 10 with pytest.raises(AssertionError): calc_nfeats(filtsz, nfeat_factor=feat_factor, max_feat=None)
def test_use_nfeats(): filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] input_nfeat = random.randint(1, 100) gold_nfeats = [input_nfeat] * len(filtsz) _, nfeat = calc_nfeats(filtsz, None, None, nfeats=input_nfeat) assert nfeat == gold_nfeats
def test_use_nfeats_list(): filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] nfeats = [random.randint(1, 10) for _ in range(len(filtsz))] with pytest.raises(AssertionError): _, nfeat = calc_nfeats(filtsz, None, None, nfeats=nfeats)
def test_use_nfeats_none(): filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] with pytest.raises(AssertionError): calc_nfeats(filtsz)
def test_use_nfeats_filtsz_unchanged(): gold_filtsz = [random.randint(1, 10) for _ in range(random.randint(2, 6))] nfeat = random.randint(1, 100) filtsz, _ = calc_nfeats(gold_filtsz, None, None, nfeats=nfeat) assert filtsz == gold_filtsz