Customized Model

In this tutorial, we show how to customize your search space with
AutoModel and how to implement your own block as search space.
This API is mainly for advanced users who already know what their model should look like.

Customized Search Space

First, let us see how we can build the following neural network using the building blocks in AutoKeras.

Whild building the model, the blocks used need to follow this topology:
Preprocessor -> Block -> Head. Normalization and ImageAugmentation are Preprocessors.
ClassificationHead is Head. The rest are Blocks.

In the code above, we use ak.ResNetBlock(version='v2') to specify the version of ResNet to use.
There are many other arguments to specify for each building block.
For most of the arguments, if not specified, they would be tuned automatically.
Please refer to the documentation links at the bottom of the page for more details.

Implement New Block

You can extend the Block
class to implement your own building blocks and use it with
AutoModel.

The first step is to learn how to write a build function for KerasTuner.
You need to override the build function of the block.
The following example shows how to implement a single Dense layer block whose number of neurons is tunable.

importautokerasasakimporttensorflowastfclassSingleDenseLayerBlock(ak.Block):defbuild(self,hp,inputs=None):# Get the input_node from inputs.input_node=tf.python.util.nest.flatten(inputs)[0]layer=tf.keras.layers.Dense(hp.Int('num_units',min_value=32,max_value=512,step=32))output_node=layer(input_node)returnoutput_node