如何在自定义损失函数中遍历张量?

时间:2018-07-18 15:13:04

标签: python tensorflow keras loss-function

我正在使用带有tensorflow后端的keras。我的目标是使用自定义损失函数查询当前批次的batchsize。这是计算定制损失函数的值所必需的,该值取决于特定观测值的索引。考虑到以下最少的可重现示例,我想更清楚地说明这一点。

(顺便说一句:当然,我可以使用为训练过程定义的批处理大小,并在定义自定义损失函数时使用它的值,但是有一些原因导致这种变化的原因,尤其是在epochsize % batchsize(epochsize modulo batchsize)不等于零,那么最后一个时期的大小是不同的。我在stackoverflow中找不到合适的方法,例如 Tensor indexing in custom loss functionTensorflow custom loss function in Keras - loop over tensorLooping over a tensor,因为显然在构建图时就无法推断出任何张量的形状(损失函数就是这种情况)–形状推断仅在评估给定值时才可能数据,仅在给定图形的情况下才可能。因此,我需要告诉自定义损失函数对沿特定维度的特定元素执行某些操作,而无需知道维度的长度。

(所有示例中都是相同的)

from keras.models import Sequential
from keras.layers import Dense, Activation

# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))

示例1:没有问题,没有特殊之处,没有自定义损失

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])    

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)

(省略输出,可以正常运行)

示例2:没什么特别的,具有相当简单的自定义损失

def custom_loss(yTrue, yPred):
    loss = np.abs(yTrue-yPred)
    return loss

model.compile(optimizer='rmsprop',
              loss=custom_loss,
              metrics=['accuracy'])

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)

(省略输出,可以正常运行)

示例3:问题

def custom_loss(yTrue, yPred):
    print(yPred) # Output: Tensor("dense_2/Sigmoid:0", shape=(?, 1), dtype=float32)
    n = yPred.shape[0]
    for i in range(n): # TypeError: __index__ returned non-int (type NoneType)
        loss = np.abs(yTrue[i]-yPred[int(i/2)])
    return loss

model.compile(optimizer='rmsprop',
              loss=custom_loss,
              metrics=['accuracy'])

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)

当然,张量还没有形状信息,只有在训练时才可以在构建图形时推断出该信息。因此for i in range(n)会引发错误。有什么方法可以执行此操作?

输出的追溯: enter image description here

-------

顺便说一句,如果有任何疑问,这是我真正的自定义损失函数。为了清楚和简单起见,我在上面跳过了它。

def neg_log_likelihood(yTrue,yPred):
    yStatus = yTrue[:,0]
    yTime = yTrue[:,1]    
    n = yTrue.shape[0]    
    for i in range(n):
        s1 = K.greater_equal(yTime, yTime[i])
        s2 = K.exp(yPred[s1])
        s3 = K.sum(s2)
        logsum = K.log(y3)
        loss = K.sum(yStatus[i] * yPred[i] - logsum)
    return loss

image loss function: partial negative log-likelihood of the cox proportional harzards model。这是为了澄清注释中的一个问题,以避免混淆。我认为没有必要详细了解这一问题来回答这个问题。

1 个答案:

答案 0 :(得分:2)

和往常一样,不要循环播放。存在严重的性能缺陷和错误。除非完全不可避免(通常并非不可避免),否则仅使用后端函数


解决方案示例3:

所以,那里有一件很奇怪的事情……

  

您真的要忽略模型预测的一半吗? (示例3)

假设这是真的,只需在最后一个维度上复制张量,展平并丢弃一半即可。您将获得所需的确切效果。

def custom_loss(true, pred):
    n = K.shape(pred)[0:1]

    pred = K.concatenate([pred]*2, axis=-1) #duplicate in the last axis
    pred = K.flatten(pred)                  #flatten 
    pred = K.slice(pred,                    #take only half (= n samples)
                   K.constant([0], dtype="int32"), 
                   n) 

    return K.abs(true - pred)

损失函数的解决方案:

如果您按从大到小的顺序对时间进行排序,则只需进行累计即可。

  

警告:如果每个样本一次,则无法进行迷你批次训练!!
   batch_size = len(labels)

像在循环和一维转换网络中那样,在附加维度上有时间是有意义的(每个样本很多次)。无论如何,考虑到您所表示的示例,对于(samples_equal_times,),形状为yTime

def neg_log_likelihood(yTrue,yPred):
    yStatus = yTrue[:,0]
    yTime = yTrue[:,1]    
    n = K.shape(yTrue)[0]    


    #sort the times and everything else from greater to lower:
    #obs, you can have the data sorted already and avoid doing it here for performance

    #important, yTime will be sorted in the last dimension, make sure its (None,) in this case
    # or that it's (None, time_length) in the case of many times per sample
    sortedTime, sortedIndices = tf.math.top_k(yTime, n, True)    
    sortedStatus = K.gather(yStatus, sortedIndices)
    sortedPreds = K.gather(yPred, sortedIndices)

    #do the calculations
    exp = K.exp(sortedPreds)
    sums = K.cumsum(exp)  #this will have the sum for j >= i in the loop
    logsums = K.log(sums)

    return K.sum(sortedStatus * sortedPreds - logsums)