19242

Output from TensorFlow `py_func` has unknown rank/shape

Question:

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!

Answer1:

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())

Recommend

  • Session variable does not get updated within javascript code
  • How to prevent default on form submit?
  • Android: force onKeyListener to wait
  • Increase the speed of redrawing contour plot in matplotlib
  • Accessing function attribute created in a decorator outside that decorator
  • Sampling unique column indexes for each row of a numpy array
  • Tensorflow: “GraphDef cannot be larger than 2GB.” error when saving model after assigning variables
  • UglifyJS2 removes wanted comments at the end of a block or file
  • A limitation of Sqlite3's full text search doesn't allow ORs with MATCHes. Workaround?
  • TabBarController: Orienting views in different orientations
  • Replicating and differentiating portions of a form
  • Implementing HMAC-SHA256 for Keybase in Javascript
  • How to add new index numbers to the upsampled data while preserving the orginal indices one
  • Catching exception and throwing the same?
  • Cythonized function unexpectedly slow
  • How to reshape a 3D numpy array?
  • How to change placeholder text in an autocomplete activity of android google place?
  • Efficient & Pythonic way of finding all possible sublists of a list in given range and the minim
  • Recording values of radio buttons in ember
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • How to push additional view controllers onto NavigationController but keep the TabBar?