-
Notifications
You must be signed in to change notification settings - Fork 137
Add Avgpool3d and Maxpool3d Layers & Tests #117
Conversation
@Shashi456 yes you can overload init, and you can have one init delegate to another init as well. Having convenience initializers to simplify common cases is a good idea. |
Can you explain what you mean by delegate? @saeta |
Great question! In the Dense layer, we have one initializer call a different initializer to reduce duplication of code: https://github.com/tensorflow/swift-apis/blob/master/Sources/DeepLearning/Layer.swift#L245 Does that make sense? |
Oh that's pretty cool. So you mean in this case, something like:
In similar fashion? |
Yes, and you can even refactor the 2 initializers you currently have in the PR into one delegating (calling) the other. :-) |
You mean initialization delegation is possible across functions? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add tests to Tests/DeepLearningTests/LayerTests.swift
?
Let's try to maintain tests for every layer!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Please also address comments above (add tests).
@rxwei so the things i have to incorporate is :
would that be it? Also i had a doubt, to init function in the case of |
@dan-zheng @saeta @rxwei please review! |
Sources/DeepLearning/Layer.swift
Outdated
public extension AvgPool3D { | ||
/// Creates a AvgPool3D Layer with the specified poolsize and strides. The poolsize | ||
/// and strides are square windows. | ||
init(poolSize: Int, stride: Int, padding: Padding = .valid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please be mindful of the following:
- Use 4-space indentation.
- Wrap
{
onto the previous line. - Local variable bindings like
let poolsize = poolSize
are not meaningful, use the argumentpoolSize
directly.
I hope those points make sense! Being mindful of them helps us merge your PR more quickly.
Please use the following:
public extension AvgPool3D {
/// Creates an average pooling layer with the specified pooling window size and stride. All
/// pooling sizes and strides are the same.
init(poolSize: Int, strides: Int, padding: Padding = .valid) {
self.init(poolSize: (poolSize, poolSize, poolSize),
strides: (strides, strides, strides),
padding: padding)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it does. I will pay heed from the next time, Sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also @dan-zheng can you think of any other variations of inits and should i extend an init for 2d max and average pooling as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this API is not comprehensive: both pooling window sizes and strides can either be specified as "multiple integers" or "one integer, implicitly duplicated".
In other words, all of the following may be useful:
init(poolSize: (Int, Int, Int), strides: (Int, Int, Int), padding: Padding)
init(poolSize: Int, strides: Int, padding: Padding)
init(poolSize: (Int, Int, Int), strides: Int, padding: Padding)
init(poolSize: Int, strides: (Int, Int, Int) padding: Padding)
Something to think about and visit after this PR.
Here's Keras (note "integer or tuple of two integers"):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While writing the code for this, i thought about exactly this case, but had no idea how to address it exactly. In python, It is easier to handle. I'm not very well versed with init overloading in swift
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it does. I will pay heed from the next time, Sorry!
Thanks a ton! That would be a huge help. 🙂
Also @dan-zheng can you think of any other variations of inits and should i extend an init for 2d max and average pooling as well?
I commented above about this.
To match Keras, we could have the following combinations:
poolSize
:(Int, Int, Int)
orInt
strides
:(Int, Int, Int)
orInt?
(ifnil
, then usepoolSize
as strides)
I would recommend adding such initializers in a follow-up to this PR, to make API additions more incremental.
@dan-zheng thank you for being so patient with these reviews. |
@rxwei can you review this once more? |
This should not have been merged because it does not compile. The @Shashi456 Could you make sure everything compiles with the latest toolchain before you commit? |
I reverted it in #118. Feel free to start a new PR when you have a fix. |
@rxwei when i just try to simply add a layer in colab, to get an idea of how the layer works. It often throws me this error, could you tell me how i can solve this ?
|
This is sometimes because of missing |
Is there a temporary fix I could do @rxwei? |
Could you share a code example that I can use to reproduce your error? |
@rxwei for example, if i use this code, I know this could also be because max is not differentiable, but this input output error is common with any layer i try to define.
|
Colab is not yet updated with the latest APIs (v0.3), and that is causing your errors. You can follow the instructions here to install a new build to Colab. |
Thanks a lot ! I will try and see if this works. |
Actually, it should be updated now if you restart Colab. |
@rxwei the error majorly seems to be :
|
Before adding tests i wanted to ask, @rxwei can we overload init? Because what we currently support in the max pooling and avg pooling layers is non-square windows, for square windows someone would have to write the layer as
MaxPool3d(poolsize:(3,3,3), stride:(2,2,2)
Is there any way to work around that?