简体   繁体   English

In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()?

[英]In Tensorflow.keras 2.0, when a model has multiple outputs, how to define a flexible loss function for model.fit()?

Let's say here is a model with two outputs.假设这里是一个具有两个输出的 model。

import tensorflow as tf
import numpy as np 

x = tf.keras.Input(shape=(35, 7), dtype=tf.float32)     # (None, 35, 7)
net = tf.keras.layers.Dense(11, activation='relu')(x)   # (None, 35, 11)
net = tf.reduce_max(net, axis=1, name='maxpool')        # (None, 11)

a = tf.keras.layers.Dense(13, activation='relu')(net)   # (None, 35, 11)
b = tf.keras.layers.Dense(17, activation='relu')(net)   # (None, 35, 11)
model = tf.keras.Model(inputs=x, outputs=[a, b])

When I do model.compile(loss=loss_fn, optimizer='sgd') :当我做model.compile(loss=loss_fn, optimizer='sgd')
the model.fit(x=train, y=(label1, label2)) runs loss_fn for each pair of output and label (ie, loss_fn(a, l1) and loss_fn(b, l1) ). the model.fit(x=train, y=(label1, label2)) runs loss_fn for each pair of output and label (ie, loss_fn(a, l1) and loss_fn(b, l1) ).

When I do model.compile(loss=[loss_fn1, loss_fn2], optimizer='sgd') :当我做model.compile(loss=[loss_fn1, loss_fn2], optimizer='sgd')
the model.fit(x=train, y=(label1, label2)) runs loss_fn1 for a and loss_fn2 for b (ie, loss_fn1(a, l1) and loss_fn2(b, l1) ). model.fit(x=train, y=(label1, label2))a运行loss_fn1 ,为b运行loss_fn2 (即loss_fn1(a, l1)loss_fn2(b, l1) )。

So, basically it seems to handle outputs individually (paired with given corresponding labels).因此,基本上它似乎单独处理输出(与给定的相应标签配对)。

What if I have to define a loss function that should handle/consider both outputs together, and use the function with model.fit ?如果我必须定义一个应该同时处理/考虑两个输出的损失 function 并将 function 与model.fit一起使用怎么办?

(One thing I can think of is to concatenate outputs into one tensor, and separate them in a loss function. However, I don't want to go there since two output may not have consistent shape. Instead, is it possible, for example, something like...) (我能想到的一件事是将输出连接到一个张量中,并将它们分离为损失 function。但是,我不想 go 在那里,因为两个 Z78E6221F6393D,1356681 可能有不一致的形状。 , 就像是...)

def loss_fn(y_true, y_pred):
    # I want to access both output ...
    l1, l2 = y_true
    a, b = y_pred
    # ... do something about loss ...
    return loss

You would concatenate your two Dense layers, and do exactly the same as you mentioned:您将连接您的两个 Dense 层,并执行与您提到的完全相同的操作:

import numpy as np
from tensorflow.keras.layers import Input, Dense, Concatenate
from tensorflow.keras.models import Model
import tensorflow.keras.backend as K

i = Input((10,))
x = Dense(10)(i)
a = Dense(3, use_bias=False)(x)
b = Dense(3, use_bias=False)(x)
# Now you concatenate both outputs,
# so nothing happens to them
c = Concatenate()([a,b])
m = Model(i, c)

def loss(y_true, y_pred):
    # Do your loss on your subset
    a, b  = y_pred[:, :3], y_pred[:, 3:]
    # Do something random
    return K.mean(a*b)

m.compile("adam", loss)

m.fit(np.random.random((10, 10)),
      np.random.random((10, 6)))

# Outputs:
# 10/10 [=======] - 0s 22ms/sample - loss: -0.2251

edit;编辑; haven't seen that actually @bit01 commented already the to go approach还没有看到实际上@bit01 已经评论了 go 方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将自定义数据生成器输入到 model.fit 中,它会生成 X,y 和一个额外的数组,到 tensorflow.keras Z20F4ZF011622Z Z20F4ZF35E630DAF39466 - How to input custom data generator into model.fit, which generates X,y and one additional array, into tensorflow.keras model? 我可以在具有自定义损失的多输出模型上使用 Keras model.fit(),该模型使用 Tensorflow 2 中的所有输出目标和预测吗? - Can I use Keras model.fit() on a multi-output model with custom loss that uses all outputs' targets and predictions in Tensorflow 2? 如何将 Keras 中的多个输入的标签赋予 model.fit() function? - How to give labels of multiple inputs in Keras to model.fit() function? 在Keras中调用model.fit时无法从损失函数运行打印语句 - unable to run print statements from loss function when calling model.fit in Keras Tensorflow Keras:运行model.fit时出现尺寸/形状错误 - Tensorflow Keras: Dimension/Shape Error when running model.fit NotImplementedError 运行 model.fit 时在 Tensorflow Keras - NotImplementedError when running model.fit in Tensorflow Keras Tensorflow Keras:在 model.fit() 上进行训练 - Tensorflow Keras: Training Holting on model.fit() 使用自定义损失 function 时 model.fit() 出错 - Error in model.fit() when using custom loss function Keras模型到tensorflow.keras - Keras model to tensorflow.keras 如何将 tensorflow.keras 模型移动到 GPU - How to move a tensorflow.keras model to GPU
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM