Kotlin / kotlindl

High-level Deep Learning Framework written in Kotlin and inspired by Keras
Apache License 2.0
1.47k stars 104 forks source link

[API] [Layers] introduce separate Layer's subclasses to represent number of layer's inputs #224

Open knok16 opened 3 years ago

knok16 commented 3 years ago
public sealed class Layer

public abstract class NoInputsLayer : Layer() {
    public abstract fun build(tf: Ops)

    public abstract fun computeOutputShape(): Shape

    public abstract fun forward(
        tf: Ops,
        isTraining: Operand<Boolean>,
        numberOfLosses: Operand<Float>?
    ): Operand<Float>
}

public abstract class SingleInputLayer : Layer() {
    public abstract fun build(tf: Ops, inputShape: Shape)

    public abstract fun computeOutputShape(inputShape: Shape): Shape

    public abstract fun forward(
        tf: Ops,
        input: Operand<Float>,
        isTraining: Operand<Boolean>,
        numberOfLosses: Operand<Float>?
    ): Operand<Float>

    public operator fun invoke(layer: Layer): Layer
}

public abstract class MultipleInputsLayer : Layer() {
    public abstract fun build(tf: Ops, inputShapes: List<Shape>)

    public abstract fun computeOutputShape(inputShapes: List<Shape>): Shape

    public abstract fun forward(
        tf: Ops,
        input: List<Operand<Float>>,
        isTraining: Operand<Boolean>,
        numberOfLosses: Operand<Float>?
    ): Operand<Float>

    public operator fun invoke(layer1: Layer, layer2: Layer, vararg otherLayers: Layer): Layer
}

Benefits:

Reference https://github.com/knok16/KotlinDL/commit/8643a87d7c983920d0b7434a3d2d9917f0e9aefc

zaleslaw commented 3 years ago

It's a great proposal, please prepare a PR for that.

I have one minor note for that: I suppose the basic class Layer should not be sealed at all, because we need to give the ability to write a custom Layer class in user code.