Python 使用包装器投掷的keras中的自定义损失;作业前参考“;错误
我试图使用包装器将网络输入包含到损耗计算中。我将输入作为model.input传递给loss wrapper函数。带包装器的自定义损失函数为:Python 使用包装器投掷的keras中的自定义损失;作业前参考“;错误,python,tensorflow,keras,Python,Tensorflow,Keras,我试图使用包装器将网络输入包含到损耗计算中。我将输入作为model.input传递给loss wrapper函数。带包装器的自定义损失函数为: def custom_loss_wrapper(input_tensor): def custom_loss(y_true, y_pred): y_pred = K.print_tensor(y_pred, message="y_pred - ") input_tensor_2 = input_
def custom_loss_wrapper(input_tensor):
def custom_loss(y_true, y_pred):
y_pred = K.print_tensor(y_pred, message="y_pred - ")
input_tensor_2 = input_tensor
input_tensor = K.print_tensor(input_tensor, message="input_tensor - ")
y_true_1 = [0.1, 0.2]
y_true_2 = [0.3, 0.2]
bool_2 = input_tensor - tf.constant([1], dtype="float32")
bool_2 = K.print_tensor(bool_2, message="bool_2 - ")
bool_1 = tf.constant([2], dtype="float32") - input_tensor
bool_1 = K.print_tensor(bool_1, message="bool_1 - ")
y_true_1_tf = tf.constant([y_true_1], dtype=tf.float32)
y_true_1_bool = y_true_1_tf * bool_1
y_true_2_tf = tf.constant([y_true_2], dtype=tf.float32)
y_true_2_bool = y_true_2_tf * bool_2
y_true_custom = y_true_1_bool + y_true_2_bool
#y_true_custom = K.print_tensor(y_true_custom, message="y_true_custom - ")
loss = K.square(y_pred - y_true)
#loss=K.print_tensor(loss, message="loss - ")
return loss
return custom_loss
编译模型的代码是:
model.compile(loss=custom_loss_wrapper(model.input), optimizer='adam')
这将抛出错误->
UnboundLocalError: in user code:
<ipython-input-3-a2ee38c1577c>:16 custom_loss *
input_tensor_2 = input_tensor
UnboundLocalError: local variable 'input_tensor' referenced before assignment
UnboundLocalError:在用户代码中:
:16海关损失*
输入张量2=输入张量
UnboundLocalError:赋值前引用的局部变量“input_tensor”
具体来说,错误作为内部自定义丢失函数的第二行抛出
如果您有任何关于为什么会发生这种情况以及如何纠正这种情况的帮助,我们将不胜感激。编辑答案: 原因是您选择如何命名变量。如果运行此代码,您将遇到相同的错误:
def test(x):
def next_level():
x = x
print("What to print?", x)
return next_level
a = test("Print this.")
a()
但是如果删除x=x
(或更改为y=x
),此代码运行正常。这是因为在这里,您正在内部函数中本地创建一个x
,当您尝试访问它时,该函数尚未声明。如果未在内部函数中声明x
,x
将引用外部函数中定义的内容
通过确保不在内部函数中声明input\u tensor
,将使用外部范围中的input\u tensor
。这应该可以做到:
def custom_loss_wrapper(input_tensor):
def custom_loss(y_true, y_pred):
y_pred = K.print_tensor(y_pred, message="y_pred - ")
input_tensor_2 = K.print_tensor(input_tensor, message="input_tensor - ")
y_true_1 = [0.1, 0.2]
y_true_2 = [0.3, 0.2]
bool_2 = input_tensor_2 - tf.constant([1], dtype="float32")
bool_2 = K.print_tensor(bool_2, message="bool_2 - ")
bool_1 = tf.constant([2], dtype="float32") - input_tensor_2
bool_1 = K.print_tensor(bool_1, message="bool_1 - ")
y_true_1_tf = tf.constant([y_true_1], dtype=tf.float32)
y_true_1_bool = y_true_1_tf * bool_1
y_true_2_tf = tf.constant([y_true_2], dtype=tf.float32)
y_true_2_bool = y_true_2_tf * bool_2
y_true_custom = y_true_1_bool + y_true_2_bool
#y_true_custom = K.print_tensor(y_true_custom, message="y_true_custom - ")
loss = K.square(y_pred - y_true)
#loss=K.print_tensor(loss, message="loss - ")
return loss
return custom_loss
编辑答案: 原因是您选择如何命名变量。如果运行此代码,您将遇到相同的错误:
def test(x):
def next_level():
x = x
print("What to print?", x)
return next_level
a = test("Print this.")
a()
但是如果删除x=x
(或更改为y=x
),此代码运行正常。这是因为在这里,您正在内部函数中本地创建一个x
,当您尝试访问它时,该函数尚未声明。如果未在内部函数中声明x
,x
将引用外部函数中定义的内容
通过确保不在内部函数中声明input\u tensor
,将使用外部范围中的input\u tensor
。这应该可以做到:
def custom_loss_wrapper(input_tensor):
def custom_loss(y_true, y_pred):
y_pred = K.print_tensor(y_pred, message="y_pred - ")
input_tensor_2 = K.print_tensor(input_tensor, message="input_tensor - ")
y_true_1 = [0.1, 0.2]
y_true_2 = [0.3, 0.2]
bool_2 = input_tensor_2 - tf.constant([1], dtype="float32")
bool_2 = K.print_tensor(bool_2, message="bool_2 - ")
bool_1 = tf.constant([2], dtype="float32") - input_tensor_2
bool_1 = K.print_tensor(bool_1, message="bool_1 - ")
y_true_1_tf = tf.constant([y_true_1], dtype=tf.float32)
y_true_1_bool = y_true_1_tf * bool_1
y_true_2_tf = tf.constant([y_true_2], dtype=tf.float32)
y_true_2_bool = y_true_2_tf * bool_2
y_true_custom = y_true_1_bool + y_true_2_bool
#y_true_custom = K.print_tensor(y_true_custom, message="y_true_custom - ")
loss = K.square(y_pred - y_true)
#loss=K.print_tensor(loss, message="loss - ")
return loss
return custom_loss
这部分
def自定义丢失包装(输入张量):
def自定义损耗(y_真,y_pred):
y_pred=K.print_张量(y_pred,message=“y_pred-”)
输入张量2=输入张量
这是嵌套函数。如果未传递,则第二个函数不知道第一个函数中的任何变量或参数。换句话说,输入张量
变量尚未在第二个函数中声明。此部分
def自定义丢失包装(输入张量):
def自定义损耗(y_真,y_pred):
y_pred=K.print_张量(y_pred,message=“y_pred-”)
输入张量2=输入张量
这是嵌套函数。如果未传递,则第二个函数不知道第一个函数中的任何变量或参数。换句话说,
input\u tensor
变量尚未在第二个函数中声明。我使用了这个答案。据我所知,我无法将更多参数传递给自定义损失(y_pred,y_true)@corvo我现在更新了答案。希望它能解决你的问题。我试着按照你说的做,但还是犯了同样的错误。我更新了code@corvo很抱歉,我不是很清楚,但问题是您正在同时使用和分配input\u tensor
。我将用你的代码示例更新我的答案。我使用了这个答案。据我所知,我无法将更多参数传递给自定义损失(y_pred,y_true)@corvo我现在更新了答案。希望它能解决你的问题。我试着按照你说的做,但还是犯了同样的错误。我更新了code@corvo很抱歉,我不是很清楚,但问题是您正在同时使用和分配input\u tensor
。我将用你的代码示例更新我的答案。你知道我如何在第二个函数中访问它吗?我无法将任何其他参数传递给自定义的\u loss(y\u true,y\u pred)知道如何在第二个函数中访问它吗?我无法将任何其他参数传递给自定义\u loss(y\u true,y\u pred)