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

Issue:
snippet from guide throws ValueError when using Convolution3D instead of 2D
For the architecture I want to implement I need layers that work on inputs of different sizes while sharing weights.
The following code snippet from the guide to the functional API (The concept of layer 'node') achieves that for Convolution2D and works fine:
```python
from keras.layers import Input, Convolution2D
a = Input(shape=(32, 32, 3))
b = Input(shape=(64, 64, 3))

conv = Convolution2D(16, 3, 3, border_mode='same')
conved_a = conv(a)

# only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 3)

conved_b = conv(b)
# now the `.input_shape` property wouldn't work, but this does:
assert conv.get_input_shape_at(0) == (None, 32, 32, 3)
assert conv.get_input_shape_at(1) == (None, 64, 64, 3)
```

However, running the equivalent code for Convolution3D results in a ValueError because the input shape expected by the layer seems to be fixed to the first shape it saw.

```python
from keras.layers import Input, Convolution3D
a = Input(shape=(32, 32, 32, 3))
b = Input(shape=(64, 64, 64, 3))

conv = Convolution3D(16, 3, 3, 3, border_mode='same')
conved_a = conv(a)

# only one input so far, the following will work:
assert conv.input_shape == (None, 32, 32, 32, 3)

conved_b = conv(b)
```
```
ValueError: Input 0 is incompatible with layer convolution3d_1: expected shape=(None, 32, 32, 32, 3), found shape=(None, 64, 64, 64, 3)
```
I'm using the tensorflow backend (and dim_ordering) on gpu.

Any ideas what might cause this inconsistency between Convolution2D and 3D?


Discussion:

Resolving patch:
diff --git a/keras/layers/convolutional.py b/keras/layers/convolutional.py
--- a/keras/layers/convolutional.py
+++ b/keras/layers/convolutional.py
@@ -1169,7 +1169,6 @@ def __init__(self, nb_filter, kernel_dim1, kernel_dim2, kernel_dim3,
 
     def build(self, input_shape):
         assert len(input_shape) == 5
-        self.input_spec = [InputSpec(shape=input_shape)]
 
         if self.dim_ordering == 'th':
             stack_size = input_shape[1]
@@ -1229,11 +1228,9 @@ def get_output_shape_for(self, input_shape):
             raise ValueError('Invalid dim_ordering:', self.dim_ordering)
 
     def call(self, x, mask=None):
-        input_shape = self.input_spec[0].shape
         output = K.conv3d(x, self.W, strides=self.subsample,
                           border_mode=self.border_mode,
                           dim_ordering=self.dim_ordering,
-                          volume_shape=input_shape,
                           filter_shape=self.W_shape)
         if self.bias:
             if self.dim_ordering == 'th':


Test patch:
Repository: keras-team/keras

Issue:
max mode can not be used in merge function ? Why ?
merge, Not Merge

> > > merge([forwards, backwards], mode='max')
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in <module>
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1490, in merge
> > >     name=name)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1148, in **init**
> > >     self.add_inbound_node(layers, node_indices, tensor_indices)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in add_inbound_node
> > >     Node.create_node(self, inbound_layers, node_indices, tensor_indices)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 154, in create_node
> > >     output_masks = to_list(outbound_layer.compute_mask(input_tensors, input_masks))
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1372, in compute_mask
> > >     raise Exception('Invalid merge mode: {}'.format(self.mode))
> > > Exception: Invalid merge mode: max



