Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Add Sequential{N} typealiases. - #934

Merged
dan-zheng merged 2 commits into
tensorflow:masterfrom
dan-zheng:sequential-typealiases
May 6, 2020
Merged

dan-zheng merged 2 commits into
tensorflow:masterfrom
dan-zheng:sequential-typealiases

Conversation

@dan-zheng

@dan-zheng dan-zheng commented May 6, 2020 •

Copy link
Copy Markdown
Member

Add Sequential{N} generic typealiases.
These are easier to use than long nested Sequential types.

import TensorFlow
// Before:
typealias Model = Sequential<Dense<Float>, Sequential<Dense<Float>, Dense<Float>>>
// After:
typealias Model = Sequential3<Dense<Float>, Dense<Float>, Dense<Float>>

Add `Sequential{N}` generic typealiases.
These are easier to use than long nested `Sequential` types.
@dan-zheng

dan-zheng commented May 6, 2020 •

Copy link
Copy Markdown
Member Author

Generic typealiases with additional constraints aren't supported, so we can't land this anytime soon. I asked about potential support on the Swift forums.


In the meantime, we'll have to deal with long nested Sequential types:

var module: Sequential<Sequential<Conv2D<Float>, Sequential<Function<Tensor<Float>, Tensor<Float>>, Sequential<Conv2D<Float>, Sequential<BatchNorm<Float>, Sequential<Function<Tensor<Float>, Tensor<Float>>, Sequential<Conv2D<Float>, Sequential<BatchNorm<Float>, Function<Tensor<Float>, Tensor<Float>>>>>>>>>, Sequential<ConvLayer, Sequential<BatchNorm<Float>, Sequential<Function<Tensor<Float>, Tensor<Float>>, ConvLayer>>>>

An alternative to long nested Sequential types is type erasure:

@dan-zheng dan-zheng closed this May 6, 2020
The generic typealiases compile now!
@dan-zheng dan-zheng reopened this May 6, 2020
@dan-zheng

Copy link
Copy Markdown
Member Author

Hey, generic typealiases can have where clauses! So generic Sequential{N} typealiases do actually work.

Ready for review.

@dan-zheng
dan-zheng marked this pull request as ready for review May 6, 2020 16:48

@saeta saeta left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a nice improvement. Thanks!

@dan-zheng
dan-zheng merged commit 02a4170 into tensorflow:master May 6, 2020
@dan-zheng
dan-zheng deleted the sequential-typealiases branch May 6, 2020 17:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants