#NEWLINE# def test_total_norm_constraint( ) : #NEWLINE# #INDENT# import numpy as np #NEWLINE# import theano #NEWLINE# import theano.tensor as T #NEWLINE# from lasagne.updates import total_norm_constraint #NEWLINE# x1 = T . scalar ( ) #NEWLINE# x2 = T . matrix ( ) #NEWLINE# threshold = 5.0 #NEWLINE# tensors1 = total_norm_constraint ( [ x1 , x2 ] , threshold , return_norm = False ) #NEWLINE# ( tensors2 , norm ) = total_norm_constraint ( [ x1 , x2 ] , threshold , return_norm = True ) #NEWLINE# f1 = theano . function ( [ x1 , x2 ] , [ tensors1 [ 0 ] , tensors1 [ 1 ] ] ) #NEWLINE# f2 = theano . function ( [ x1 , x2 ] , [ tensors2 [ 0 ] , tensors2 [ 1 ] , norm ] ) #NEWLINE# x_test = np . arange ( ( 1 + 9 ) , dtype = 'float32' ) #NEWLINE# x1_test = x_test [ ( - 1 ) ] #NEWLINE# x2_test = x_test [ : 9 ] . reshape ( ( 3 , 3 ) ) #NEWLINE# ( x1_out1 , x2_out1 ) = f1 ( x1_test , x2_test ) #NEWLINE# ( x1_out2 , x2_out2 , norm ) = f2 ( x1_out2 , x2_test ) #NEWLINE# np . testing . assert_array_almost_equal ( x1_out1 , x1_out2 ) #NEWLINE# np . testing . assert_array_almost_equal ( x2_out1 , x2_out2 ) #NEWLINE# x_out = ( [ float ( x1_out1 ) ] + list ( x2_out1 . flatten ( ) ) ) #NEWLINE# np . testing . assert_array_almost_equal ( np . linalg . norm ( x_test ) , norm ) #NEWLINE# np . testing . assert_array_almost_equal ( np . linalg . norm ( x_out ) , threshold )