【发布时间】:2018-02-16 23:59:11
【问题描述】:
我正在学习 tensorflow 来构建神经网络,下面有一个示例代码:
import tensorflow as tf
# get weight of a layer, and add the l2 regularizer of the weight to the collection of 'losses'
def get_weight(shape, lambda):
var = tf.Variable(tf.random_normal(shape), dtype = tf.float32)
tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(lambda)(var))
return var
我使用 python 3.5 运行这个脚本,但我得到了这个:
File "4.4.2.py", line 4
def get_weight(shape, lambda):
^
SyntaxError: invalid syntax
【问题讨论】:
-
lambda是python中的关键字。您必须选择不同的变量名称。 -
lambda是 python 中的保留字,不应将其用作参数名称。