29

Keras で複数出力モデルを使用しています

model1 = Model(input=x, output=[y2, y3])

model1.compile((optimizer='sgd', loss=cutom_loss_function)

私のcustom_loss機能は

def custom_loss(y_true, y_pred):
   y2_pred = y_pred[0]
   y2_true = y_true[0]

   loss = K.mean(K.square(y2_true - y2_pred), axis=-1)
   return loss

output でネットワークをトレーニングしたいだけですy2

複数の出力が使用される場合の損失関数のy_predand引数の形状/構造は何ですか? y_true上記のようにアクセスできますか? y_pred[0]それとも?y_pred[:,0]_

4

3 に答える 3

2

Sharapolas' answer is right.

However, there is a better way than using a layer for building custom loss functions with complex interdependence of several outputs of a model.

The method I know is being used in practice is by never calling model.compile but only model._make_predict_function(). From there on, you can go on and build a custom optimizer method by calling model.output in there. This will give you all outputs, [y2,y3] in your case. When doing your magic with it, get a keras.optimizer and use it's get_update method using your model.trainable_weights and your loss. Finally, return a keras.function with a list of the inputs required (in your case only model.input) and the updates you just got from the optimizer.get_update call. This function now replaces model.fit.

The above is often used in PolicyGradient algorithms, like A3C or PPO. Here is an example of what I tried to explain: https://github.com/Hyeokreal/Actor-Critic-Continuous-Keras/blob/master/a2c_continuous.py Look at build_model and critic_optimizer methods and read kreas.backend.function documentation to understand what happens.

I found this way to have frequently problems with the session management and does not appear to work in tf-2.0 keras at all currently. Hence, if anyone knows a method, please let me know. I came here looking for one :)

于 2019-04-28T21:52:47.347 に答える