本文介绍了具有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 20:35