本文介绍了Keras 中带有权重的自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是神经网络的新手.我想在TensorFlow中做一个自定义的损失函数,但是我需要得到一个权重向量,所以我是这样做的:

I'm new with neural networks. I wanted to make a custom loss function in TensorFlow, but I need to get a vector of weights, so I did it in this way:

def my_loss(weights):
  def custom_loss(y, y_pred):
    return weights*(y - y_pred)
  return custom_loss
model.compile(optimizer='adam', loss=my_loss(weights), metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=None,  validation_data=(x_test, y_test), epochs=100)

当我启动它时,我收到此错误:

When I launch it, I receive this error:

InvalidArgumentError:  Incompatible shapes: [50000,10] vs. [32,10]

形状是:

print(weights.shape)
print(y_train.shape)
(50000, 10)
(50000, 10)

所以我认为这是批次的问题,我对 TensorFlow 没有很强的背景,所以我尝试使用全局变量以一种幼稚的方式解决

So I thought that it was a problem with the batches, I don't have a strong background with TensorFlow, so I tried to solve in a naive way using a global variable

batch_index = 0

然后在自定义回调中将其更新到on_batch_begin"挂钩中.但它不起作用,这是一个可怕的解决方案.那么,如何获得具有相应 y 的权重的确切部分?我有没有办法在自定义损失中获取当前批次索引?预先感谢您的帮助

and then updating it within a custom callback into the "on_batch_begin" hook. But it didn't work and it was a horrible solution. So, how can I get the exact part of the weights with the corresponding y? Do I have a way to get the current batch index inside the custom loss?Thank you in advance for your help

推荐答案

这是一种将附加参数传递给自定义损失函数(在您的情况下为权重数组)的解决方法.诀窍在于使用虚假输入,这些输入有助于以正确的方式构建和使用损失.不要忘记 keras 处理固定的批量维度

this is a workaround to pass additional arguments to a custom loss function, in your case an array of weights. the trick consists in using fake inputs which are useful to build and use the loss in the correct ways. don't forget that keras handles fixed batch dimension

我在回归问题中提供了一个虚拟示例

I provide a dummy example in a regression problem

def mse(y_true, y_pred, weights):
    error = y_true-y_pred
    return K.mean(K.square(error) + K.sqrt(weights))

X = np.random.uniform(0,1, (1000,10))
y = np.random.uniform(0,1, 1000)
w = np.random.uniform(0,1, 1000)

inp = Input((10,))
true = Input((1,))
weights = Input((1,))
x = Dense(32, activation='relu')(inp)
out = Dense(1)(x)

m = Model([inp,true,weights], out)
m.add_loss( mse( true, out, weights ) )
m.compile(loss=None, optimizer='adam')
m.fit(x=[X, y, w], y=None, epochs=3)

## final fitted model to compute predictions (remove W if not needed)
final_m = Model(inp, out)

这篇关于Keras 中带有权重的自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 19:46