title: Keras load_model with custom objects doesn't work properly
body: <p><strong>Setting</strong></p>
  
  <p>As already mentioned in the title, I got a problem with my custom loss function, when trying to load the saved model. My loss looks as follows:</p>
  
  <pre><code>def weighted_cross_entropy(weights):
  
      weights = K.variable(weights)
  
      def loss(y_true, y_pred):
          y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
  
          loss = y_true * K.log(y_pred) * weights
          loss = -K.sum(loss, -1)
          return loss
  
      return loss
  
  weighted_loss = weighted_cross_entropy([0.1,0.9])
  </code></pre>
  
  <p>So during training, I used the <code>weighted_loss</code> function as loss function and everything worked well. When training is finished I save the model as <code>.h5</code>file with the standard <code>model.save</code> function from keras API.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>When I am trying to load the model via</p>
  
  <pre><code>model = load_model(path,custom_objects={"weighted_loss":weighted_loss})
  </code></pre>
  
  <p>I am getting a <code>ValueError</code> telling me that the loss is unknown.</p>
  
  <p><strong>Error</strong></p>
  
  <p>The error message looks as follows:</p>
  
  <pre><code>File "...\predict.py", line 29, in my_script
  "weighted_loss": weighted_loss})
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 419, in load_model
  model = _deserialize_model(f, custom_objects, compile)
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\saving.py", line 312, in _deserialize_model
  sample_weight_mode=sample_weight_mode)
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\training.py", line 139, in compile
  loss_function = losses.get(loss)
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 133, in get
  return deserialize(identifier)
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\losses.py", line 114, in deserialize
  printable_module_name='loss function')
  File "...\Continuum\anaconda3\envs\processing\lib\site-packages\keras\utils\generic_utils.py", line 165, in deserialize_keras_object
  ':' + function_name)
  ValueError: Unknown loss function:loss
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <p>How can I fix this problem? May it be possible that the reason for that is my wrapped loss definition? So <code>keras</code> doesn't know, how to handle the <code>weights</code> variable?</p>