# Anyone using the Tensorflow functional API from inside q(embedpy)?

**URL:** <https://forum.kx.com/t/anyone-using-the-tensorflow-functional-api-from-inside-q-embedpy/13668>\
**Category:** Community Support\
**Tags:** imported, other\
**Created:** [February 17, 2022, 12:00am UTC](https://forum.kx.com/t/anyone-using-the-tensorflow-functional-api-from-inside-q-embedpy/13668 "2022-02-17T00:00:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krish240574](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@krish240574](https://forum.kx.com/u/krish240574)\
**Post date:** [February 17, 2022, 12:00am UTC](https://forum.kx.com/t/anyone-using-the-tensorflow-functional-api-from-inside-q-embedpy/13668/1 "2022-02-17T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/anyone-using-the-tensorflow-functional-api-from-inside-qembedpy](https://learninghub.kx.com/forums/topic/anyone-using-the-tensorflow-functional-api-from-inside-qembedpy)

Hello,

I’m trying to write this code inside q:

```
def get_model(input_shape, time2vec_dim = 3):
inp = Input(input_shape)
x = inp

time_embedding = keras.layers.TimeDistributed(Time2Vec(time2vec_dim - 1))(x) / ******************* /

Equivalent q code is :

lyr:.p.import`keras.layers

x:lyr[`:Input;<;65;16] /create Input(65,16)

time_embedding:lyr[`:TimeDistributed;<;.p.eval("Time2Vec(2)")] / Time2Vec is my own class, defined in a .p file
```

Now, how do I pass the final (x) in the above line? (marked /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/)

I tried time\_embedding, it gives me a “rank” error.

What is the semantic meaning of a (x) passed in the functional API? Code-wise, it seems that we’re passing (x) to an object of TimeDistributed above. Doesn’t quite make sense, could someone please shed light on this issue?

Thanks,

Krishna

---

<div class="post-metadata">

**Author:** ![rocuinneagain](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/rocuinneagain/32/287_2.png) [@rocuinneagain](https://forum.kx.com/u/rocuinneagain)\
**Post date:** [February 18, 2022, 12:00am UTC](https://forum.kx.com/t/anyone-using-the-tensorflow-functional-api-from-inside-q-embedpy/13668/2 "2022-02-18T00:00:00Z")

</div>

You could load the function in to q rather than redefine it line by line.

&nbsp;

Example file.p

def get\_model(input\_shape, time2vec\_dim = 3): inp = Input(input\_shape) x = inp time\_embedding = keras.layers.TimeDistributed(Time2Vec(time2vec\_dim - 1))(x)

Load it and pull in the function:

```
q)l file.p 
q)get_model:.p.get`get_model 
q)get_model[(65;16)]
```

Another example of this: [https://github.com/rianoc/qparquet](https://github.com/rianoc/qparquet")

&nbsp;

Looking to replicate an example from [https://keras.io/api/layers/recurrent\_layers/time\_distributed/](https://keras.io/api/layers/recurrent_layers/time_distributed/")

```
q)lyr:.p.import`keras.layers 
q)x:lyr[`:Input;`shape pykw (10, 128, 128, 3)] 
q)conv_2d_layer:lyr[`:Conv2D;64;(3;3)] 
q)time_embedding:lyr[`:TimeDistributed;conv_2d_layer] q
)outputs:time_embedding[x] 
q)print outputs[`:shape] (None, 10, 126, 126, 64)
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![krish240574](https://avatars.discourse-cdn.com/v4/letter/k/838e76/32.png) [@krish240574](https://forum.kx.com/u/krish240574)\
**Post date:** [February 18, 2022, 12:00am UTC](https://forum.kx.com/t/anyone-using-the-tensorflow-functional-api-from-inside-q-embedpy/13668/3 "2022-02-18T00:00:00Z")

</div>

Thanks a ton for the detailed replies, @rocuinneagain. I see that I was making a couple of errors:

1. Using all the tensorflow function calls with “\<” included, i.e intending to get them to return q. I’m much better off leaving them without the “\<”, - using them as a default embedpy , as mentioned here :

[https://code.kx.com/q/ml/embedpy/userguide/#function-calls](https://code.kx.com/q/ml/embedpy/userguide/#function-calls")

1. Doing a .p.eval"Time2Vec(2)" - causes the following warning:

WARNING:tensorflow:AutoGraph could not transform \<bound method Time2Vec.call of \< **main**.Time2Vec object at 0x7fbf09ad6160\>\> and will run it as-is.  
Cause: Unable to locate the source code of \<bound method Time2Vec.call of \< **main**.Time2Vec object at 0x7fbf09ad6160\>\>. Note that functions defined in certain environments, like the interactive Python shell, do not expose their source code. If that is the case, you should define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.experimental.do\_not\_convert. Original error: source code not available  
To silence this warning, decorate the function with @tf.autograph.experimental.do\_not\_convert

&nbsp;

Which I avoided using the following:(

(.p.get`Time2Vec)[2] - seems strange, that the .p.eval should cause that warning, which the .p.get `Time2Vec does not.

I

I’ll read the documentation again.

&nbsp;

Cheers,

Krishna
