swift-apis icon indicating copy to clipboard operation
swift-apis copied to clipboard

Parameter counting API for Layer

Open t-ae opened this issue 6 years ago • 6 comments
trafficstars

Currently we can't easily know how many parameters Layer instance has. It'll be useful for estimating model size.

Keras's Layer has this feature. https://github.com/keras-team/keras/blob/c10d24959b0ad615a21e671b180a1b2466d77a2b/keras/engine/base_layer.py#L1105-L1123

t-ae avatar Aug 02 '19 19:08 t-ae

This can be done once we flesh out the property wrapper solution for parameters (along the lines of #250).

rxwei avatar Aug 03 '19 06:08 rxwei

I have been counting parameters based on how variables used to be updated in optimisers:

extension Layer {
    var parameterCount: Int {
        let floatKeyPaths = self.recursivelyAllWritableKeyPaths(to: Tensor<Float>.self)
        let doubleKeyPaths = self.recursivelyAllWritableKeyPaths(to: Tensor<Double>.self)
        var parameters = 0         
        for kp in floatKeyPaths {
           parameters += self[keyPath: kp].shape.contiguousSize
        }
        for kp in doubleKeyPaths {
            parameters += self[keyPath: kp].shape.contiguousSize
        }
        return parameters
    }
}

Is there anything inherently wrong in doing it this way or is it just not ideal as more variable types get used?

mikowals avatar Aug 03 '19 10:08 mikowals

One problem with this approach is duplication for each variable type as you mentioned. A bigger problem is that you might include tensors that are not parameters of a model, e.g. ones that are marked @noDerivative.

rxwei avatar Aug 05 '19 23:08 rxwei

Thanks for explaining this Richard.

mikowals avatar Aug 06 '19 00:08 mikowals

@mikowals @rxwei another problem is that you might not include tensors that are parameters. E.g., different data types or potentially, arrays of tensors (I believe this would not account for tensor arrays).

eaplatanios avatar Aug 06 '19 14:08 eaplatanios

I'd like to work on this one. Could you please assign this to me? :slightly_smiling_face:

Kshitij09 avatar Mar 28 '20 10:03 Kshitij09