Gotcha! Tensor Shape

While working on the second part of my style-transfer project, I needed to obtain the shape of a tensor. I decided to try using the tf.shape function.

Reading the example provided on the documentation, it seemed like it would do what I needed, nevermind the odd:

This operation returns a 1-D integer tensor representing the shape of input.

When I tried this in a notebook:

dummy_data = np.random.rand(1,4,2)
dummy_tensor = tf.constant(dummy_data)
print("TF shape function outside a session: {}".format(tf.shape(dummy_tensor)))

The behaviour was unexpected:

TF shape function outside a session: Tensor(“Shape:0”, shape=(3,), dtype=int32)

I had a hunch about what I was doing wrong so I tried the following:

with tf.Session() as sess:
    dummy_tensor_shape_ = sess.run(dummy_tensor_shape)
    print("TF shape function inside a session: {}".format(dummy_tensor_shape_))

Which produced the following:

TF shape function inside a session: [1 4 2]

As expected!

Once again, I was reminded that tensor operations will only provide what you want within a session! Before that, the tensor is simply an operation waiting to be executed. The 3 noted in the shape outside the session seems to be the length of the list containing the actual shape.

Thankfully, TF offers another function: get_shape() Note that get_shape is just an alias for shape. This method infers the static shape but can fail in some cases, for example where the input data shape isn’t known until runtime. This explanation provides a clear example of a failure case.

The TF documentation notes that this function can provide debugging information and early warnings. My use case was slightly different in that I actually used the shape information in a loss calcuation. So perhaps I should have figured out how to use tf.shape properly within the graph. However, get_shape worked fine and it was easier to understand at the time so it is what I ended up using.

If you’d like to see for yourself the difference ways of getting a tensor’s shape, here’s a notebook for you to tinker with!

2020

Guns Germs And Steel

46 minute read

I’ve never been a big history fan. Too many names and dates to memorize. But now, free from the pressure of having to learn for the sake of getting good grad...

Letters From A Stoic

42 minute read

A little while ago, I read Letters from a Stoic by Seneca (translated and edited by Robin Campbell). I got a lot out of reading Letters and wanted to encoura...

Every Tool Is A Hammer

21 minute read

I recently finished reading Every Tool’s a Hammer: Life Is What You Make It1 by Adam Savage. It was an energizing read and I highly recommend this book to fe...

Back to top ↑

2019

Culture Series

46 minute read

Recently, I binged through the Culture series by Ian M. Banks. It was an amazing read and I thought a write up about it might help ground the experience and ...

Learning to Learn

25 minute read

I recently the following on Coursera: Learning How to Learn: Powerful mental tools to help you master tough subjects Mindshift: Break Through Obstacles ...

Back to top ↑

2018

Style Transfer

4 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

Gotcha! Tensor Shape

1 minute read

While working on the second part of my style-transfer project, I needed to obtain the shape of a tensor. I decided to try using the tf.shape function.

Style Reconstruction

11 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

Gotcha! TF Variable Initialization Order

2 minute read

While working on the first part of my style-transfer project, I had: A new input variable which would have to be initialized from scratch. The VGG-19 ne...

Gotcha! TF Input Data Type

1 minute read

While working on the first part of my style-transfer project, I found out the hard way that TF is very sensitive to the network’s input’s data type.

Gotcha! TF Saver Subset Initialization

2 minute read

While working on the first part of my style-transfer project, I dealt with two main variable groups: The input variable which was the image I was optimizi...

Gotcha! Pyplot Image Displays

3 minute read

While working on the first part of my style-transfer project, I used pyplot’s imshow to diplay images in the notebook. However, it took me a little bit of pl...

Gotcha! CV2 and Pyplot Channel Order

less than 1 minute read

While working on the first part of my style-transfer project, I used Open CV’s cv2.imwrite to save images to disk. However, the images seemed to have a weird...

Gotcha! CV2 JPEG vs PNG

1 minute read

While working on the first part of my style-transfer project, I ran into lots of image issues. One of the issues was that cv2 uses a BGR channel order inste...

Content Reconstruction

12 minute read

If you’re reading this, I’m assuming that you’ve read the paper Image Style Transfer Using Convolutional Neural Networks and have some familiarity with it.

First Post

less than 1 minute read

Cue customary Hello World.

Back to top ↑