Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how does the get_shape function work with placeholders? #25

Closed
JenkinsY94 opened this issue Feb 9, 2018 · 4 comments
Closed

how does the get_shape function work with placeholders? #25

JenkinsY94 opened this issue Feb 9, 2018 · 4 comments

Comments

@JenkinsY94
Copy link

I tried The code exapmle b = tf.placeholder(tf.float32, [None, 10, 32]); shape = get_shape(b) ,
but when I print out the shape, it show tensor objects, rather than the dynamic/static shape as expected.

I wonder how can I use this get_shape function in a session properly in order to get a placeholder's shape?

Thx!

@vahidk
Copy link
Owner

vahidk commented Feb 9, 2018

Try print(tf.Session().run(shape))

@vahidk vahidk closed this as completed Feb 9, 2018
@JenkinsY94
Copy link
Author

Hi vahidk. It seems not working because get_shape() returns a list of mixture of integer and tensor object.

Here's a working example:

def get_dyn_shape(tensor):
    dynamic_shape = tf.unstack(tf.shape(tensor))
    return dynamic_shape
a = tf.placeholder(tf.float32, [None, 128])
dyn_shape = get_dyn_shape(a)
print(tf.Session().run(dyn_shape, feed_dict={a:np.random.random((3, 128))}))

But When I replace get_dyn_shape with get_shape in your article, it failed. Tensorflow says:

TypeError: Fetch argument 128 has invalid type <class 'int'>, must be a string or Tensor. (Can not 
convert a int into a Tensor or Operation.)

Any suggestions?

Also, I'm confused about when to use this get_shape, as get_dyn_shape returns exactly same result?

@vahidk
Copy link
Owner

vahidk commented Feb 9, 2018

This get_shape() function is not designed to be used for printing shapes. If you want to do that just use session.run(tf.shape(x)). The output of get_shape() is useful for creating other tensors. What it does is that it tries to keep the shape static if possible otherwise dynamic which can be useful at times.

@Shunsuke-1994
Copy link

Shunsuke-1994 commented Aug 14, 2018

".values().get_shape()" or ".outputs.get_chape()" method worked!
https://www.tensorflow.org/api_docs/python/tf/Operation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants