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] ParametrizedLayer/TrainableLayer interfaces to represent presence of parameters and trainability of the layer #217

Closed
knok16 opened this issue Sep 8, 2021 · 2 comments · Fixed by #345
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@knok16
Copy link
Contributor

knok16 commented Sep 8, 2021

Hello, what about moving some aspects of Layer logic into separate interfaces, to offload Layer class?

For example, introduce ParametrizedLayer/TrainableLayer interfaces to encapsulate isTranable / paramCount logic and will be implemented only by layers that actually have variables/can be trained

public interface ParametrizedLayer {
    /**
     * Layer's variables
     */
    public val variables: List<Variable>

    /**
     * Returns amount of parameters
     */
    public val ParametrizedLayer.paramCount: Int
        get() = variables.sumOf { it.shape.numElements() }.toInt()
}

public interface TrainableLayer : ParametrizedLayer {
    /**
     * True, if layer's weights could be changed during training.
     * If false, layer's weights are frozen and could not be changed during the training.
     */
    public var isTrainable: Boolean
}

Benefits:

  • Layer class itself will become thinner and hence easier to grasp
  • Untrainable by nature (for example Flatten,Input,ActivationLayer) layers will not even have isTrainable flag and weight field
  • Main idea here is that the code Flatten().isTrainable = true logically doesn't make sense, and with ParametrizedLayer it will not compile
  • Instead of marking layers with NoGradient it will be possible just not to implement Trainable which will work as whitelisting for training (instead of current blacklisting) which is more explicit.
  • It will be possible to remove KGraph parameter from Layer::build method
  • It will be possible to move weight setter/getter to the model (example knok16@4dc0286)

Reference knok16@f5244b6

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

I'd like this idea in common, but suggest postponing for a 0.4 release.
What is about arranging a call to discuss the proposals? Could you write me on kotlinlang slack if you are ready to talk about it?

@zaleslaw zaleslaw changed the title Additional typing for layers [API] Additional typing for layers Sep 15, 2021
@knok16 knok16 changed the title [API] Additional typing for layers [API] [Layers] ParametrizedLayer/TrainableLayer interfaces to represent presence of parameters and trainability of the layer Sep 18, 2021
@zaleslaw
Copy link
Collaborator

zaleslaw commented Oct 18, 2021

I also support this great proposal

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

Successfully merging a pull request may close this issue.

3 participants