Python 什么是';对于在里面如果不是';意思是
有一个函数我不知道:Python 什么是';对于在里面如果不是';意思是,python,Python,有一个函数我不知道: 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]) 最后一行是什么意思 起初我以为它是“将函
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])
最后一行是什么意思
起初我以为它是“将函数应用于
W
,除了名为bias
”的函数之外”,但我不确定。它表示列表理解扩展如下:
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)
它正在迭代v
中的每个项目,作为组正则化中的列表传递
然后,检查字符串“bias”是否不在W.name中
然后,对tensorflow执行(任何)操作。它表示列表理解,扩展如下:
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)
它正在迭代v
中的每个项目,作为组正则化中的列表传递
然后,检查字符串“bias”是否不在W.name中
然后,对tensorflow执行(任何)操作。这是一个有条件的操作。它与Tensorflow无关,它只是基本的Python
条件列表的理解形式为
[<expression> for <name> in <list> if <condition>]
[用于if中]
这将迭代
中
为真的所有元素,依次将每个元素分配给
,评估
,并生成新的结果列表。相当于
new_list = []
for <name> in <list>:
if <condition>:
new_list.append(<expression>)
new_list=[]
例如:
如果:
新建_list.append()
除了列表理解是更干净的代码,而且可能运行得更快一些。这是一个有条件的问题。它与Tensorflow无关,它只是基本的Python
条件列表的理解形式为
[<expression> for <name> in <list> if <condition>]
[用于if中]
这将迭代
中
为真的所有元素,依次将每个元素分配给
,评估
,并生成新的结果列表。相当于
new_list = []
for <name> in <list>:
if <condition>:
new_list.append(<expression>)
new_list=[]
例如:
如果:
新建_list.append()
除此之外,列表理解是更干净的代码,并且可能运行得更快一些。让我们把它分解一下,这是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
让我们将其分解,这是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
是的,这是一个带有条件的列表理解。列表理解可以有多个条件
如果
条件为,则列表理解的一般格式为
[<expression> for <value> in <iterable> if <condition>]
注意:您的iterable可以是列表、元组、集合、字符串等
为了说明清楚,请考虑这个简单的例子,
>>> v = [1,2,3,4]
>>> v
[1, 2, 3, 4]
假设这是给定代码中的v
,也为了理解,我们假设Wname
类似于W.name
>>> Wname = [1,2]
>>> Wname
[1, 2]
现在您突然决定我想要一个列表x,其中包含来自v的项目,但不在Wname中。六羟甲基三聚氰胺六甲醚。。。怎么做?看看下面
>>> x = [W for W in v if W not in Wname]
>>> x
[3, 4]
您在上面提到的代码中发生了一些类似的事情。是的,这是一个带有条件的列表理解。列表理解可以有多个条件
如果
条件为,则列表理解的一般格式为
[<expression> for <value> in <iterable> if <condition>]
注意:您的iterable可以是列表、元组、集合、字符串等
为了说明清楚,请考虑这个简单的例子,
>>> v = [1,2,3,4]
>>> v
[1, 2, 3, 4]
假设这是给定代码中的v
,也为了理解,我们假设Wname
类似于W.name
>>> Wname = [1,2]
>>> Wname
[1, 2]
现在您突然决定我想要一个列表x,其中包含来自v的项目,但不在Wname中。六羟甲基三聚氰胺六甲醚。。。怎么做?看看下面
>>> x = [W for W in v if W not in Wname]
>>> x
[3, 4]
您在上面提到的代码中发生了一些类似的事情