Output from TensorFlow `py_func` has unknown rank/shape
Asked Answered
T

1

34

I am trying to create a simple neural net in TensorFlow. The only tricky part is I have a custom operation that I have implemented with py_func. When I pass the output from py_func to a Dense layer, TensorFlow complains that the rank should be known. The specific error is:

ValueError: Inputs to `Dense` should have known rank.

I don't know how to preserve the shape of my data when I pass it through py_func. My question is how do I get the correct shape? I have a simple example below to illustrate the problem.

def my_func(x):
    return np.sinh(x).astype('float32')

inp = tf.convert_to_tensor(np.arange(5))
y = tf.py_func(my_func, [inp], tf.float32, False)

with tf.Session() as sess:
    with sess.as_default():
        print(inp.shape)
        print(inp.eval())
        print(y.shape)
        print(y.eval())

The output from this snippet is:

(5,)
[0 1 2 3 4]
<unknown>
[  0.       
1.17520118   3.62686038  10.01787472  27.28991699]

Why is y.shape <unknown>? I want the shape to be (5,) the same as inp. Thanks!

Trocki answered 4/3, 2017 at 0:2 Comment(2)
Possible duplicate of Tensorflow: Py_func returns unknown shapeMoluccas
@Moluccas Maybe, it's been a while since I asked but I found that question which the author answered himself. I remember his solution not working for me which is why I wrote my question.Trocki
A
54

Since py_func can execute arbitrary Python code and output anything, TensorFlow can't figure out the shape (it would require analyzing Python code of function body) You can instead give the shape manually

y.set_shape(inp.get_shape())
Anabatic answered 4/3, 2017 at 1:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.