0

There's a function I don't get:

def group_regularization(v):
    const_coeff = lambda W: tf.sqrt(tf.cast(W.get_shape().as_list()[1], tf.float32))
    return tf.reduce_sum([tf.multiply(const_coeff(W), l21_norm(W)) for W in v if 'bias' not in W.name])

What does the last line mean?

At first I thought it was "apply the function to W except the ones with name bias", but I'm not sure.

Salvador Dali
  • 182,715
  • 129
  • 638
  • 708
Joo Sohn
  • 25
  • 6
  • [Pythonic way to combine FOR loop and IF statement](https://stackoverflow.com/q/6981717/6521116) – LF00 Jun 29 '17 at 06:12

4 Answers4

4

This is a list comprehension with a condition. It has nothing to do with Tensorflow, it's just basic Python.

Conditional list comprehensions take the form

[<expression> for <name> in <list> if <condition>]

This iterates over all elements in <list> for which <condition> is true, assigning each to <name> in turn, evaluating <expression>, and making a new list of the results. It's equivalent to

new_list = []
for <name> in <list>:
    if <condition>:
        new_list.append(<expression>)

except that the list comprehension is cleaner code and possibly a bit faster to run.

David Z
  • 116,302
  • 26
  • 230
  • 268
1

It represents list comprehension expanded as the following:

out_list = []
def group_regularization(v):
    ...
    for W in v:
        if 'bias' not in W.name:
            out_list.append(tf.multiply(const_coeff(W), l21_norm(W)))
    tf.reduce_sum(out_list)

It is iterating through each item in v passed as list in group_regularization

Then, checking if string 'bias' is not in W.name

and then, doing(whatever) operation for tensorflow.

abhinav
  • 956
  • 9
  • 20
0

Let's break it down, it's convention in python:

#Step 1:
for W in v 
# Will foreach loop W for every item in v

#Step 2:
if 'bias' not in W.name #
# in loop, check if 'bias' was an element of list W.name, if yes, dont do anything with that, if not, do next

#Step 3:
const_coeff(W), l21_norm(W))
# calculate these 2

#Step 4:
tf.multiply(const_coeff(W), l21_norm(W)))
# put those calculated in step 3 into tf.multiply

#Step 5:
[tf.multiply(....)]
# put everything in a list (since you have a loop at step 1)

#Step 6:
tf.reduct_sum([...])
# calculate reduce_sum

#Step 7:
return ....
# pass that result out
Phung Duy Phong
  • 796
  • 5
  • 17
0

Yes it is a list comprehension with a condition. List comprehensions can have multiple conditions.

The general format for a list comprehension with a if condition is this,

[<expression> for <value> in <iterable> if <condition>]

You can also have an if..else in the comprehension

[<expression> if <condition> else <expression> for <value> in <iterable> ]

NOTE: Your iterable can be list,tuple,set,string,...etc

To make things clear consider this simple example,

>>> v = [1,2,3,4]
>>> v
[1, 2, 3, 4]

Assume this is your v from your given code, also for understanding purposes let's say Wname is something similar to W.name

>>> Wname = [1,2]
>>> Wname
[1, 2]

Now suddenly you decide I want a list x which has items from v but not in Wname. Hmmm... How to do that? Take a look below.

>>> x = [W for W in v if W not in Wname]
>>> x
[3, 4]

Somewhat along these lines is what's happening in the code you mentioned above.

void
  • 2,254
  • 1
  • 16
  • 29