使用 keras 模型对函数进行 Python 类型提示

Mar*_*tin 5 python type-hinting keras

如果我创建这样的函数:

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
Run Code Online (Sandbox Code Playgroud)

我非常关心良好的编程实践,我应该如何指示函数的返回类型?

Bra*_*roy 9

def mdl(input_shape) -> Sequential:
Run Code Online (Sandbox Code Playgroud)

您可能还想输入 input_shape。我猜它是一个整数元组,所以:

def mdl(input_shape: Tuple[int, ...]) -> Sequential:
Run Code Online (Sandbox Code Playgroud)

如果您对最佳实践感兴趣,您可能还想使用更好、更具语义的函数名称,例如build_model