Python Keras/theano的最大利润损失

Python Keras/theano的最大利润损失,python,neural-network,keras,theano,loss-function,Python,Neural Network,Keras,Theano,Loss Function,我想在Keras中训练一个神经网络(以theano为后端),使用最大边际损失函数,每个正样本使用一个负样本: max(0,1 -pos_score +neg_score) 我有一个神经网络,它接受两个参数I和j,并返回分数base(I,j)。对于给定的i,我有一个阳性样本j和一个阴性样本k。因此,我想计算如下: max(0, 1 - base(i, j) + base(i, k)) i = Input(...) # d=100 j = Input(...) # d=300 k = Inp

我想在Keras中训练一个神经网络(以theano为后端),使用最大边际损失函数,每个正样本使用一个负样本:

 max(0,1 -pos_score +neg_score)
我有一个神经网络,它接受两个参数
I
j
,并返回分数
base(I,j)
。对于给定的
i
,我有一个阳性样本
j
和一个阴性样本
k
。因此,我想计算如下:

 max(0, 1 - base(i, j) + base(i, k))
i = Input(...) # d=100
j = Input(...) # d=300
k = Input(...) # d=300

i_vec = Sequential()
i_vec.add(Dense(20, input_dim=100))
j_vec = Sequential()
j_vec.add(Dense(30, input_dim=300))

base = Sequential()
base.add(Merge([i_vec, j_vec], mode='concat')
# Here goes definition of the base network
base.add(Dense(output_dim=1, bias=False))

pos = base([i, j])
neg = base([i, k])

def custom_loss(y_true, y_pred):
    return K.maximum(0, 1 - y_pred[0] + y_pred[1])

model = Model(input=[i,j,k], output=[pos, neg])
# Shape of I=(1000,100), J and K=(1000,300), XX=(1000,)
model.fit([I, J, K], [XX,XX], nb_epoch=10)
在抽象级别,我的代码如下所示:

 max(0, 1 - base(i, j) + base(i, k))
i = Input(...) # d=100
j = Input(...) # d=300
k = Input(...) # d=300

i_vec = Sequential()
i_vec.add(Dense(20, input_dim=100))
j_vec = Sequential()
j_vec.add(Dense(30, input_dim=300))

base = Sequential()
base.add(Merge([i_vec, j_vec], mode='concat')
# Here goes definition of the base network
base.add(Dense(output_dim=1, bias=False))

pos = base([i, j])
neg = base([i, k])

def custom_loss(y_true, y_pred):
    return K.maximum(0, 1 - y_pred[0] + y_pred[1])

model = Model(input=[i,j,k], output=[pos, neg])
# Shape of I=(1000,100), J and K=(1000,300), XX=(1000,)
model.fit([I, J, K], [XX,XX], nb_epoch=10)
请注意,
XX
在培训期间无效

运行代码时,我遇到以下错误:

ValueError: GpuElemwise. Output dimension mismatch. Output 0 (indices start at 0), working inplace on input 0, has shape[0] == 1, but the output's size on that axis is 32.
Apply node that caused the error: GpuElemwise{Composite{(i0 * (i1 * i2))}}[(0, 0)](GpuElemwise{Composite{Cast{float32}(EQ(i0, i1))}}[(0, 0)].0, GpuElemwise{Composite{(i0 / (i1 * i2))}}[(0, 0)].0, GpuFromHost.0)
Toposort index: 83
Inputs types: [CudaNdarrayType(float32, vector), CudaNdarrayType(float32, (True,)), CudaNdarrayType(float32, vector)]
Inputs shapes: [(1,), (1,), (32,)]
Inputs strides: [(0,), (0,), (1,)]
Inputs values: [CudaNdarray([ 1.]), CudaNdarray([ 1.]), 'not shown']
Outputs clients: [[GpuIncSubtensor{InplaceInc;int64}(GpuIncSubtensor{Inc;int64}.0, GpuElemwise{Composite{(i0 * (i1 * i2))}}[(0, 0)].0, Constant{1}), GpuElemwise{neg,no_inplace}(GpuElemwise{Composite{(i0 * (i1 * i2))}}[(0, 0)].0)]]
我认为问题在于损失函数的计算

注意:我已经尝试使用
XX
作为原始向量和列向量。但是,错误依然存在

使用TensorFlow作为后端解决同一问题的解决方案可用且可用


编辑1: 按以下方式更改损失函数可以正常工作(我的意思是它可以正常工作)。但是,我既不知道为什么,也不知道新代码的正确性

def custom_loss(y_true, y_pred):
    return K.sum(K.maximum(0, 1 - y_pred[0] + y_pred[1]))

似乎
K.max(0,1-y_pred[0]+y_pred[1])
不会给出标量损失值,而是每个样本的误差。您需要平均整个小批量的损失。因此,使用
K.sum
将每样本损失减少为每小批量标量损失。我想使用
mean
sum
更准确(以防您决定更改批量大小)。

明白了,@shai。但是,它会像预期的那样工作吗?我在问代码的正确性。它最小化了最大损失的平均值(总和是按比例计算的平均值)。它是最大值的平均值,而不是最大值的最大值。您可能会考虑K.max(K.max(…))对于以ML为单位的(小型)批次优化,通常会优化平均损失。每个示例的平均值都超过了损失值。