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!

Ali Salehi
  • 1,003
  • 8
  • 19
Jacques Kvam
  • 2,856
  • 1
  • 26
  • 31
  • 1
    Possible duplicate of [Tensorflow: Py\_func returns unknown shape](https://stackoverflow.com/questions/38992445/tensorflow-py-func-returns-unknown-shape) – gkcn Aug 09 '17 at 14:02
  • @gkcn 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. – Jacques Kvam Aug 09 '17 at 21:36

1 Answers1

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())
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197