Repository: mila-udem/fuel (python)

Issue:
cross_validation doesn't work with SequentialScheme
```chain``` gets depleted after one pass. The scheme returns nothing after first epoch.

```python 
>>> from fuel.schemes import SequentialScheme, cross_validation

>>> cross = cross_validation(SequentialScheme, 10, 3, False, batch_size=5)
>>> train, valid, valid_size = next(cross)

>>> print list(train.get_request_iterator())
[[3, 4, 5, 6, 7], [8, 9]]
>>> print list(train.get_request_iterator())
[]
```

Pull request description:
Turn chain into a list to avoid indices depletion #191
SequentialScheme gets depleted after one epoch as the chain iterator
gets empty.

Fix #191 

Patch:
diff --git a/fuel/schemes.py b/fuel/schemes.py
index ba9ddea..4dcb58c 100644
--- a/fuel/schemes.py
+++ b/fuel/schemes.py
@@ -271,8 +271,8 @@ def cross_validation(scheme_class, num_examples, num_folds, strict=True,
     for i in xrange(num_folds):
         begin = num_examples * i // num_folds
         end = num_examples * (i+1) // num_folds
-        train = scheme_class(chain(xrange(0, begin),
-                                   xrange(end, num_examples)),
+        train = scheme_class(list(chain(xrange(0, begin),
+                                        xrange(end, num_examples))),
                              **kwargs)
         valid = scheme_class(xrange(begin, end), **kwargs)
 


Test patch:
diff --git a/tests/test_schemes.py b/tests/test_schemes.py
index fef0e08..8b8fd65 100644
--- a/tests/test_schemes.py
+++ b/tests/test_schemes.py
@@ -117,17 +117,30 @@ def test_cross_validation():
     (train, valid, valid_size) = next(cross)
     assert list(train.get_request_iterator()) == list(range(3, 10))
     assert list(valid.get_request_iterator()) == list(range(0, 3))
+
+    # test that indices are not depleted
+    assert list(train.get_request_iterator()) == list(range(3, 10))
+    assert list(valid.get_request_iterator()) == list(range(0, 3))
     assert valid_size == 3
 
     (train, valid, valid_size) = next(cross)
     assert (list(train.get_request_iterator()) ==
             list(range(0, 3)) + list(range(6, 10)))
     assert list(valid.get_request_iterator()) == list(range(3, 6))
+
+    # test that indices are not depleted
+    assert (list(train.get_request_iterator()) ==
+            list(range(0, 3)) + list(range(6, 10)))
+    assert list(valid.get_request_iterator()) == list(range(3, 6))
     assert valid_size == 3
 
     (train, valid, valid_size) = next(cross)
     assert list(train.get_request_iterator()) == list(range(0, 6))
     assert list(valid.get_request_iterator()) == list(range(6, 10))
+
+    # test that indices are not depleted
+    assert list(train.get_request_iterator()) == list(range(0, 6))
+    assert list(valid.get_request_iterator()) == list(range(6, 10))
     assert valid_size == 4
 
     assert_raises(StopIteration, next, cross)
@@ -139,8 +152,16 @@ def test_cross_validation():
     assert list(train.get_request_iterator()) == [[4, 5], [6, 7]]
     assert list(valid.get_request_iterator()) == [[0, 1], [2, 3]]
 
+    # test that indices are not depleted
+    assert list(train.get_request_iterator()) == [[4, 5], [6, 7]]
+    assert list(valid.get_request_iterator()) == [[0, 1], [2, 3]]
+
     (train, valid) = next(cross)
     assert list(train.get_request_iterator()) == [[0, 1], [2, 3]]
     assert list(valid.get_request_iterator()) == [[4, 5], [6, 7]]
 
+    # test that indices are not depleted
+    assert list(train.get_request_iterator()) == [[0, 1], [2, 3]]
+    assert list(valid.get_request_iterator()) == [[4, 5], [6, 7]]
+
     assert_raises(StopIteration, next, cross)