pkmital/CADL

Problem with Python 3 in Session 4 Part 5

Closed this issue · 4 comments

Session 4

Part 5

There is this code snippet:

# Grab the tensor defining the input to the network
x = g.get_tensor_by_name(names[0] + ":0")

# And grab the tensor defining the softmax layer of the network
softmax = g.get_tensor_by_name(names[-2] + ":0")

for img in [content_img, style_img]:
    with tf.Session(graph=g) as sess, g.device('/cpu:0'):
        # Remember from the lecture that we have to set the dropout
        # "keep probability" to 1.0.
        res = softmax.eval(feed_dict={x: img,
                    'net/dropout_1/random_uniform:0': np.ones(
                        g.get_tensor_by_name(
                            'net/dropout_1/random_uniform:0'
                        ).get_shape().as_list()),
                    'net/dropout/random_uniform:0': np.ones(
                        g.get_tensor_by_name(
                            'net/dropout/random_uniform:0'
                        ).get_shape().as_list())})[0]
        print([(res[idx], net['labels'][idx])
               for idx in res.argsort()[-5:][::-1]])

The problem is that when you get the size as:

np.ones( g.get_tensor_by_name( 'net/dropout_1/random_uniform:0' ).get_shape().as_list()

The result is [None, 4096]

When this list is fed into np.ones, there is an error.

Instead, I suggest the following code (or similar):

# Grab the tensor defining the input to the network
x = g.get_tensor_by_name(names[0] + ":0")

# And grab the tensor defining the softmax layer of the network
softmax = g.get_tensor_by_name(names[-2] + ":0")

for img in [content_img, style_img]:
    with tf.Session(graph=g) as sess, g.device('/cpu:0'):
        # Remember from the lecture that we have to set the dropout
        # "keep probability" to 1.0.
        d_0 = g.get_tensor_by_name('net/dropout/random_uniform:0')
        d_0_list = d_0.get_shape().as_list()
        d_0_ones = np.ones([1 if x == None else x for x in d_0_list])
        print(d_0_ones.shape)
        
        d_1 = g.get_tensor_by_name('net/dropout_1/random_uniform:0')
        d_1_list = d_1.get_shape().as_list()
        d_1_ones = np.ones([1 if x == None else x for x in d_1_list])
        print(d_1_ones.shape)
        
        res_obj = softmax.eval(feed_dict={x: img,
                    'net/dropout_1/random_uniform:0': d_1_ones,
                    'net/dropout/random_uniform:0': d_0_ones})
        
        res = res_obj[0]
        
        print([(res[idx], net['labels'][idx])
               for idx in res.argsort()[-5:][::-1]])

Previous code

It's worth mentioning that in the lecture notes, you don't use np.ones, but instead, you explicitly just use [[1]*4096], which creates the same outcome.

Looking forward to hearing your thoughts - Thank you.

Good catch! If you want to submit a PR to change it to [[1] * 4096] I will review and really appreciate it!

Perfecto - I've created a pull request now, thanks Parag!

Hey Parag,
It doesn't appear that this was fixed, and I can't find a PR that addressed the issue. Any idea what happened?
Thanks,
Vishal

You're right! Sorry about that. I'll try and submit a PR or if someone else beats me to it I'll take a look! I think the idea was to replace all instances of feed_dict parameters for dropout to use something like:

'net/dropout/random_uniform:0': [[1]*4096]