多输出单损

时间:2019-11-13 11:04:05

标签: python tensorflow keras loss

我想在Keras / Tensorflow中创建一个具有多个输出的神经网络。我想创建一个将所有输出都考虑在内的SINGLE损失函数,并据此计算损失。我需要这样做,因为输出彼此相关。我该如何实现?我读到有关将所有输出串联到单个密集层,然后计算该层的损耗的信息。有没有更便捷的方法来实现多输出的单一损失?

我在想类似的东西:

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    scene: {
        create: create,
        update: update
    }
};

var keySpace;
var keyBackspace;
var textEntry;

var game = new Phaser.Game(config);

function create ()
{
    this.add.text(10, 10, 'Enter your name:', { font: '32px Courier', fill: '#ffffff' });

    textEntry = this.add.text(10, 50, '', { font: '32px Courier', fill: '#ffff00' });

    // keys = this.input.keyboard.addKeys('A,B,C');

    keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
    keyBackspace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.BACKSPACE);

    this.input.keyboard.on('keydown', function (event) {

        if (event.keyCode === 8 && textEntry.text.length > 0)
        {
            textEntry.text = textEntry.text.substr(0, textEntry.text.length - 1);
        }
        else if (event.keyCode === 32 || (event.keyCode >= 48 && event.keyCode < 90))
        {
            textEntry.text += event.key;
        }

        console.log(event);
    });
}

function update ()
{
}

else if (event.keyCode === 38 ) { dosomethingthirtyeight(); showPanel(); } else if (event.keyCode === 40 ) { dosomethingfortyeight(); showPanel(); } def my_custom_loss(y_true_0, ..., y_true_n, y_pred_0, ..., y_pred_n): return something

应该是n个输出(密集)层的真实/预测输出。

1 个答案:

答案 0 :(得分:0)

您可以根据变量的性质实现损失函数。一些标准的方法如下:

如果它们只是数字(而不是概率): MSE损失

def my_custom_loss(y_true_0, ..., y_true_n, y_pred_0, ..., y_true_n):
   y_true = tf.stack([y_true_0,...y_true_n], axis=0)
   y_pred = tf.stack([y_pred_0,...y_pred_n], axis=0)
   something = tf.losses.mean_squared_error(y_true, y_pred)
   return something

OR 绝对差额损失

def my_custom_loss(y_true_0, ..., y_true_n, y_pred_0, ..., y_true_n):
   y_true = tf.stack([y_true_0,...y_true_n], axis=0)
   y_pred = tf.stack([y_pred_0,...y_pred_n], axis=0)
   something = tf.losses.absolute_difference(y_true, y_pred)
   return something

如果它们是一个热门向量(有效概率):

def my_custom_loss(y_true_0, ..., y_true_n, y_pred_0, ..., y_true_n):
   y_true = tf.stack([y_true_0,...y_true_n], axis=0)
   y_pred = tf.stack([y_pred_0,...y_pred_n], axis=0)
   something = tf.reduce_mean(tf.keras.losses.binary_crossentropy(y_true, y_pred))
   return something

如果它们是零和一(无效的概率):

def my_custom_loss(y_true_0, ..., y_true_n, y_pred_0, ..., y_true_n):
   y_true = tf.stack([y_true_0,...y_true_n], axis=0)
   y_pred = tf.stack([y_pred_0,...y_pred_n], axis=0)
   something = tf.reduce_mean(tf.keras.losses.binary_crossentropy(y_true, y_pred), from_logits=True)
   return something

不仅限于这些。您可以创建自己的损失函数,只要它是可微的。