internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: keras-team/keras

Issue:
save optimizer and objective information to configure file
It seems that keras 1 doesn't support save optimizer and loss to configure file any more. It there some alternative way? Or any plan to add this function back again?



Discussion:

Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -2107,8 +2107,6 @@ def run_internal_graph(self, inputs, masks=None):
         return output_tensors, output_masks, output_shapes
 
     def get_config(self):
-        '''TODO: add keras version information
-        '''
         config = {
             'name': self.name,
         }
@@ -2348,6 +2346,26 @@ def load_weights(self, filepath):
             K.batch_set_value(weight_value_tuples)
         f.close()
 
+    def _updated_config(self):
+        '''shared between different serialization methods'''
+        from keras import __version__ as keras_version
+
+        config = self.get_config()
+        model_config = {
+            'class_name': self.__class__.__name__,
+            'config': config,
+            'keras_version': keras_version
+        }
+
+        if hasattr(self, 'optimizer'):
+            model_config['optimizer'] = self.optimizer.get_config()
+            model_config['loss'] = self.loss.__class__.__name__
+            model_config['sample_weight_mode'] = self.sample_weight_mode
+
+        if hasattr(self, 'loss_weights'):
+            model_config['loss_weights'] = self.loss_weights
+        return model_config
+
     def to_json(self, **kwargs):
         '''Returns a JSON string containing the network configuration.
 
@@ -2367,11 +2385,7 @@ def get_json_type(obj):
 
             raise TypeError('Not JSON Serializable')
 
-        config = self.get_config()
-        model_config = {
-            'class_name': self.__class__.__name__,
-            'config': config,
-        }
+        model_config = self._updated_config()
         return json.dumps(model_config, default=get_json_type, **kwargs)
 
     def to_yaml(self, **kwargs):
@@ -2385,12 +2399,7 @@ def to_yaml(self, **kwargs):
         functions / classes.
         '''
         import yaml
-        config = self.get_config()
-        model_config = {
-            'class_name': self.__class__.__name__,
-            'config': config,
-        }
-        return yaml.dump(model_config, **kwargs)
+        return yaml.dump(self._updated_config(), **kwargs)
 
     def summary(self):
         from keras.utils.layer_utils import print_summary
diff --git a/keras/engine/training.py b/keras/engine/training.py
--- a/keras/engine/training.py
+++ b/keras/engine/training.py
@@ -452,6 +452,7 @@ def compile(self, optimizer, loss, metrics=[], loss_weights=None,
         self.optimizer = optimizers.get(optimizer)
         self.sample_weight_mode = sample_weight_mode
         self.loss = loss
+        self.loss_weights = loss_weights
 
         # prepare loss weights
         if loss_weights is None:


Test patch:
Repository: keras-team/keras

Issue:
KerasClassifier does not return the same thing as scikit learn in the case of binary classification
For binary classification using a stock classifier from scikit, running predict_proba will return a matrix of shape (n_samples,n_classes) however KerasClassifier returns (nsamples,noutputs).

This results in the inability to use the ootb cross_val_score with 'roc_auc' due to the error:

```
site-packages\sklearn\metrics\scorer.pyc in __call__(self, clf, X, y, sample_weight)
    173 
    174                 if y_type == "binary":
--> 175                     y_pred = y_pred[:, 1]
    176                 elif isinstance(y_pred, list):
    177                     y_pred = np.vstack([p[:, -1] for p in y_pred]).T
IndexError: index 1 is out of bounds for axis 1 with size 1
```



Discussion:
Changing the return statement of KerasClassifier's predict_proba method to the following would fix:

```
probs = self.model.predict_proba(X, **kwargs)
if(probs.shape[1] == 1):
    probs = np.hstack([1-probs,probs]) 
return probs
```

Would anyone have a problem with me making this change?  I am a bit worried it may break people's code, but given KerasClassifier is supposed to mimic a sci-kit learn classifier, I think it is an important change to make.

sample code for the problem can be found on the post of the stack overflow user which reported it:  
http://stackoverflow.com/questions/37523882/keras-wrappers-for-scikit-learn-auc-scorer-is-not-working/37569823#37569823

I asked the person to try my solution and he confirmed that it fixed the issue.


Resolving patch:
diff --git a/keras/wrappers/scikit_learn.py b/keras/wrappers/scikit_learn.py
--- a/keras/wrappers/scikit_learn.py
+++ b/keras/wrappers/scikit_learn.py
@@ -2,6 +2,7 @@
 import copy
 import inspect
 import types
+import numpy
 
 from ..utils.np_utils import to_categorical
 from ..models import Sequential
@@ -202,9 +203,17 @@ def predict_proba(self, X, **kwargs):
         # Returns
             proba: array-like, shape `(n_samples, n_outputs)`
                 Class probability estimates.
+                In the case of binary classification (i.e. 1 output of 0 or 1)
+                 will return '(n_samples, 2)'
         '''
         kwargs = self.filter_sk_params(Sequential.predict_proba, kwargs)
-        return self.model.predict_proba(X, **kwargs)
+        probs = self.model.predict_proba(X, **kwargs)
+
+        # check if binary classification
+        if probs.shape[1] == 1:
+            # first column is probability of class 0 and second is of class 1
+            probs = numpy.hstack([1 - probs, probs])
+        return probs
 
     def score(self, X, y, **kwargs):
         '''Returns the mean accuracy on the given test data and labels.


Test patch:
Repository: keras-team/keras

Issue:
K.std results in NaN if input is zero vector
Since `K.std(x)` is just `sqrt(var(x))` under the hood the gradient will be `inf` if the input vector `x` is 0.

This is problematic when using the `BatchNormalization` layer and having a model that sometimes produces zero vectors.

Wouldn't it be better to expose `K.var` in the backends and use that instead of `K.std` so that
the code can do `K.sqrt(K.var(x) + eps)` and does not have to add `eps` to `x`?



Discussion:
That sounds reasonable. Do you want to make a PR?


Resolving patch:
diff --git a/keras/backend/tensorflow_backend.py b/keras/backend/tensorflow_backend.py
--- a/keras/backend/tensorflow_backend.py
+++ b/keras/backend/tensorflow_backend.py
@@ -314,17 +314,23 @@ def prod(x, axis=None, keepdims=False):
     return tf.reduce_prod(x, reduction_indices=axis, keep_dims=keepdims)
 
 
-def std(x, axis=None, keepdims=False):
-    '''Standard deviation of a tensor, alongside the specificied axis.
+def var(x, axis=None, keepdims=False):
+    '''Variance of a tensor, alongside the specified axis.
     '''
     axis = _normalize_axis(axis, ndim(x))
     if x.dtype.base_dtype == tf.bool:
         x = tf.cast(x, _FLOATX)
     m = tf.reduce_mean(x, reduction_indices=axis, keep_dims=True)
     devs_squared = tf.square(x - m)
-    return tf.sqrt(tf.reduce_mean(devs_squared,
-                                  reduction_indices=axis,
-                                  keep_dims=keepdims))
+    return tf.reduce_mean(devs_squared,
+                          reduction_indices=axis,
+                          keep_dims=keepdims)
+
+
+def std(x, axis=None, keepdims=False):
+    '''Standard deviation of a tensor, alongside the specified axis.
+    '''
+    return tf.sqrt(var(x, axis=axis, keepdims=keepdims))
 
 
 def mean(x, axis=None, keepdims=False):
diff --git a/keras/backend/theano_backend.py b/keras/backend/theano_backend.py
--- a/keras/backend/theano_backend.py
+++ b/keras/backend/theano_backend.py
@@ -200,6 +200,10 @@ def std(x, axis=None, keepdims=False):
     return T.std(x, axis=axis, keepdims=keepdims)
 
 
+def var(x, axis=None, keepdims=False):
+    return T.var(x, axis=axis, keepdims=keepdims)
+
+
 def any(x, axis=None, keepdims=False):
     '''Bitwise reduction (logical OR).
     '''
diff --git a/keras/layers/normalization.py b/keras/layers/normalization.py
--- a/keras/layers/normalization.py
+++ b/keras/layers/normalization.py
@@ -139,7 +139,7 @@ def call(self, x, mask=None):
         elif self.mode == 1:
             # sample-wise normalization
             m = K.mean(x, axis=-1, keepdims=True)
-            std = K.std(x, axis=-1, keepdims=True)
+            std = K.sqrt(K.var(x, axis=-1, keepdims=True) + self.epsilon)
             x_normed = (x - m) / (std + self.epsilon)
             out = self.gamma * x_normed + self.beta
         return out


Test patch:
Repository: keras-team/keras

Issue:
Why TF-IDF matrix generated by keras.preprocessing.text.Tokenizer() has negative values?
Say, if run the following script:

> > > import keras
> > > tk = keras.preprocessing.text.Tokenizer()
> > > texts = ['I love you.', 'I love you, too.']
> > > tk.fit_on_texts(texts)
> > > tk.texts_to_matrix(texts, mode='tfidf')

The output will be:
array([[ 0.        , -1.09861229, -1.09861229, -1.09861229,  0.        ],
       [ 0.        , -1.38629436, -1.38629436, -1.38629436, -1.38629436]])

But tf-idf values seems should be non-negative?

By the way, is there a neat way to get the word by its index, or the vocabulary (in the order of word indices) of the Tokenizer() class?  Say, sometimes I want to know what's the most frequent word in the documents, then I want to access word with index 1.

I can do it by running:

> > > vocab = tk.word_index.items()
> > > vocab.sort(key=lambda x:x[1])

This gives:

> > > vocab

[('i', 1), ('you', 2), ('love', 3), ('too', 4)]
But is it somehow hacky?

Thank you!



Discussion:

Resolving patch:
diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py
--- a/keras/preprocessing/text.py
+++ b/keras/preprocessing/text.py
@@ -206,8 +206,10 @@ def sequences_to_matrix(self, sequences, mode='binary'):
                 elif mode == 'binary':
                     X[i][j] = 1
                 elif mode == 'tfidf':
-                    tf = np.log(c / len(seq))
-                    df = (1 + np.log(1 + self.index_docs.get(j, 0) / (1 + self.document_count)))
+                    # Use weighting scheme 2 in
+                    #   https://en.wikipedia.org/wiki/Tf%E2%80%93idf
+                    tf = 1 + np.log(c)
+                    df = np.log(1 + self.index_docs.get(j, 0) / (1 + self.document_count))
                     X[i][j] = tf / df
                 else:
                     raise Exception('Unknown vectorization mode: ' + str(mode))


Test patch:
Repository: keras-team/keras

Issue:
Why TF-IDF matrix generated by keras.preprocessing.text.Tokenizer() has negative values?
Say, if run the following script:

> > > import keras
> > > tk = keras.preprocessing.text.Tokenizer()
> > > texts = ['I love you.', 'I love you, too.']
> > > tk.fit_on_texts(texts)
> > > tk.texts_to_matrix(texts, mode='tfidf')

The output will be:
array([[ 0.        , -1.09861229, -1.09861229, -1.09861229,  0.        ],
       [ 0.        , -1.38629436, -1.38629436, -1.38629436, -1.38629436]])

But tf-idf values seems should be non-negative?

By the way, is there a neat way to get the word by its index, or the vocabulary (in the order of word indices) of the Tokenizer() class?  Say, sometimes I want to know what's the most frequent word in the documents, then I want to access word with index 1.

I can do it by running:

> > > vocab = tk.word_index.items()
> > > vocab.sort(key=lambda x:x[1])

This gives:

> > > vocab

[('i', 1), ('you', 2), ('love', 3), ('too', 4)]
But is it somehow hacky?

Thank you!



Discussion:
Thanks for your quick fixing.
Though, I'm afraid that this bug has **NOT** been fixed.  Because idf is not equivalent to 1/df.

Maybe the following code is more reasonable:

```
idf = np.log(1 +  self.document_count / self.index_docs.get(j, self.document_count))
X[i][j] = tf * idf
```


Resolving patch:
diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py
--- a/keras/preprocessing/text.py
+++ b/keras/preprocessing/text.py
@@ -209,8 +209,8 @@ def sequences_to_matrix(self, sequences, mode='binary'):
                     # Use weighting scheme 2 in
                     #   https://en.wikipedia.org/wiki/Tf%E2%80%93idf
                     tf = 1 + np.log(c)
-                    df = np.log(1 + self.index_docs.get(j, 0) / (1 + self.document_count))
-                    X[i][j] = tf / df
+                    idf = np.log(1 + self.document_count / (1 + self.index_docs.get(j, 0)))
+                    X[i][j] = tf * idf
                 else:
                     raise Exception('Unknown vectorization mode: ' + str(mode))
         return X


Test patch:
Repository: keras-team/keras

Issue:
Why TF-IDF matrix generated by keras.preprocessing.text.Tokenizer() has negative values?
Say, if run the following script:

> > > import keras
> > > tk = keras.preprocessing.text.Tokenizer()
> > > texts = ['I love you.', 'I love you, too.']
> > > tk.fit_on_texts(texts)
> > > tk.texts_to_matrix(texts, mode='tfidf')

The output will be:
array([[ 0.        , -1.09861229, -1.09861229, -1.09861229,  0.        ],
       [ 0.        , -1.38629436, -1.38629436, -1.38629436, -1.38629436]])

But tf-idf values seems should be non-negative?

By the way, is there a neat way to get the word by its index, or the vocabulary (in the order of word indices) of the Tokenizer() class?  Say, sometimes I want to know what's the most frequent word in the documents, then I want to access word with index 1.

I can do it by running:

> > > vocab = tk.word_index.items()
> > > vocab.sort(key=lambda x:x[1])

This gives:

> > > vocab

[('i', 1), ('you', 2), ('love', 3), ('too', 4)]
But is it somehow hacky?

Thank you!



Discussion:
Thanks for your quick fixing.
Though, I'm afraid that this bug has **NOT** been fixed.  Because idf is not equivalent to 1/df.

Maybe the following code is more reasonable:

```
idf = np.log(1 +  self.document_count / self.index_docs.get(j, self.document_count))
X[i][j] = tf * idf
```

I'm sorry.
you're right.

lol~

@soloice please check whether the latest commit has fixed the issue.

Now the logic is correct.

But one more (maybe stupid) question: Does this code works for both Python2 and Python3?
Will `self.document_count / (1 + self.index_docs.get(j, 0))` fall back to integer division (rather than float division) in Python2?

Should there be something like `from __future__ import division`or replace 1 with 1.0?
I'm using Python 2.7 on Ubuntu and have updated Keras to the bleeding edge version, the truncating problem does occur.

(I don't know too much about the compatibility issue.  Feel sorry if this comment is too stupid.)

Thank you!

oh my gosh!
I forgot Python 2 completely since I use Python 3 🙄


Resolving patch:
diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py
--- a/keras/preprocessing/text.py
+++ b/keras/preprocessing/text.py
@@ -3,6 +3,7 @@
 from a fast Cython rewrite.
 '''
 from __future__ import absolute_import
+from __future__ import division
 
 import string
 import sys


Test patch:
Repository: keras-team/keras

Issue:
JSON/YAML loading not working with Lambda merge layers
It doesn't seem possible to save and load models to/from JSON or YAML if there's a Merge layer with a lambda.



Discussion:
Similar https://github.com/fchollet/keras/issues/3001

Have you updated Keras to master? Have you posted a code snippet to reproduce your issue?

Still an issue in #2582 in head version as well. 


Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -1342,9 +1342,9 @@ def get_config(self):
 
         if isinstance(self._output_shape, python_types.LambdaType):
             if py3:
-                output_shape = marshal.dumps(self._output_shape.__code__)
+                output_shape = marshal.dumps(self._output_shape.__code__).decode('raw_unicode_escape')
             else:
-                output_shape = marshal.dumps(self._output_shape.func_code)
+                output_shape = marshal.dumps(self._output_shape.func_code).decode('raw_unicode_escape')
             output_shape_type = 'lambda'
         elif callable(self._output_shape):
             output_shape = self._output_shape.__name__
@@ -1376,7 +1376,7 @@ def from_config(cls, config):
         if output_shape_type == 'function':
             output_shape = globals()[config['output_shape']]
         elif output_shape_type == 'lambda':
-            output_shape = marshal.loads(config['output_shape'])
+            output_shape = marshal.loads(config['output_shape'].encode('raw_unicode_escape'))
             output_shape = python_types.FunctionType(output_shape, globals())
         else:
             output_shape = config['output_shape']


Test patch:
Repository: keras-team/keras

Issue:
A tiny bug in http://keras.io/backend/#batch_dot
In `example` section of http://keras.io/backend/#batch_dot, it reads:

Assume x = [[1, 2] and y = [[5, 6] [3, 4]] [7, 8]] batch_dot(x, y, axes=1) = [[17, 53]] ...

It confused me for a while and later I refer to the comment of source (https://github.com/fchollet/keras/blob/master/keras/backend/theano_backend.py, L130), 
and it should look like:

```
Assume x = [[1, 2]   and y = [[5, 6]
            [3, 4]]          [7, 8]]
```

I think mkdocs just views it as two lines and then combine them into a single line, so matrix should be written in a single line. 
May you fix it and notice other potential bugs like this. Thanks.



Discussion:

Resolving patch:
diff --git a/keras/backend/tensorflow_backend.py b/keras/backend/tensorflow_backend.py
--- a/keras/backend/tensorflow_backend.py
+++ b/keras/backend/tensorflow_backend.py
@@ -224,8 +224,7 @@ def batch_dot(x, y, axes=None):
     make sure that ndim is at least 2.
 
     # Example
-        Assume x = [[1, 2]   and y = [[5, 6]
-                    [3, 4]]           [7, 8]]
+        Assume x = [[1, 2], [3, 4]]   and y = [[5, 6], [7, 8]]
         batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal
         of x.dot(y.T), although we never have to calculate the off-diagonal
         elements.
diff --git a/keras/backend/theano_backend.py b/keras/backend/theano_backend.py
--- a/keras/backend/theano_backend.py
+++ b/keras/backend/theano_backend.py
@@ -128,8 +128,7 @@ def batch_dot(x, y, axes=None):
     make sure that ndim is at least 2.
 
     # Example
-        Assume x = [[1, 2]   and y = [[5, 6]
-                    [3, 4]]           [7, 8]]
+        Assume x = [[1, 2], [3, 4]]   and y = [[5, 6], [7, 8]]
         batch_dot(x, y, axes=1) = [[17, 53]] which is the main diagonal
         of x.dot(y.T), although we never have to calculate the off-diagonal
         elements.


Test patch:
Repository: keras-team/keras

Issue:
Mean is slightly broken with theano v0.9dev
Theano has a real `'bool'` `dtype` from v0.9dev onward. Through `K.mean` does not know about it
and therefor will end up casting it  `float64` insted of `floatX` which is undesired and can lead to a exceptions depending on the setup. This happen e.g. when the `binary_accuracy` is used.

run with **keras + theano v0.9dev + floatX="float32"**
```
import keras.backend as K
import theano.tensor as T
m1 = T.imatrix('foo')
m2 = T.imatrix('bar')
cmped = K.equal(m1, m2)
assert cmped.dtype == "bool"   #<-- this is new had been int8 before
meaned = K.mean(cmped)
assert meaned.dtype == "float64" #<-- ups, this should be "float32" (floatX),
```
This break at last the binary_accuracy but possible other code, too.

I will make a simple pull request in a view minutes
---
Please make sure that the boxes below are checked before you submit your issue. Thank you!

- [X ] Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

- [X/- ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

- [X ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).


Discussion:

Resolving patch:
diff --git a/keras/backend/theano_backend.py b/keras/backend/theano_backend.py
--- a/keras/backend/theano_backend.py
+++ b/keras/backend/theano_backend.py
@@ -279,8 +279,11 @@ def prod(x, axis=None, keepdims=False):
 
 
 def mean(x, axis=None, keepdims=False):
+    '''Mean of a tensor, alongside the specified axis.
+    '''
     dtype = None
-    if 'int' in x.dtype:
+    # bool is available since theano v0.9dev
+    if 'int' in x.dtype or x.dtype == 'bool':
         dtype = _FLOATX
     return T.mean(x, axis=axis, keepdims=keepdims, dtype=dtype)
 


Test patch:
Repository: keras-team/keras

Issue:
Layer regularizers are not shared across models in 1.2.0
If I share a layer with regularizers with another model, the regularizers are not copied correctly. Reusing keras test for regularizers:

```{python}
from keras.models import *
model = Sequential()
model.add(wrappers.TimeDistributed(core.Dense(2, W_regularizer='l1'), input_shape=(3, 4)))
model.add(core.Activation('relu'))
model.compile(optimizer='rmsprop', loss='mse')
print model.losses

x = Input(batch_shape=(None, 3, 4))
x1 = model.layers[0](x)
x2 = model.layers[1](x1)
m2 = Model(input=x, output=x2)
m2.compile(optimizer='rmsprop', loss='mse')

print m2.losses
```

prints:

```
[Elemwise{add,no_inplace}.0]
[]
```


Discussion:

Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -927,7 +927,10 @@ def add_update(self, updates, inputs=None):
     def get_updates_for(self, inputs):
         if not hasattr(self, '_per_input_updates'):
             return []
-        inputs_hash = object_list_uid(inputs)
+        if inputs is not None:
+            inputs_hash = object_list_uid(inputs)
+        else:
+            inputs_hash = None
         if inputs_hash in self._per_input_updates:
             return self._per_input_updates[inputs_hash]
         return []
@@ -935,7 +938,10 @@ def get_updates_for(self, inputs):
     def get_losses_for(self, inputs):
         if not hasattr(self, '_per_input_losses'):
             return []
-        inputs_hash = object_list_uid(inputs)
+        if inputs is not None:
+            inputs_hash = object_list_uid(inputs)
+        else:
+            inputs_hash = None
         if inputs_hash in self._per_input_losses:
             return self._per_input_losses[inputs_hash]
         return []


Test patch:
diff --git a/tests/keras/engine/test_topology.py b/tests/keras/engine/test_topology.py
--- a/tests/keras/engine/test_topology.py
+++ b/tests/keras/engine/test_topology.py
@@ -9,6 +9,27 @@
 from keras.models import model_from_json, model_from_yaml
 from keras.utils.test_utils import keras_test
 
+@keras_test
+def test_get_updates_for():
+    a = Input(shape=(2,))
+    dense_layer = Dense(1)
+    dense_layer.add_update(0, inputs=a)
+    dense_layer.add_update(1, inputs=None)
+
+    assert dense_layer.get_updates_for(a) == [0]
+    assert dense_layer.get_updates_for(None) == [1]
+
+
+@keras_test
+def test_get_losses_for():
+    a = Input(shape=(2,))
+    dense_layer = Dense(1)
+    dense_layer.add_loss(0, inputs=a)
+    dense_layer.add_loss(1, inputs=None)
+    
+    assert dense_layer.get_losses_for(a) == [0]
+    assert dense_layer.get_losses_for(None) == [1]
+
 
 @keras_test
 def test_trainable_weights():