예제 #1
0
    def build(self, hp, inputs=None):
        if self.identity:
            return IdentityLayer(name=self.name)(inputs)
        if self.num_classes:
            expected = self.num_classes if self.num_classes > 2 else 1
            if self.output_shape[-1] != expected:
                raise ValueError('The data doesn\'t match the expected shape. '
                                 'Expecting {} but got {}'.format(
                                     expected, self.output_shape[-1]))
        inputs = nest.flatten(inputs)
        utils.validate_num_inputs(inputs, 1)
        input_node = inputs[0]
        output_node = input_node

        if len(output_node.shape) > 2:
            dropout_rate = self.dropout_rate or hp.Choice(
                'dropout_rate', [0.0, 0.25, 0.5], default=0)
            if dropout_rate > 0:
                output_node = tf.keras.layers.Dropout(dropout_rate)(
                    output_node)
            output_node = block_module.SpatialReduction().build(
                hp, output_node)
        output_node = tf.keras.layers.Dense(self.output_shape[-1])(output_node)
        if self.loss == 'binary_crossentropy':
            output_node = Sigmoid(name=self.name)(output_node)
        else:
            output_node = tf.keras.layers.Softmax(name=self.name)(output_node)
        return output_node
예제 #2
0
def test_spatial_reduction():
    input_shape = (32, 32, 3)
    block = block_module.SpatialReduction()
    hp = kerastuner.HyperParameters()

    block.build(hp, ak.Input(shape=input_shape).build())

    assert name_in_hps('reduction_type', hp)
예제 #3
0
 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_module.TextNode):
         raise ValueError('The input_node should be a TextNode.')
     if vectorizer == 'ngram':
         output_node = preprocessor_module.TextToNgramVector()(output_node)
         output_node = block_module.DenseBlock()(output_node)
     else:
         output_node = preprocessor_module.TextToIntSequence()(output_node)
         output_node = block_module.EmbeddingBlock(
             pretraining=self.pretraining)(output_node)
         output_node = block_module.ConvBlock(separable=True)(output_node)
         output_node = block_module.SpatialReduction()(output_node)
         output_node = block_module.DenseBlock()(output_node)
     return output_node
예제 #4
0
 def assemble(self, input_node):
     block = hyperblock.ImageBlock(seed=self.seed)
     if max(self._shape[0], self._shape[1]) < 32:
         if self._num_samples < 10000:
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/v1/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/v2/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Choice(
                             block.name + '_resnet/next/conv4_depth', [6],
                             default=6))
             self.hps.append(hp_module.Int(
                             block.name + '_xception/num_residual_blocks', 2, 4,
                             default=4))
     output_node = block(input_node)
     output_node = block_module.SpatialReduction()(output_node)
     return block_module.DenseBlock()(output_node)