def test_embedding_block(): input_shape = (32, ) block = block_module.EmbeddingBlock() hp = kerastuner.HyperParameters() block.build(hp, ak.Input(shape=input_shape).build()) assert name_in_hps('pretraining', hp) assert name_in_hps('embedding_dim', hp)
def test_embedding_block(): input_shape = (32,) block = block_module.EmbeddingBlock() block.max_features = 100 block.set_state(block.get_state()) hp = kerastuner.HyperParameters() block.build(hp, ak.Input(shape=input_shape).build()) assert common.name_in_hps('pretraining', hp) assert common.name_in_hps('embedding_dim', hp)
def assemble(self, input_node): output_node = input_node ratio = self.sw_ratio() if not isinstance(input_node, node.TextNode): raise ValueError('The input_node should be a TextNode.') if ratio < 1500: output_node = processor.TextToNgramVector()(output_node) output_node = block.DenseBlock()(output_node) else: output_node = processor.TextToIntSequence()(output_node) output_node = block.EmbeddingBlock( pretrained=(ratio < 15000))(output_node) output_node = block.ConvBlock(separable=True)(output_node) return output_node
def build(self, hp, inputs=None): input_node = nest.flatten(inputs)[0] output_node = input_node vectorizer = self.vectorizer or hp.Choice( 'vectorizer', ['sequence', 'ngram'], default='sequence') if not isinstance(input_node, node.TextNode): raise ValueError('The input_node should be a TextNode.') if vectorizer == 'ngram': output_node = preprocessor.TextToNgramVector()(output_node) output_node = block.DenseBlock()(output_node) else: output_node = preprocessor.TextToIntSequence()(output_node) output_node = block.EmbeddingBlock( pretraining=self.pretraining)(output_node) output_node = block.ConvBlock(separable=True)(output_node) return output_node
def test_embedding_block_with_pretraining(get_file, load_embedding_index): load_embedding_index.return_value = {'test': np.ones((100, ))} get_file.return_value = '' input_shape = (32, ) block = block_module.EmbeddingBlock(pretraining='glove') block.max_features = 2 block.word_index = {'test': 1} block.set_state(block.get_state()) hp = kerastuner.HyperParameters() block.build(hp, ak.Input(shape=input_shape).build()) embedding_matrix = block._build_embedding_matrix('glove') assert np.array_equal(embedding_matrix[1], np.ones((100, ))) assert not common.name_in_hps('pretraining', hp) assert not common.name_in_hps('embedding_dim', hp)