Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
knok16 opened this issue Sep 18, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@knok16
Copy link
Contributor

knok16 commented Sep 18, 2021

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:

  • Main benefit is that the arity of a layer will be checked by compiler
  • Using Functional builder it will not be possible to write the next code:
    • Add()(x) - there is no a point in adding single input
    • Softmax()(x, y, z) - Softmax layer accept a single input layer and in current implementation will just ignore y and z layers - which can create confusion
  • Layers will not have methods that doesn't make sense for them (for example computeInputShapeFromIncomingLayers for Input layer)
  • Sequential builder can be restricted to accept only SingleInputLayer layers since it doesn't make to pass multiple input layers there

Reference knok16@8643a87

@zaleslaw zaleslaw added the enhancement New feature or request label Oct 18, 2021
@zaleslaw zaleslaw added this to the 0.4 milestone Oct 18, 2021
@zaleslaw
Copy link
Collaborator

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.

@zaleslaw zaleslaw modified the milestones: 0.4, 0.5 Feb 25, 2022
@zaleslaw zaleslaw assigned juliabeliaeva and unassigned knok16 Feb 25, 2022
@ermolenkodev ermolenkodev removed this from the 0.5 milestone Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants