标签: loss-function

如何在Keras中进行逐点分类的交叉熵损失?

我有一个产生4D输出张量的网络,其中空间维度(〜像素)中每个位置的值将被解释为该位置的类概率.换句话说,输出是(num_batches, height, width, num_classes).我有相同大小的标签,其中真实类被编码为一热.我想categorical-crossentropy用这个计算损失.

问题#1:K.softmax函数需要2D张量(num_batches, num_classes)

问题2:我不确定每个位置的损失应该如何组合.reshape张量是否正确(num_batches * height * width, num_classes)然后再呼吁K.categorical_crossentropy?或者更确切地说,调用K.categorical_crossentropy(num_batches, num_classes)高度*宽度乘以平均结果?

machine-learning neural-network keras cross-entropy loss-function

6
推荐指数
2
解决办法
3566
查看次数

如何在Keras模型中使用TensorFlow的采样softmax损失函数?

我正在Keras训练语言模型,并希望通过使用采样softmax作为我网络中的最终激活功能来加速训练.从TF文档中,看起来我需要为weights和提供参数biases,但我不确定这些的输入是什么.好像我可以在Keras中编写自定义函数,如下所示:

import keras.backend as K

def sampled_softmax(weights, biases, y_true, y_pred, num_sampled, num_classes):
    return K.sampled_softmax(weights, biases, y_true, y_pred, num_sampled, num_classes)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何"插入"到我现有的网络.LM的架构非常简单:

model = Sequential()
model.add(Embedding(input_dim=len(vocab), output_dim=256))
model.add(LSTM(1024, return_sequence=True))
model.add(Dense(output_dim=len(vocab), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
Run Code Online (Sandbox Code Playgroud)

鉴于这种架构,我可以在模型上调用编译方法时将sampled_softmax函数作为loss参数传递吗?或者这需要写为最终完全连接层之后的层.这里的任何指导将不胜感激.谢谢.

deep-learning keras tensorflow loss-function

6
推荐指数
1
解决办法
1440
查看次数

多类别细分的广义骰子损失:keras实现

我刚刚在keras中实现了广义骰子损失(骰子损失的多类版本),如ref中所述

(我的目标定义为:(batch_size,image_dim1,image_dim2,image_dim3,nb_of_classes))

def generalized_dice_loss_w(y_true, y_pred): 
    # Compute weights: "the contribution of each label is corrected by the inverse of its volume"
    Ncl = y_pred.shape[-1]
    w = np.zeros((Ncl,))
    for l in range(0,Ncl): w[l] = np.sum( np.asarray(y_true[:,:,:,:,l]==1,np.int8) )
    w = 1/(w**2+0.00001)

    # Compute gen dice coef:
    numerator = y_true*y_pred
    numerator = w*K.sum(numerator,(0,1,2,3))
    numerator = K.sum(numerator)

    denominator = y_true+y_pred
    denominator = w*K.sum(denominator,(0,1,2,3))
    denominator = K.sum(denominator)

    gen_dice_coef = numerator/denominator

    return 1-2*gen_dice_coef
Run Code Online (Sandbox Code Playgroud)

但是一定有问题。我正在处理3D图像,必须将其细分为4类(1个背景类和3个对象类,我有一个不平衡的数据集)。首先奇怪的事情:当我的火车损失和准确性训练中提高(和收敛非常快),我确认损失/精度是恒定的低谷时期(见图片)。其次,在对测试数据进行预测时,仅预测了背景类:我得到了恒定的体积。

我使用了完全相同的数据和脚本,但存在分类交叉熵损失,并得到了合理的结果(对象类别已细分)。这意味着我的实现存在问题。知道会是什么吗?

另外,我认为具有通用的骰子丢失实现对keras社区很有用,因为它似乎已用于大多数最近的语义分割任务中(至少在医学图像社区中)。

PS:对我来说,如何定义权重似乎很奇怪;我得到大约10 ^ -10的值。还有其他人尝试实现吗?我也测试了我的功能,但没有权重,但是遇到了同样的问题。

machine-learning deep-learning keras loss-function semantic-segmentation

6
推荐指数
1
解决办法
2680
查看次数

Keras - 自定义损失函数/访问张量的第 75 个百分位元素

我正在尝试为 Keras 中的模型实现一个稍微修改的二元交叉熵损失函数。在 Keras 中,binary_crossentropy 定义为:

def binary_crossentropy(y_true, y_pred):
  return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
Run Code Online (Sandbox Code Playgroud)

我拥有的数据是分组的(即有一列指示 group1、group2 等)但每个组的行数不同(即 group1 有 52 个观察值,group2 有 101 个观察值等)。

理想情况下,我想找到每个组的平均二元交叉熵,并返回最大平均二元交叉熵(最大的平均二元交叉熵,按组)。

似乎没有使用组的任何现成的解决方案,我无法想出一个解决方案。关于观察属于哪个组的信息会丢失,并且不会传递到 y_true 和 y_pred,而且我不确定 k-fold cv 将如何准确地改变作为 y_true 和 y_pred 传递的观察值/数量。如果有办法通过 Sequential 模型保留组信息,那可能就是解决方案。代码将完成类似的事情:

def custom_loss(y_true, y_pred):
  max_bc = []
  for group in groups:
     max_bc += [K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)]
  return max_bc
Run Code Online (Sandbox Code Playgroud)

如果上述方法不可行,另一种度量可能是张量的第 75 个百分位值。就像是:

def custom_loss(y_true, y_pred):
  return K.binary_crossentropy(y_true, y_pred)[len(y_true)*0.75]
Run Code Online (Sandbox Code Playgroud)

但我确信这是错误的——我只是对 Keras 和 Tensorflow 不够熟悉,无法获得正确的代码。

编辑:我可能刚刚找到了一种方法来做百分位数,但结果并不如预期......对第一部分有所了解仍然很棒。

def custom_loss(y_true, y_pred):
  e = K.binary_crossentropy(y_true,y_pred)
  return distributions.percentile(e, q=75.)
Run Code Online (Sandbox Code Playgroud)

python sequential keras tensorflow loss-function

6
推荐指数
0
解决办法
588
查看次数

Keras:加权二进制交叉熵实现

我是Keras(以及ML)的新手,并且正在尝试训练二进制分类器。我正在使用加权二进制交叉熵作为损失函数,但不确定如何测试实现是否正确。

这是加权二进制交叉熵的准确实现吗?我该如何测试呢?

def weighted_binary_crossentropy(self, y_true, y_pred):
    logloss = -(y_true * K.log(y_pred) * self.weights[0] + \
               (1 - y_true) * K.log(1 - y_pred) * self.weights[1])

    return K.mean(logloss, axis=-1)
Run Code Online (Sandbox Code Playgroud)

python machine-learning keras cross-entropy loss-function

6
推荐指数
1
解决办法
1220
查看次数

用条件在keras中实现自定义丢失函数

我需要一些keras损失功能的帮助.我一直在使用Tensorflow后端在keras上实现自定义丢失功能.

我已经在numpy中实现了自定义丢失函数,但如果它可以转换为keras损失函数则会很棒.loss函数采用数据帧和一系列用户id.如果user_id不同,则相同user_id的欧几里德距离为正和负.该函数返回数据帧的标量距离的总和.

def custom_loss_numpy (encodings, user_id):
# user_id: a pandas series of users
# encodings: a pandas dataframe of encodings

    batch_dist = 0

    for i in range(len(user_id)):
         first_row = encodings.iloc[i,:].values
         first_user = user_id[i]

         for j in range(i+1, len(user_id)):
              second_user = user_id[j]
              second_row = encodings.iloc[j,:].values

        # compute distance: if the users are same then Euclidean distance is positive otherwise negative.
            if first_user == second_user:
                tmp_dist = np.linalg.norm(first_row - second_row)
            else:
                tmp_dist = -np.linalg.norm(first_row - second_row)

            batch_dist += tmp_dist

    return batch_dist
Run Code Online (Sandbox Code Playgroud)

我试图实现keras损失功能.我从y_true和y_pred张量对象中提取了numpy数组. …

python keras tensorflow loss-function

6
推荐指数
2
解决办法
6336
查看次数

R Keras中的自定义丢失功能

我想计算加权均方误差,其中权重是数据中的一个向量.我根据堆栈溢出提供的建议编写了一个自定义代码.

该功能如下:

weighted_mse <- function(y_true, y_pred,weights){
  # convert tensors to R objects
  K        <- backend()
  y_true   <- K$eval(y_true)
  y_pred   <- K$eval(y_pred)
  weights  <- K$eval(weights)

  # calculate the metric
  loss <- sum(weights*((y_true - y_pred)^2)) 

  # convert to tensor
  return(K$constant(loss))
  }
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何将自定义函数传递给编译器.如果有人可以帮助我会很棒.谢谢.

model      <- model %>% compile(
                loss = 'mse', 
                optimizer = 'rmsprop',
                metrics = 'mse')
Run Code Online (Sandbox Code Playgroud)

问候

python r keras loss-function

6
推荐指数
1
解决办法
1395
查看次数

在Keras中实现批次依赖性损失

我在Keras中设置了自动编码器。我希望能够根据预定的“精度”向量加权输入向量的特征。此连续值向量的长度与输入的长度相同,并且每个元素都位于范围内[0, 1],与相应输入元素的置信度相对应,其中1是完全置信度,0是无置信度。

对于每个示例,我都有一个精确向量。

我定义了一种损失,其中考虑了该精度向量。在这里,低置信度特征的重构权重降低。

def MAEpw_wrapper(y_prec):
    def MAEpw(y_true, y_pred):
        return K.mean(K.square(y_prec * (y_pred - y_true)))
    return MAEpw
Run Code Online (Sandbox Code Playgroud)

我的问题是精度张量y_prec取决于批次。我希望能够y_prec根据当前批次进行更新,以使每个精度向量都与其观察值正确关联。

我做了以下工作:

global y_prec
y_prec = K.variable(P[:32])
Run Code Online (Sandbox Code Playgroud)

P是一个包含所有精度向量的numpy数组,其索引与示例相对应。我初始化y_prec为批次大小为32的正确形状。然后定义以下内容DataGenerator

class DataGenerator(Sequence):

    def __init__(self, batch_size, y, shuffle=True):

        self.batch_size = batch_size

        self.y = y

        self.shuffle = shuffle
        self.on_epoch_end()

    def on_epoch_end(self):
        self.indexes = np.arange(len(self.y))

        if self.shuffle == True:
            np.random.shuffle(self.indexes)

    def __len__(self):
        return int(np.floor(len(self.y) / self.batch_size))

    def __getitem__(self, index):
        indexes = self.indexes[index * self.batch_size: (index+1) * …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow loss-function

6
推荐指数
1
解决办法
476
查看次数

Keras 自定义损失函数(弹性网)

我正在尝试编写 Elastic-Net 代码。它看起来像:

elticnet公式

我想在 Keras 中使用这个损失函数:

def nn_weather_model():
    ip_weather = Input(shape = (30, 38, 5))
    x_weather = BatchNormalization(name='weather1')(ip_weather)
    x_weather = Flatten()(x_weather)
    Dense100_1 = Dense(100, activation='relu', name='weather2')(x_weather)
    Dense100_2 = Dense(100, activation='relu', name='weather3')(Dense100_1)
    Dense18 = Dense(18, activation='linear', name='weather5')(Dense100_2)
    model_weather = Model(inputs=[ip_weather], outputs=[Dense18])
    model = model_weather
    ip = ip_weather
    op = Dense18
    return model, ip, op
Run Code Online (Sandbox Code Playgroud)

我的损失函数是:

def cost_function(y_true, y_pred):
        return ((K.mean(K.square(y_pred - y_true)))+L1+L2)
   return cost_function
Run Code Online (Sandbox Code Playgroud)

它是 mse+L1+L2

L1 和 L2 是

weight1=model.layers[3].get_weights()[0]
weight2=model.layers[4].get_weights()[0]
weight3=model.layers[5].get_weights()[0]
L1 = Calculate_L1(weight1,weight2,weight3)
L2 = Calculate_L2(weight1,weight2,weight3)
Run Code Online (Sandbox Code Playgroud)

我使用Calculate_L1 函数来计算dense1、dense2 …

python machine-learning keras tensorflow loss-function

6
推荐指数
1
解决办法
1273
查看次数

在 Keras/Tensorflow 自定义损失函数中使用额外的“可训练”变量

我知道如何在 Keras 中使用附加输入(而不是标准y_true,y_pred对)编写自定义损失函数,请参见下文。我的问题是使用可训练变量(其中一些)输入损失函数,该变量是损失梯度的一部分,因此应该更新。

我的解决方法是:

  • 在网络中输入NXV大小的虚拟输入,其中N是观测值的数量和V附加变量的数量
  • 添加一个Dense()dummy_output,以便 Keras 跟踪我的V“体重”
  • V在我的自定义损失函数中使用该层的权重作为我的真实输出层
  • dummy_output对于该层使用虚拟损失函数(仅返回 0.0 和/或权重 0.0),因此我的V“权重”仅通过我的自定义损失函数进行更新

我的问题是:是否有更自然的类似 Keras/TF 的方法来做到这一点?因为它感觉很做作,更不用说容易出现错误了。

我的解决方法的示例:

(是的,我知道这是一个非常愚蠢的自定义损失函数,实际上事情要复杂得多)

import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import EarlyStopping
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Input
from tensorflow.keras import Model

n_col = 10
n_row = …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow loss-function

6
推荐指数
1
解决办法
3288
查看次数