简体   繁体   中英

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?

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, eg build_model .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM