jack06215

[keras][tutorial] custom loss using closure

Oct 5th, 2020 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. # https://github.com/keras-team/keras/issues/2121#issuecomment-214551349
  2. def penalized_loss(noise):
  3.     def loss(y_true, y_pred):
  4.         return K.mean(K.square(y_pred - y_true) - K.square(y_true - noise), axis=-1)
  5.     return loss
  6.  
  7.  
  8. input1 = Input(batch_shape=(batch_size, timesteps, features))
  9. lstm =  LSTM(features, stateful=True, return_sequences=True)(input1)
  10. output1 = TimeDistributed(Dense(features, activation='sigmoid'))(lstm)
  11. output2 = TimeDistributed(Dense(features, activation='sigmoid'))(lstm)
  12. model = Model(input=[input1], output=[output1, output2])
  13. model.compile(loss=[penalized_loss(noise=output2), penalized_loss(noise=output1)], optimizer='rmsprop')
Add Comment
Please, Sign In to add comment