Skip to content

Commit 51a7356

Browse files
author
jfelectron
committed
Fix for out of bounds error in RBM with odd # of rows
Fixes out of bounds error in RBM with odd numbered matrix rows due to gen_even_slices generating even slices that overshoot bounds of matrix. /usr/local/lib/python2.7/dist-packages/sklearn/neural_network/rbm.pyc in fit(self, X, y) 310 311 for batch_slice in batch_slices: --> 312 pl_batch = self._fit(X[batch_slice], rng) 313 314 if verbose: /usr/lib/python2.7/dist-packages/scipy/sparse/csr.pyc in __getitem__(self, key) 279 280 elif isintlike(key) or isinstance(key,slice): --> 281 return self[key,:] #[i] or [1:2] 282 else: 283 return self[asindices(key),:] #[[1,2]] /usr/lib/python2.7/dist-packages/scipy/sparse/csr.pyc in __getitem__(self, key) 239 #[1:2,??] 240 if isintlike(col) or isinstance(col, slice): --> 241 return self._get_submatrix(row, col) #[1:2,j] 242 else: 243 P = extractor(col,self.shape[1]).T #[1:2,[1,2]] /usr/lib/python2.7/dist-packages/scipy/sparse/csr.pyc in _get_submatrix(self, row_slice, col_slice) 380 i0, i1 = process_slice( row_slice, M ) 381 j0, j1 = process_slice( col_slice, N ) --> 382 check_bounds( i0, i1, M ) 383 check_bounds( j0, j1, N ) 384 /usr/lib/python2.7/dist-packages/scipy/sparse/csr.pyc in check_bounds(i0, i1, num) 376 raise IndexError( \ 377 "index out of bounds: 0<=%d<%d, 0<=%d<%d, %d<%d" %\ --> 378 (i0, num, i1, num, i0, i1) ) 379 380 i0, i1 = process_slice( row_slice, M ) IndexError: index out of bounds: 0<=113000<113966, 0<=114000<113966, 113000<114000
1 parent d82cf06 commit 51a7356

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

sklearn/utils/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,21 @@ def gen_batches(n, batch_size):
350350
yield slice(start, n)
351351

352352

353-
def gen_even_slices(n, n_packs):
353+
def gen_even_slices(n, n_packs,n_max=None):
354354
"""Generator to create n_packs slices going up to n.
355355
356+
Args:
357+
358+
n : int
359+
correct end_point if dimensions are even_slices
360+
max_n:
361+
int
362+
maximum dimension, correct is dimension are odd
363+
n_pack:
364+
number of packs to slice
365+
366+
367+
356368
Examples
357369
--------
358370
>>> from sklearn.utils import gen_even_slices
@@ -366,12 +378,13 @@ def gen_even_slices(n, n_packs):
366378
[slice(0, 4, None), slice(4, 7, None), slice(7, 10, None)]
367379
"""
368380
start = 0
381+
n_max= n_max if n_max else n
369382
for pack_num in range(n_packs):
370383
this_n = n // n_packs
371384
if pack_num < n % n_packs:
372385
this_n += 1
373386
if this_n > 0:
374-
end = start + this_n
387+
end = start + this_n if end<n_max else n_max
375388
yield slice(start, end, None)
376389
start = end
377390

0 commit comments

Comments
 (0)