<s> from __future__ import division , print_function , unicode_literals <EOL> import numpy as np <EOL> from brainstorm . handlers import default_handler <EOL> from brainstorm . optional import has_pycuda <EOL> from brainstorm . value_modifiers import ConstrainL2Norm <EOL> non_default_handlers = [ ] <EOL> if has_pycuda : <EOL> from brainstorm . handlers import PyCudaHandler <EOL> non_default_handlers . append ( PyCudaHandler ( ) ) <EOL> def test_limit_incoming_weights_squared ( ) : <EOL> for orig in ( np . random . rand ( <NUM_LIT:4> , <NUM_LIT:5> ) , np . random . randn ( <NUM_LIT:3> , <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:6> ) ) : <EOL> for limit in [ <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:10> , <NUM_LIT> ] : <EOL> x = orig . reshape ( orig . shape [ <NUM_LIT:0> ] , orig . size / orig . shape [ <NUM_LIT:0> ] ) . copy ( ) <EOL> divisor = ( x * x ) . sum ( axis = <NUM_LIT:1> , keepdims = True ) ** <NUM_LIT:0.5> / limit <EOL> divisor [ divisor < <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> out = ( x / divisor ) . reshape ( orig . shape ) <EOL> y = orig . copy ( ) <EOL> mod = ConstrainL2Norm ( limit ) <EOL> mod ( default_handler , y ) <EOL> assert np . allclose ( y , out ) <EOL> for handler in non_default_handlers : <EOL> y = handler . create_from_numpy ( orig ) <EOL> mod ( handler , y ) <EOL> assert np . allclose ( handler . get_numpy_copy ( y ) , out ) </s>