Discussion:
It seems that the "max" mode for merge is not supported if you mask the inputs. The support for "max" mode in Merge (#3128) was added after the support for masking (#2413) and it was apparently forgotten to add "max" to the "compute_mask" method of Merge.


Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -1512,7 +1512,7 @@ def compute_mask(self, inputs, mask=None):
 
         assert hasattr(mask, '__len__') and len(mask) == len(inputs)
 
-        if self.mode in ['sum', 'mul', 'ave']:
+        if self.mode in ['sum', 'mul', 'ave', 'max']:
             masks = [K.expand_dims(m, 0) for m in mask if m is not None]
             return K.all(K.concatenate(masks, axis=0), axis=0, keepdims=False)
         elif self.mode == 'concat':
@@ -1635,7 +1635,7 @@ def merge(inputs, mode='sum', concat_axis=-1,
 
     # Arguments
         mode: String or lambda/function. If string, must be one
-            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot'.
+            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot', 'max'.
             If lambda/function, it should take as input a list of tensors
             and return a single tensor.
         concat_axis: Integer, axis to use in mode `concat`.


Test patch:
Repository: keras-team/keras

Issue:
max mode can not be used in merge function ? Why ?
merge, Not Merge

> > > merge([forwards, backwards], mode='max')
> > > Traceback (most recent call last):
> > >   File "<stdin>", line 1, in <module>
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1490, in merge
> > >     name=name)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1148, in **init**
> > >     self.add_inbound_node(layers, node_indices, tensor_indices)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 543, in add_inbound_node
> > >     Node.create_node(self, inbound_layers, node_indices, tensor_indices)
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 154, in create_node
> > >     output_masks = to_list(outbound_layer.compute_mask(input_tensors, input_masks))
> > >   File "/home/job/analyse/env/lib/python2.7/site-packages/keras/engine/topology.py", line 1372, in compute_mask
> > >     raise Exception('Invalid merge mode: {}'.format(self.mode))
> > > Exception: Invalid merge mode: max



Discussion:
It seems that the "max" mode for merge is not supported if you mask the inputs. The support for "max" mode in Merge (#3128) was added after the support for masking (#2413) and it was apparently forgotten to add "max" to the "compute_mask" method of Merge.


Resolving patch:
diff --git a/keras/legacy/layers.py b/keras/legacy/layers.py
--- a/keras/legacy/layers.py
+++ b/keras/legacy/layers.py
@@ -286,7 +286,7 @@ def compute_mask(self, inputs, mask=None):
 
         assert hasattr(mask, '__len__') and len(mask) == len(inputs)
 
-        if self.mode in ['sum', 'mul', 'ave']:
+        if self.mode in ['sum', 'mul', 'ave', 'max']:
             masks = [K.expand_dims(m, 0) for m in mask if m is not None]
             return K.all(K.concatenate(masks, axis=0), axis=0, keepdims=False)
         elif self.mode == 'concat':
@@ -361,6 +361,7 @@ def get_config(self):
 
     @classmethod
     def from_config(cls, config):
+        config = config.copy()
         mode_type = config.pop('mode_type')
         if mode_type == 'function':
             mode = globals()[config['mode']]
@@ -406,7 +407,7 @@ def merge(inputs, mode='sum', concat_axis=-1,
     ```
     # Arguments
         mode: String or lambda/function. If string, must be one
-            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot'.
+            of: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot', 'max'.
             If lambda/function, it should take as input a list of tensors
             and return a single tensor.
         concat_axis: Integer, axis to use in mode `concat`.


Test patch:
Repository: keras-team/keras

Issue:
Causal padding documentation
In the documentation of the  `Conv1D` layer, it is said about the `padding` causal option,
```
"causal" results in causal (dilated) convolutions, e.g. output[t] depends solely on input[:t-1]
```
But from the code we can that see that output[t] always depend on input[t]. Maybe we can say that
```
"causal" results in causal (dilated) convolutions, e.g. output[t] does not depends on input[t+1:]
```


Discussion:

Resolving patch:
diff --git a/keras/layers/convolutional.py b/keras/layers/convolutional.py
--- a/keras/layers/convolutional.py
+++ b/keras/layers/convolutional.py
@@ -257,7 +257,7 @@ class Conv1D(_Conv):
             any `dilation_rate` value != 1.
         padding: One of `"valid"`, `"causal"` or `"same"` (case-insensitive).
             `"causal"` results in causal (dilated) convolutions, e.g. output[t]
-            depends solely on input[:t-1]. Useful when modeling temporal data
+            does not depend on input[t+1:]. Useful when modeling temporal data
             where the model should not violate the temporal order.
             See [WaveNet: A Generative Model for Raw Audio, section 2.1](https://arxiv.org/abs/1609.03499).
         dilation_rate: an integer or tuple/list of a single integer, specifying


Test patch:
Repository: keras-team/keras

Issue:
CSVLogger doesn't handle properly a logs' value with str type
Suppose we have `logs['foo'] = 'bar'`.
`CSVLogger` will output `"[b, a, r]"` instead of `bar` in `foo` column.

I'm currently working with the fix.


Discussion:

Resolving patch:
diff --git a/keras/callbacks.py b/keras/callbacks.py
--- a/keras/callbacks.py
+++ b/keras/callbacks.py
@@ -879,7 +879,9 @@ def on_epoch_end(self, epoch, logs=None):
 
         def handle_value(k):
             is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
-            if isinstance(k, Iterable) and not is_zero_dim_ndarray:
+            if isinstance(k, six.string_types):
+                return k
+            elif isinstance(k, Iterable) and not is_zero_dim_ndarray:
                 return '"[%s]"' % (', '.join(map(str, k)))
             else:
                 return k


Test patch:
Repository: keras-team/keras

Issue:
skipgram seed parameter got removed in a documentation patch, seed parameter should be readded
Patch 0af6b6c7f5cbad394673bc962dd248f50fd821ff removed the seed parameter from skipgrams. Having a seed parameter makes it easier to vary the results from``skipgram`` in a controlled way.


Discussion:

Resolving patch:
diff --git a/keras/preprocessing/sequence.py b/keras/preprocessing/sequence.py
--- a/keras/preprocessing/sequence.py
+++ b/keras/preprocessing/sequence.py
@@ -114,7 +114,7 @@ def make_sampling_table(size, sampling_factor=1e-5):
 
 def skipgrams(sequence, vocabulary_size,
               window_size=4, negative_samples=1., shuffle=True,
-              categorical=False, sampling_table=None):
+              categorical=False, sampling_table=None, seed=None):
     """Generates skipgram word pairs.
 
     Takes a sequence (list of indexes of words),
@@ -140,6 +140,7 @@ def skipgrams(sequence, vocabulary_size,
             if True labels will be categorical eg. [[1,0],[0,1],[0,1] .. ]
         sampling_table: 1D array of size `vocabulary_size` where the entry i
             encodes the probabibily to sample a word of rank i.
+        seed: random seed.
 
     # Returns
         couples, labels: where `couples` are int pairs and
@@ -184,7 +185,8 @@ def skipgrams(sequence, vocabulary_size,
             labels += [0] * num_negative_samples
 
     if shuffle:
-        seed = random.randint(0, 10e6)
+        if seed is None:
+            seed = random.randint(0, 10e6)
         random.seed(seed)
         random.shuffle(couples)
         random.seed(seed)


Test patch:
Repository: keras-team/keras

Issue:
Tokenizer failed with unicode text
Tokenizer failed with unicode text 

d = [u'ali veli kırk dokuz elli',u'ali veli kırk dokuz elli veli kırk dokuz']
tokenizer.fit_on_texts(d)

TypeError                                 Traceback (most recent call last)
<ipython-input-181-b9cd26967244> in <module>()
----> 1 tokenizer.fit_on_texts(d)

C:\Anaconda2\lib\site-packages\keras-2.0.3-py2.7.egg\keras\preprocessing\text.pyc in fit_on_texts(self, texts)
    117                                                                      self.filters,
    118                                                                      self.lower,
--> 119                                                                      self.split)
    120             for w in seq:
    121                 if w in self.word_counts:

C:\Anaconda2\lib\site-packages\keras-2.0.3-py2.7.egg\keras\preprocessing\text.pyc in text_to_word_sequence(text, filters, lower, split)
     36     if lower:
     37         text = text.lower()
---> 38     text = text.translate(maketrans(filters, split * len(filters)))
     39     seq = text.split(split)
     40     return [i for i in seq if i]

TypeError: character mapping must return integer, None or unicode




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
@@ -38,7 +38,13 @@ def text_to_word_sequence(text,
     """
     if lower:
         text = text.lower()
-    text = text.translate(maketrans(filters, split * len(filters)))
+
+    if sys.version_info < (3,) and isinstance(text, unicode):
+        translate_map = dict((ord(c), unicode(split)) for c in filters)
+    else:
+        translate_map = maketrans(filters, split * len(filters))
+
+    text = text.translate(translate_map)
     seq = text.split(split)
     return [i for i in seq if i]
 


Test patch:
diff --git a/tests/keras/preprocessing/text_test.py b/tests/keras/preprocessing/text_test.py
--- a/tests/keras/preprocessing/text_test.py
+++ b/tests/keras/preprocessing/text_test.py
@@ -1,7 +1,9 @@
+# -*- coding: utf-8 -*-
+
 import numpy as np
 import pytest
 
-from keras.preprocessing.text import Tokenizer, one_hot, hashing_trick
+from keras.preprocessing.text import Tokenizer, one_hot, hashing_trick, text_to_word_sequence
 
 
 def test_one_hot():
@@ -47,5 +49,23 @@ def test_tokenizer():
         matrix = tokenizer.texts_to_matrix(texts, mode)
 
 
+def test_text_to_word_sequence():
+    text = 'hello! ? world!'
+    assert text_to_word_sequence(text) == ['hello', 'world']
+
+
+def test_text_to_word_sequence_unicode():
+    text = u'ali! veli? kırk dokuz elli'
+    assert text_to_word_sequence(text) == [u'ali', u'veli', u'kırk', u'dokuz', u'elli']
+
+
+def test_tokenizer_unicode():
+    texts = [u'ali veli kırk dokuz elli', u'ali veli kırk dokuz elli veli kırk dokuz']
+    tokenizer = Tokenizer(num_words=5)
+    tokenizer.fit_on_texts(texts)
+
+    assert len(tokenizer.word_counts) == 5
+
+
 if __name__ == '__main__':
     pytest.main([__file__])
Repository: keras-team/keras

Issue:
Add transparent Tensor (non-numpy) input to `evaluate`.
Keras TOT (2.0.8):

Works:
`loss = model.evaluate(None, None, steps=steps)`

Suggestion conform current Tensor-input of `def fit(..)`:
`loss = model.evaluate(steps=steps)`

@athundt @ahundt Opinion?
Add transparent Tensor (non-numpy) input to `evaluate`.
Keras TOT (2.0.8):

Works:
`loss = model.evaluate(None, None, steps=steps)`

Suggestion conform current Tensor-input of `def fit(..)`:
`loss = model.evaluate(steps=steps)`

@athundt @ahundt Opinion?


Discussion:
Yes that sounds good to me, it should be easy to add `None` as the default to those two evaluate parameters.
Yes that sounds good to me, it should be easy to add `None` as the default to those two evaluate parameters.

Resolving patch:
diff --git a/keras/engine/training.py b/keras/engine/training.py
--- a/keras/engine/training.py
+++ b/keras/engine/training.py
@@ -1437,11 +1437,13 @@ def fit(self, x=None,
                 If all inputs in the model are named,
                 you can also pass a dictionary
                 mapping input names to Numpy arrays.
+                Can be `None` (default) if feeding from framework-native tensors.
             y: Numpy array of target data,
                 or list of Numpy arrays if the model has multiple outputs.
                 If all outputs in the model are named,
                 you can also pass a dictionary
                 mapping output names to Numpy arrays.
+                Can be `None` (default) if feeding from framework-native tensors.
             batch_size: Integer or `None`.
                 Number of samples per gradient update.
                 If unspecified, it will default to 32.
@@ -1597,7 +1599,7 @@ class indices (integers) to
                               steps_per_epoch=steps_per_epoch,
                               validation_steps=validation_steps)
 
-    def evaluate(self, x, y,
+    def evaluate(self, x=None, y=None,
                  batch_size=None,
                  verbose=1,
                  sample_weight=None,
@@ -1612,11 +1614,13 @@ def evaluate(self, x, y,
                 If all inputs in the model are named,
                 you can also pass a dictionary
                 mapping input names to Numpy arrays.
+                Can be `None` (default) if feeding from framework-native tensors.
             y: Numpy array of target data,
                 or list of Numpy arrays if the model has multiple outputs.
                 If all outputs in the model are named,
                 you can also pass a dictionary
                 mapping output names to Numpy arrays.
+                Can be `None` (default) if feeding from framework-native tensors.
             batch_size: Integer. If unspecified, it will default to 32.
             verbose: Verbosity mode, 0 or 1.
             sample_weight: Array of weights to weight the contribution


Test patch:
Repository: keras-team/keras

Issue:
DirectoryIterator should have a sorted filenames when shuffle=False
I am trying to perform images segmentation.
In one directorty I have a list of files :

- Image1.tif
- Image2.tif
- Image3.tif

And in an other directory the masks :

- Mask1.tif
- Mask2.tif
- Mask3.tif

If I say to flow_from_directory with shuffle=False or Shuffle=True but seeds are the same, I expect the files to be yielded in the same order. They are not.

A simple self.filenames.sort() juste before shuffling should solve the problem.


Discussion:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity  occurs, but feel free to re-open a closed issue if needed.


Resolving patch:
diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py
--- a/keras/preprocessing/image.py
+++ b/keras/preprocessing/image.py
@@ -910,7 +910,7 @@ def _recursive_list(subpath):
     subdir = os.path.basename(directory)
     basedir = os.path.dirname(directory)
     for root, _, files in _recursive_list(directory):
-        for fname in files:
+        for fname in sorted(files):
             is_valid = False
             for extension in white_list_formats:
                 if fname.lower().endswith('.' + extension):


Test patch:
diff --git a/tests/keras/preprocessing/image_test.py b/tests/keras/preprocessing/image_test.py
--- a/tests/keras/preprocessing/image_test.py
+++ b/tests/keras/preprocessing/image_test.py
@@ -175,7 +175,7 @@ def test_directory_iterator(self, tmpdir):
         # check number of classes and images
         assert len(dir_iterator.class_indices) == num_classes
         assert len(dir_iterator.classes) == count
-        assert sorted(dir_iterator.filenames) == sorted(filenames)
+        assert dir_iterator.filenames == sorted(filenames)
 
         # Test invalid use cases
         with pytest.raises(ValueError):
Repository: keras-team/keras

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**



Discussion:

Resolving patch:
diff --git a/keras/losses.py b/keras/losses.py
--- a/keras/losses.py
+++ b/keras/losses.py
@@ -71,7 +71,7 @@ def poisson(y_true, y_pred):
 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.


Test patch:
Repository: keras-team/keras

Issue:
Input layer's dtype default parameter improperly calls K.floatx()
model.layers.Input's constructor has a default parameter `dtype=K.floatx()`. This just calls `K.floatx()` when Keras is first imported, instead of calling `K.floatx()` at construction time. This is problematic because the user might call `K.set_floatx(...)` after importing Keras.

Happy to submit a PR fixing this.


Discussion:
Sounds reasonable, at least for me.

Resolving patch:
diff --git a/keras/engine/topology.py b/keras/engine/topology.py
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -1372,7 +1372,7 @@ def get_config(self):
 
 
 def Input(shape=None, batch_shape=None,
-          name=None, dtype=K.floatx(), sparse=False,
+          name=None, dtype=None, sparse=False,
           tensor=None):
     """`Input()` is used to instantiate a Keras tensor.
 
@@ -1430,6 +1430,8 @@ def Input(shape=None, batch_shape=None,
                        'dimension.')
     if shape and not batch_shape:
         batch_shape = (None,) + tuple(shape)
+    if not dtype:
+        dtype = K.floatx()
     input_layer = InputLayer(batch_input_shape=batch_shape,
                              name=name, dtype=dtype,
                              sparse=sparse,


Test patch: