通过添加新的权重参数自定义损失函数/添加新的lambda层来实现Keras自定义预测值

问题描述

我正在尝试将我的预测值构造为:

y_hat = alpha * output_1 + (1-alpha) * output_2

“ output_1”和“ output_2”都是从前一层获得的n * 1向量,而alpha是标量。但是问题是我不想自己修复Alpha,而是希望Alpha能够自己学习。

示例代码

def custom_lambda_layer_1(input1):
    return output1

def custom_lambda_layer_2(input2):
    return output2

lambda_layer_1 = Lambda(custom_lambda_layer_1)(prevIoUs1)  #prevIoUs1 is the output from prevIoUs layers
lambda_layer_2 = Lambda(custom_lambda_layer_2)(prevIoUs2)

### here is what I want to do 
def y_hat(parameter):
    return parameter[0] * alpha + parameter[1] * (1-alpha)

lambda_layer = Lambda(y_hat)([lambda_layer_1,lambda_layer_2])

### but I don't kNow where to write alpha as input/ output

我不知道如何修改代码,以便可以将alpha视为可以更新的参数。我不确定是否也可以通过这种方式学习alpha。

或者,我可以让

alpha_new = alpha_odd - learn_rate * gradient_of_alpha

但是我也不知道该怎么插入?我读了其他答案,但它们的“ alpha”不是一个可以由人更改的固定值,就是来自先前各层的输出。但是在这里,“ alpha”基本上是连接两个输出部分的权重参数。最初的问题是我不知道如何为这两个输出分配权重,因此我希望自己可以对其进行学习/优化。

任何想法将不胜感激!!谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)