pythonkeraspython-typing

Python type hinting for function with keras model


If I create function like this:

def mdl(input_shape):

    model = Sequential()
    model.add(Conv2D(depth=64, kernel_size=(3, 3), input_shape=input_shape, activation='relu'))
    model.add(Dense(32), activation='relu')
    model.add(Dropout(0.3))
    model.add(Dense(32), activation='relu')
    model.add(Dropout(0.3))
    model.add(Dense(16), activation='relu')
    model.add(Dropout(0.3))
    model.add(Dense(1))

    return model

and I care a lot about good programming practices, how should I indicate returning type of the function?


Solution

  • def mdl(input_shape) -> Sequential:
    

    You probably also want to type the input_shape. I am guessing it is a tuple of ints, so:

    def mdl(input_shape: Tuple[int, ...]) -> Sequential:
    

    If you are interested in best practice, you may want to use a better, more semantic function name as well, e.g. build_model.