-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add convert_to_tensor
to tensorflow
#11292
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
Add convert_to_tensor
to tensorflow
#11292
Conversation
This comment has been minimized.
This comment has been minimized.
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.
One small comment. Otherwise if you'd like to add tf stubs I'm happy to review, with one caveat it may help if I give you remaining tf stubs I have.
The current typeshed tf stubs are subset of ones I use, but haven't spent much time in open source recently and each time I add stubs to typeshed I also do a pass to clean them up so some discrepancies have accumulated. The stubs I have do contain a lot more files/missing classes though and can be a good starting point.
https://github.com/hmc-cs-mdrissi/tensorflow_stubs I've placed all of tensorflow internal stubs I have in this public repository. I'd be happy to see them incorporated to typeshed and some of classes you want like Model do have stubs there. These stubs have diverged a little from typeshed final ones, but they are very similar. |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
@hmc-cs-mdrissi Thank you for the review, and thank you for sharing your stubs! I don't have a huge codebase to test against, so the stubs I made are incomplete and too restrictive. I'll start by completing/fixing what I already have here using your stubs if that's ok with you. Or if there's a way I can help get your stubs incorporated to typeshed, please let me know! On a bit of an unrelated note, I have two questions about how you're using your stubs (or the ones in typeshed):
I just had to switch from PyTorch to TensorFlow, so any advice on how to type a TensorFlow codebase would be extremely welcome. |
Concrete tensors always return int. Symbolic tensors may return None for dynamic dimensions like batch size. My experience is if you are writing a layer then first dimension (batch size) can commonly be None while other dimensions are rarely None. In practice you normally know if you have symbolic vs concrete tensor but I don’t see an easy way in type system to distinguish the two especially as which is used can vary on tensorflow execution mode (eager vs graph vs tf1). So I normally just put an assert not None as needed. In pytorch tensors are almost always concrete and if you stick to tf2 eager you may not notice the Nones much.
Ideally runtime would become generic. While that's not case that way I handle it for layers/models is like this, if TYPE_CHECKING:
_KerasLayerBase = _KerasLayer
_ModelBase = Model
else:
class _KerasLayerBase(_KerasLayer, Generic[InputT, OutputT]):
...
class _ModelBase(Model, Generic[InputT, OutputT]):
...
class KerasLayerGeneric(_KerasLayerBase[InputT, OutputT], Generic[InputT, OutputT]):
...
class KerasModelGeneric(_ModelBase[InputT, OutputT], Generic[InputT, OutputT]):
...
class KerasLayer(KerasLayerGeneric[tf.Tensor, tf.Tensor]):
...
class KerasModel(KerasModelGeneric[tf.Tensor, tf.Tensor]):
... and then I always use these types at runtime.
My recommendation is do it few files at a time and keep pr size to couple hundred lines (at most 1k) for reviewability. You can pick any files in my stubs and prepare a PR for them here. The main steps I've had for adding to typeshed,
|
Thanks for sharing the snipet to make layers/models into generics, that worked really nicely!
I'll do that, thanks. |
Add the convert_to_tensor TensorFlow function.
The actual return type of the function is
Union[EagerTensor, SymbolicTensor]
(cf the tensorflow function), however those are not present in the stub yet, should they be added ?I would like to help complete the TensorFlow stubs, so I would be thankful if you could tell me anything I would need to know in order to contribute.