prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Wrong result for cosine proximity: keras 2.0.8
  # Conclusion: Observation of keras cosine proximity stuck as -1/3 #
  As noted by numerous post, Keras seriously currently has an issue with cosine proximity:
  
  https://github.com/fchollet/keras/issues/3031
  https://github.com/fchollet/keras/issues/5046
  
  Here is the code in jupyter notebook for simple test:
  
  
  ```
  import keras
  from keras.layers import Input, Dense
  from keras.models import Model
  import numpy as np
  
  # --> print keras version
  print keras.__version__
  
  # --> compute average cosine between all angles samples
  def computeMeanConsineAngle(x,y):
      cosMean = 0
      numSample = x.shape[0]
      for i in xrange(numSample):
          cosMean += np.dot(x[i,:],y[i,:])/np.sqrt(np.dot(x[i,:],x[i,:])*np.dot(y[i,:],y[i,:]))
          
      return cosMean/float(numSample)
  
  X = np.random.random((1000,3))
  Y = X
  
  inputs = Input(shape=(3,))
  preds = Dense(3,activation='linear')(inputs)
  model = Model(inputs=inputs,outputs=preds)
  
  sgd=keras.optimizers.Adam(lr=1e-2)
  model.compile(optimizer=sgd ,loss='mse',metrics=['cosine_proximity'])
  model.fit(X,Y, batch_size=1000, epochs=500, shuffle=False)
  
  pred = model.predict(X)
  
  from sklearn.metrics import mean_squared_error
  mse = mean_squared_error(X, pred)
  
  
  %pylab
  %matplotlib inline
  plt.scatter(pred,Y)
  
  print 'mse = ', mse
  print computeMeanConsineAngle(pred, Y)
  
  testX = np.array([[1,0]])
  testY = np.array([[1,0]])
  - computeMeanConsineAngle(testX,testY)
  ```
  
  The printed result is 
  ```
  Epoch 500/500
  1000/1000 [==============================] - 0s - loss: 7.1132e-04 
  - cosine_proximity: -0.3329
  Using matplotlib backend: TkAgg
  Populating the interactive namespace from numpy and matplotlib
  mse =  0.000703760391565
  0.998615947541
  ```
  
  **So the true cosine proximity is actually 0.9986, but keras shows near -1/3. Of course keras would use the negative of cosine proximity for minimization purpose, but it should be -0.9986.., in any case, don't trust the outcome of metric in keras cosine proximity**
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `keras/losses.py`
  Content:
  ```
  1 from __future__ import absolute_import
  2 import six
  3 from . import backend as K
  4 from .utils.generic_utils import deserialize_keras_object
  5 
  6 
  7 # noinspection SpellCheckingInspection
  8 def mean_squared_error(y_true, y_pred):
  9     return K.mean(K.square(y_pred - y_true), axis=-1)
  10 
  11 
  12 def mean_absolute_error(y_true, y_pred):
  13     return K.mean(K.abs(y_pred - y_true), axis=-1)
  14 
  15 
  16 def mean_absolute_percentage_error(y_true, y_pred):
  17     diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
  18                                             K.epsilon(),
  19                                             None))
  20     return 100. * K.mean(diff, axis=-1)
  21 
  22 
  23 def mean_squared_logarithmic_error(y_true, y_pred):
  24     first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
  25     second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
  26     return K.mean(K.square(first_log - second_log), axis=-1)
  27 
  28 
  29 def squared_hinge(y_true, y_pred):
  30     return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)
  31 
  32 
  33 def hinge(y_true, y_pred):
  34     return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
  35 
  36 
  37 def categorical_hinge(y_true, y_pred):
  38     pos = K.sum(y_true * y_pred, axis=-1)
  39     neg = K.max((1. - y_true) * y_pred, axis=-1)
  40     return K.maximum(0., neg - pos + 1.)
  41 
  42 
  43 def logcosh(y_true, y_pred):
  44     def cosh(x):
  45         return (K.exp(x) + K.exp(-x)) / 2
  46     return K.mean(K.log(cosh(y_pred - y_true)), axis=-1)
  47 
  48 
  49 def categorical_crossentropy(y_true, y_pred):
  50     return K.categorical_crossentropy(y_true, y_pred)
  51 
  52 
  53 def sparse_categorical_crossentropy(y_true, y_pred):
  54     return K.sparse_categorical_crossentropy(y_true, y_pred)
  55 
  56 
  57 def binary_crossentropy(y_true, y_pred):
  58     return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
  59 
  60 
  61 def kullback_leibler_divergence(y_true, y_pred):
  62     y_true = K.clip(y_true, K.epsilon(), 1)
  63     y_pred = K.clip(y_pred, K.epsilon(), 1)
  64     return K.sum(y_true * K.log(y_true / y_pred), axis=-1)
  65 
  66 
  67 def poisson(y_true, y_pred):
  68     return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)
  69 
  70 
  71 def cosine_proximity(y_true, y_pred):
  72     y_true = K.l2_normalize(y_true, axis=-1)
  73     y_pred = K.l2_normalize(y_pred, axis=-1)
  74     return -K.mean(y_true * y_pred, axis=-1)
  75 
  76 
  77 # Aliases.
  78 
  79 mse = MSE = mean_squared_error
  80 mae = MAE = mean_absolute_error
  81 mape = MAPE = mean_absolute_percentage_error
  82 msle = MSLE = mean_squared_logarithmic_error
  83 kld = KLD = kullback_leibler_divergence
  84 cosine = cosine_proximity
  85 
  86 
  87 def serialize(loss):
  88     return loss.__name__
  89 
  90 
  91 def deserialize(name, custom_objects=None):
  92     return deserialize_keras_object(name,
  93                                     module_objects=globals(),
  94                                     custom_objects=custom_objects,
  95                                     printable_module_name='loss function')
  96 
  97 
  98 def get(identifier):
  99     if identifier is None:
  100         return None
  101     if isinstance(identifier, six.string_types):
  102         identifier = str(identifier)
  103         return deserialize(identifier)
  104     elif callable(identifier):
  105         return identifier
  106     else:
  107         raise ValueError('Could not interpret '
  108                          'loss function identifier:', identifier)
  109
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/keras/losses.py b/keras/losses.py
  --- a/keras/losses.py
  +++ b/keras/losses.py
  @@ -71,7 +71,7 @@
   def cosine_proximity(y_true, y_pred):
       y_true = K.l2_normalize(y_true, axis=-1)
       y_pred = K.l2_normalize(y_pred, axis=-1)
  -    return -K.mean(y_true * y_pred, axis=-1)
  +    return -K.sum(y_true * y_pred, axis=-1)
   
   
   # Aliases.
  
verification_info: {"golden_diff": "diff --git a/keras/losses.py b/keras/losses.py\n--- a/keras/losses.py\n+++ b/keras/losses.py\n@@ -71,7 +71,7 @@\n def cosine_proximity(y_true, y_pred):\n     y_true = K.l2_normalize(y_true, axis=-1)\n     y_pred = K.l2_normalize(y_pred, axis=-1)\n-    return -K.mean(y_true * y_pred, axis=-1)\n+    return -K.sum(y_true * y_pred, axis=-1)\n \n \n # Aliases.\n", "issue": "Wrong result for cosine proximity: keras 2.0.8\n# Conclusion: Observation of keras cosine proximity stuck as -1/3 #\r\nAs noted by numerous post, Keras seriously currently has an issue with cosine proximity:\r\n\r\nhttps://github.com/fchollet/keras/issues/3031\r\nhttps://github.com/fchollet/keras/issues/5046\r\n\r\nHere is the code in jupyter notebook for simple test:\r\n\r\n\r\n```\r\nimport keras\r\nfrom keras.layers import Input, Dense\r\nfrom keras.models import Model\r\nimport numpy as np\r\n\r\n# --> print keras version\r\nprint keras.__version__\r\n\r\n# --> compute average cosine between all angles samples\r\ndef computeMeanConsineAngle(x,y):\r\n    cosMean = 0\r\n    numSample = x.shape[0]\r\n    for i in xrange(numSample):\r\n        cosMean += np.dot(x[i,:],y[i,:])/np.sqrt(np.dot(x[i,:],x[i,:])*np.dot(y[i,:],y[i,:]))\r\n        \r\n    return cosMean/float(numSample)\r\n\r\nX = np.random.random((1000,3))\r\nY = X\r\n\r\ninputs = Input(shape=(3,))\r\npreds = Dense(3,activation='linear')(inputs)\r\nmodel = Model(inputs=inputs,outputs=preds)\r\n\r\nsgd=keras.optimizers.Adam(lr=1e-2)\r\nmodel.compile(optimizer=sgd ,loss='mse',metrics=['cosine_proximity'])\r\nmodel.fit(X,Y, batch_size=1000, epochs=500, shuffle=False)\r\n\r\npred = model.predict(X)\r\n\r\nfrom sklearn.metrics import mean_squared_error\r\nmse = mean_squared_error(X, pred)\r\n\r\n\r\n%pylab\r\n%matplotlib inline\r\nplt.scatter(pred,Y)\r\n\r\nprint 'mse = ', mse\r\nprint computeMeanConsineAngle(pred, Y)\r\n\r\ntestX = np.array([[1,0]])\r\ntestY = np.array([[1,0]])\r\n- computeMeanConsineAngle(testX,testY)\r\n```\r\n\r\nThe printed result is \r\n```\r\nEpoch 500/500\r\n1000/1000 [==============================] - 0s - loss: 7.1132e-04 \r\n- cosine_proximity: -0.3329\r\nUsing matplotlib backend: TkAgg\r\nPopulating the interactive namespace from numpy and matplotlib\r\nmse =  0.000703760391565\r\n0.998615947541\r\n```\r\n\r\n**So the true cosine proximity is actually 0.9986, but keras shows near -1/3. Of course keras would use the negative of cosine proximity for minimization purpose, but it should be -0.9986.., in any case, don't trust the outcome of metric in keras cosine proximity**\r\n\n", "before_files": [{"content": "from __future__ import absolute_import\nimport six\nfrom . import backend as K\nfrom .utils.generic_utils import deserialize_keras_object\n\n\n# noinspection SpellCheckingInspection\ndef mean_squared_error(y_true, y_pred):\n    return K.mean(K.square(y_pred - y_true), axis=-1)\n\n\ndef mean_absolute_error(y_true, y_pred):\n    return K.mean(K.abs(y_pred - y_true), axis=-1)\n\n\ndef mean_absolute_percentage_error(y_true, y_pred):\n    diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),\n                                            K.epsilon(),\n                                            None))\n    return 100. * K.mean(diff, axis=-1)\n\n\ndef mean_squared_logarithmic_error(y_true, y_pred):\n    first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)\n    second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)\n    return K.mean(K.square(first_log - second_log), axis=-1)\n\n\ndef squared_hinge(y_true, y_pred):\n    return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)\n\n\ndef hinge(y_true, y_pred):\n    return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)\n\n\ndef categorical_hinge(y_true, y_pred):\n    pos = K.sum(y_true * y_pred, axis=-1)\n    neg = K.max((1. - y_true) * y_pred, axis=-1)\n    return K.maximum(0., neg - pos + 1.)\n\n\ndef logcosh(y_true, y_pred):\n    def cosh(x):\n        return (K.exp(x) + K.exp(-x)) / 2\n    return K.mean(K.log(cosh(y_pred - y_true)), axis=-1)\n\n\ndef categorical_crossentropy(y_true, y_pred):\n    return K.categorical_crossentropy(y_true, y_pred)\n\n\ndef sparse_categorical_crossentropy(y_true, y_pred):\n    return K.sparse_categorical_crossentropy(y_true, y_pred)\n\n\ndef binary_crossentropy(y_true, y_pred):\n    return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)\n\n\ndef kullback_leibler_divergence(y_true, y_pred):\n    y_true = K.clip(y_true, K.epsilon(), 1)\n    y_pred = K.clip(y_pred, K.epsilon(), 1)\n    return K.sum(y_true * K.log(y_true / y_pred), axis=-1)\n\n\ndef poisson(y_true, y_pred):\n    return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)\n\n\ndef cosine_proximity(y_true, y_pred):\n    y_true = K.l2_normalize(y_true, axis=-1)\n    y_pred = K.l2_normalize(y_pred, axis=-1)\n    return -K.mean(y_true * y_pred, axis=-1)\n\n\n# Aliases.\n\nmse = MSE = mean_squared_error\nmae = MAE = mean_absolute_error\nmape = MAPE = mean_absolute_percentage_error\nmsle = MSLE = mean_squared_logarithmic_error\nkld = KLD = kullback_leibler_divergence\ncosine = cosine_proximity\n\n\ndef serialize(loss):\n    return loss.__name__\n\n\ndef deserialize(name, custom_objects=None):\n    return deserialize_keras_object(name,\n                                    module_objects=globals(),\n                                    custom_objects=custom_objects,\n                                    printable_module_name='loss function')\n\n\ndef get(identifier):\n    if identifier is None:\n        return None\n    if isinstance(identifier, six.string_types):\n        identifier = str(identifier)\n        return deserialize(identifier)\n    elif callable(identifier):\n        return identifier\n    else:\n        raise ValueError('Could not interpret '\n                         'loss function identifier:', identifier)\n", "path": "keras/losses.py"}], "after_files": [{"content": "from __future__ import absolute_import\nimport six\nfrom . import backend as K\nfrom .utils.generic_utils import deserialize_keras_object\n\n\n# noinspection SpellCheckingInspection\ndef mean_squared_error(y_true, y_pred):\n    return K.mean(K.square(y_pred - y_true), axis=-1)\n\n\ndef mean_absolute_error(y_true, y_pred):\n    return K.mean(K.abs(y_pred - y_true), axis=-1)\n\n\ndef mean_absolute_percentage_error(y_true, y_pred):\n    diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),\n                                            K.epsilon(),\n                                            None))\n    return 100. * K.mean(diff, axis=-1)\n\n\ndef mean_squared_logarithmic_error(y_true, y_pred):\n    first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)\n    second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)\n    return K.mean(K.square(first_log - second_log), axis=-1)\n\n\ndef squared_hinge(y_true, y_pred):\n    return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)\n\n\ndef hinge(y_true, y_pred):\n    return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)\n\n\ndef categorical_hinge(y_true, y_pred):\n    pos = K.sum(y_true * y_pred, axis=-1)\n    neg = K.max((1. - y_true) * y_pred, axis=-1)\n    return K.maximum(0., neg - pos + 1.)\n\n\ndef logcosh(y_true, y_pred):\n    def cosh(x):\n        return (K.exp(x) + K.exp(-x)) / 2\n    return K.mean(K.log(cosh(y_pred - y_true)), axis=-1)\n\n\ndef categorical_crossentropy(y_true, y_pred):\n    return K.categorical_crossentropy(y_true, y_pred)\n\n\ndef sparse_categorical_crossentropy(y_true, y_pred):\n    return K.sparse_categorical_crossentropy(y_true, y_pred)\n\n\ndef binary_crossentropy(y_true, y_pred):\n    return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)\n\n\ndef kullback_leibler_divergence(y_true, y_pred):\n    y_true = K.clip(y_true, K.epsilon(), 1)\n    y_pred = K.clip(y_pred, K.epsilon(), 1)\n    return K.sum(y_true * K.log(y_true / y_pred), axis=-1)\n\n\ndef poisson(y_true, y_pred):\n    return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)\n\n\ndef cosine_proximity(y_true, y_pred):\n    y_true = K.l2_normalize(y_true, axis=-1)\n    y_pred = K.l2_normalize(y_pred, axis=-1)\n    return -K.sum(y_true * y_pred, axis=-1)\n\n\n# Aliases.\n\nmse = MSE = mean_squared_error\nmae = MAE = mean_absolute_error\nmape = MAPE = mean_absolute_percentage_error\nmsle = MSLE = mean_squared_logarithmic_error\nkld = KLD = kullback_leibler_divergence\ncosine = cosine_proximity\n\n\ndef serialize(loss):\n    return loss.__name__\n\n\ndef deserialize(name, custom_objects=None):\n    return deserialize_keras_object(name,\n                                    module_objects=globals(),\n                                    custom_objects=custom_objects,\n                                    printable_module_name='loss function')\n\n\ndef get(identifier):\n    if identifier is None:\n        return None\n    if isinstance(identifier, six.string_types):\n        identifier = str(identifier)\n        return deserialize(identifier)\n    elif callable(identifier):\n        return identifier\n    else:\n        raise ValueError('Could not interpret '\n                         'loss function identifier:', identifier)\n", "path": "keras/losses.py"}]}