Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output from TensorFlow `py_func` has unknown rank/shape

Tags:

tensorflow

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!

like image 522
Jacques Kvam Avatar asked Mar 04 '17 00:03

Jacques Kvam


1 Answers

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()) 
like image 143
Yaroslav Bulatov Avatar answered Sep 25 '22 08:09

Yaroslav Bulatov