tfpyth icon indicating copy to clipboard operation
tfpyth copied to clipboard

Simpler interface that automatically creates placeholders

Open theRealSuperMario opened this issue 4 years ago • 2 comments

Hi. First of all, thanks for writing this library. It saves me a lot of time just wrapping tensorflow code into a new function and using it as is in my pytorch projects.

This is exactly the gist of this issue: I thought we might be able to get the interface a little bit simpler so that you just literally have to wrap it into a new function.

I think it should look something like this:

def tf_function(a, b):
    c = 3 * a + 4 * b * b

    return c

session = tf.compat.v1.Session()
f = tfpyth.wrap_torch_from_tensorflow(
        tf_function, ["a", "b"], session=session
    )
# or simpler
f = tfpyth.wrap_torch_from_tensorflow(
        tf_function, session=session
    ) # automatically creates placeholders for "a" and "b" inside
# or even simpler
f = tfpyth.wrap_torch_from_tensorflow(
        tf_function
    ) # automatically creates placeholders for "a" and "b" and session

a_ = th.tensor(1, dtype=th.float32, requires_grad=True)
b_ = th.tensor(3, dtype=th.float32, requires_grad=True)
x = f(a_, b_)

assert x == 39.0

x.backward()

assert np.allclose((a_.grad, b_.grad), (3.0, 24.0))

Turns out, I already went ahead and added this exact feature. You can check it out -->here<--. I was just wondering a PR would be interesting to persue this feature.

Cheers

theRealSuperMario avatar Apr 15 '20 14:04 theRealSuperMario

Oh yes, this is cool! :tada: I'm currently scramming towards some deadlines, but I'll get back to you next week.

It looks very useful. I need to think about the session singleton. I'm wary of having too much state, so it might be easier to just use tf.compat.v1.get_default_session()? Everything else could go in right away I think.

Thanks!

BlackHC avatar Jun 08 '20 19:06 BlackHC

Thanks for taking a minute to respond.

I am not set on the singleton. It was just my first guess on how to prevent multiple sessions to spawn.

I also think that ultimately, one would want to have a decorator style functionality which reads types from type hints and creates the callable automatically.

On Mon, Jun 8, 2020, 21:06 Andreas Kirsch [email protected] wrote:

Oh yes, this is cool! 🎉 I'm currently scramming towards some deadlines, but I'll get back to you next week.

It looks very useful. I need to think about the session singleton. I'm wary of having too much state, so it might be easier to just use tf.compat.v1.get_default_session()? Everything else could go in right away I think.

Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/BlackHC/tfpyth/issues/3#issuecomment-640815303, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRXFWISN2N5GM6VVYABE4TRVUZDHANCNFSM4MIUTYRQ .

theRealSuperMario avatar Jun 09 '20 07:06 theRealSuperMario