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!
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With