<s> import numpy as np <EOL> from numpy . testing import ( assert_array_equal , assert_array_almost_equal , <EOL> assert_equal , assert_almost_equal ) <EOL> from pystruct . datasets import ( generate_big_checker , <EOL> generate_blocks_multinomial , generate_blocks ) <EOL> from pystruct . utils import ( exhaustive_loss_augmented_inference , <EOL> make_grid_edges , find_constraint ) <EOL> from pystruct . models import ( LatentGridCRF , LatentDirectionalGridCRF , <EOL> LatentGraphCRF ) <EOL> from pystruct . models . latent_grid_crf import kmeans_init <EOL> from pystruct . inference import get_installed <EOL> def test_k_means_initialization ( ) : <EOL> n_samples = <NUM_LIT:10> <EOL> X , Y = generate_big_checker ( n_samples = n_samples ) <EOL> edges = [ make_grid_edges ( x , return_lists = True ) for x in X ] <EOL> Y = Y . reshape ( Y . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) <EOL> X = X . reshape ( X . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> , X . shape [ - <NUM_LIT:1> ] ) <EOL> n_labels = len ( np . unique ( Y ) ) <EOL> X = X . reshape ( n_samples , - <NUM_LIT:1> , n_labels ) <EOL> H = kmeans_init ( X , Y , edges , n_states_per_label = [ <NUM_LIT:1> ] * n_labels , <EOL> n_labels = n_labels ) <EOL> H = np . vstack ( H ) <EOL> assert_array_equal ( Y , H ) <EOL> H = kmeans_init ( X , Y , edges , n_states_per_label = [ <NUM_LIT:3> ] * n_labels , <EOL> n_labels = n_labels ) <EOL> H = np . vstack ( H ) <EOL> assert_array_equal ( np . unique ( H ) , np . arange ( <NUM_LIT:6> ) ) <EOL> assert_array_equal ( Y , H // <NUM_LIT:3> ) <EOL> X , Y = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> ) <EOL> edges = [ make_grid_edges ( x , return_lists = True ) for x in X ] <EOL> Y = Y . reshape ( Y . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> ) <EOL> X = X . reshape ( X . shape [ <NUM_LIT:0> ] , - <NUM_LIT:1> , X . shape [ - <NUM_LIT:1> ] ) <EOL> n_labels = len ( np . unique ( Y ) ) <EOL> H = kmeans_init ( X , Y , edges , n_states_per_label = [ <NUM_LIT:1> ] * n_labels , <EOL> n_labels = n_labels ) <EOL> H = np . vstack ( H ) <EOL> assert_array_equal ( Y , H ) <EOL> H = kmeans_init ( X , Y , edges , n_states_per_label = [ <NUM_LIT:2> ] * n_labels , <EOL> n_labels = n_labels ) <EOL> H = np . vstack ( H ) <EOL> assert_array_equal ( np . unique ( H ) , np . arange ( <NUM_LIT:6> ) ) <EOL> assert_array_equal ( Y , H // <NUM_LIT:2> ) <EOL> def test_k_means_initialization_grid_crf ( ) : <EOL> X , Y = generate_big_checker ( n_samples = <NUM_LIT:10> ) <EOL> crf = LatentGridCRF ( n_states_per_label = <NUM_LIT:1> , n_features = <NUM_LIT:2> , n_labels = <NUM_LIT:2> ) <EOL> H = crf . init_latent ( X , Y ) <EOL> assert_array_equal ( Y , H ) <EOL> def test_k_means_initialization_graph_crf ( ) : <EOL> X , Y = generate_big_checker ( n_samples = <NUM_LIT:10> ) <EOL> crf = LatentGraphCRF ( n_states_per_label = <NUM_LIT:1> , n_features = <NUM_LIT:2> , n_labels = <NUM_LIT:2> ) <EOL> X = [ ( x . reshape ( - <NUM_LIT:1> , x . shape [ - <NUM_LIT:1> ] ) , make_grid_edges ( x , return_lists = False ) ) <EOL> for x in X ] <EOL> H = crf . init_latent ( X , Y ) <EOL> assert_array_equal ( Y , H ) <EOL> def test_k_means_initialization_directional_grid_crf ( ) : <EOL> X , Y = generate_big_checker ( n_samples = <NUM_LIT:10> ) <EOL> crf = LatentDirectionalGridCRF ( n_states_per_label = <NUM_LIT:1> , n_features = <NUM_LIT:2> , <EOL> n_labels = <NUM_LIT:2> ) <EOL> H = crf . init_latent ( X , Y ) <EOL> assert_array_equal ( Y , H ) <EOL> def test_blocks_crf_unaries ( ) : <EOL> X , Y = generate_blocks ( n_samples = <NUM_LIT:1> ) <EOL> x , _ = X [ <NUM_LIT:0> ] , Y [ <NUM_LIT:0> ] <EOL> unary_weights = np . repeat ( np . eye ( <NUM_LIT:2> ) , <NUM_LIT:2> , axis = <NUM_LIT:0> ) <EOL> pairwise_weights = np . array ( [ <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> w = np . hstack ( [ unary_weights . ravel ( ) , pairwise_weights ] ) <EOL> crf = LatentGridCRF ( n_states_per_label = <NUM_LIT:2> , n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> ) <EOL> h_hat = crf . inference ( x , w ) <EOL> assert_array_equal ( h_hat // <NUM_LIT:2> , np . argmax ( x , axis = - <NUM_LIT:1> ) ) <EOL> def test_blocks_crf ( ) : <EOL> X , Y = generate_blocks ( n_samples = <NUM_LIT:1> ) <EOL> x , y = X [ <NUM_LIT:0> ] , Y [ <NUM_LIT:0> ] <EOL> pairwise_weights = np . array ( [ <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> unary_weights = np . repeat ( np . eye ( <NUM_LIT:2> ) , <NUM_LIT:2> , axis = <NUM_LIT:0> ) <EOL> w = np . hstack ( [ unary_weights . ravel ( ) , pairwise_weights ] ) <EOL> crf = LatentGridCRF ( n_states_per_label = <NUM_LIT:2> , n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> ) <EOL> h_hat = crf . inference ( x , w ) <EOL> assert_array_equal ( y , h_hat // <NUM_LIT:2> ) <EOL> h = crf . latent ( x , y , w ) <EOL> assert_equal ( crf . loss ( h , h_hat ) , <NUM_LIT:0> ) <EOL> def test_blocks_crf_directional ( ) : <EOL> X , Y = generate_blocks ( n_samples = <NUM_LIT:1> ) <EOL> x , y = X [ <NUM_LIT:0> ] , Y [ <NUM_LIT:0> ] <EOL> pairwise_weights = np . array ( [ <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> unary_weights = np . repeat ( np . eye ( <NUM_LIT:2> ) , <NUM_LIT:2> , axis = <NUM_LIT:0> ) <EOL> w = np . hstack ( [ unary_weights . ravel ( ) , pairwise_weights ] ) <EOL> pw_directional = np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:4> , - <NUM_LIT:4> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:4> , - <NUM_LIT:4> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:4> , - <NUM_LIT:4> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:4> , - <NUM_LIT:4> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , - <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> w_directional = np . hstack ( [ unary_weights . ravel ( ) , pw_directional ] ) <EOL> crf = LatentGridCRF ( n_states_per_label = <NUM_LIT:2> ) <EOL> crf . initialize ( X , Y ) <EOL> directional_crf = LatentDirectionalGridCRF ( n_states_per_label = <NUM_LIT:2> ) <EOL> directional_crf . initialize ( X , Y ) <EOL> h_hat = crf . inference ( x , w ) <EOL> h_hat_d = directional_crf . inference ( x , w_directional ) <EOL> assert_array_equal ( h_hat , h_hat_d ) <EOL> h = crf . latent ( x , y , w ) <EOL> h_d = directional_crf . latent ( x , y , w_directional ) <EOL> assert_array_equal ( h , h_d ) <EOL> h_hat = crf . loss_augmented_inference ( x , y , w ) <EOL> h_hat_d = directional_crf . loss_augmented_inference ( x , y , w_directional ) <EOL> assert_array_equal ( h_hat , h_hat_d ) <EOL> joint_feature = crf . joint_feature ( x , h_hat ) <EOL> joint_feature_d = directional_crf . joint_feature ( x , h_hat ) <EOL> assert_array_equal ( np . dot ( joint_feature , w ) , np . dot ( joint_feature_d , w_directional ) ) <EOL> def test_latent_consistency_zero_pw_graph ( ) : <EOL> crf = LatentGraphCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . zeros ( <NUM_LIT> ) <EOL> w [ : <NUM_LIT:8> ] = np . random . normal ( size = <NUM_LIT:8> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:5> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:5> , <NUM_LIT:2> ) ) <EOL> h = crf . latent ( ( x , np . zeros ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , dtype = np . int ) ) , y , w ) <EOL> assert_array_equal ( h // <NUM_LIT:2> , y ) <EOL> def test_latent_consistency_graph ( ) : <EOL> crf = LatentGraphCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . random . normal ( size = <NUM_LIT> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:4> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:4> , <NUM_LIT:2> ) ) <EOL> e = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] , dtype = np . int ) <EOL> h = crf . latent ( ( x , e ) , y , w ) <EOL> assert_array_equal ( h // <NUM_LIT:2> , y ) <EOL> def test_loss_augmented_inference_energy_graph ( ) : <EOL> crf = LatentGraphCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . random . normal ( size = <NUM_LIT> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:3> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> e = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:2> , <NUM_LIT:0> ] ] , dtype = np . int ) <EOL> h_hat , energy = crf . loss_augmented_inference ( ( x , e ) , y * <NUM_LIT:2> , w , <EOL> relaxed = True , <EOL> return_energy = True ) <EOL> assert_almost_equal ( - energy , np . dot ( w , crf . joint_feature ( ( x , e ) , h_hat ) ) <EOL> + crf . loss ( y * <NUM_LIT:2> , h_hat ) ) <EOL> def test_latent_consistency_zero_pw_grid ( ) : <EOL> crf = LatentGridCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . zeros ( <NUM_LIT> ) <EOL> w [ : <NUM_LIT:8> ] = np . random . normal ( size = <NUM_LIT:8> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:5> , <NUM_LIT:5> , <NUM_LIT:2> ) ) <EOL> h = crf . latent ( x , y , w ) <EOL> assert_array_equal ( h // <NUM_LIT:2> , y ) <EOL> def test_latent_consistency_grid ( ) : <EOL> crf = LatentGridCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . random . normal ( size = <NUM_LIT> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:2> ) ) <EOL> h = crf . latent ( x , y , w ) <EOL> assert_array_equal ( h // <NUM_LIT:2> , y ) <EOL> def test_loss_augmented_inference_exhaustive_grid ( ) : <EOL> crf = LatentGridCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:2> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> w = np . random . normal ( size = <NUM_LIT> ) <EOL> y = np . random . randint ( <NUM_LIT:2> , size = ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> x = np . random . normal ( size = ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> h_hat = crf . loss_augmented_inference ( x , y * <NUM_LIT:2> , w ) <EOL> h = exhaustive_loss_augmented_inference ( crf , x , y * <NUM_LIT:2> , w ) <EOL> assert_array_equal ( h , h_hat ) <EOL> def test_continuous_y ( ) : <EOL> for inference_method in get_installed ( [ "<STR_LIT>" , "<STR_LIT>" ] ) : <EOL> X , Y = generate_blocks ( n_samples = <NUM_LIT:1> ) <EOL> x , y = X [ <NUM_LIT:0> ] , Y [ <NUM_LIT:0> ] <EOL> w = np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <EOL> <NUM_LIT:0> , <EOL> - <NUM_LIT:4> , <NUM_LIT:0> ] ) <EOL> crf = LatentGridCRF ( n_labels = <NUM_LIT:2> , n_features = <NUM_LIT:2> , n_states_per_label = <NUM_LIT:1> , <EOL> inference_method = inference_method ) <EOL> joint_feature = crf . joint_feature ( x , y ) <EOL> y_cont = np . zeros_like ( x ) <EOL> gx , gy = np . indices ( x . shape [ : - <NUM_LIT:1> ] ) <EOL> y_cont [ gx , gy , y ] = <NUM_LIT:1> <EOL> vert = np . dot ( y_cont [ <NUM_LIT:1> : , : , : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) . T , <EOL> y_cont [ : - <NUM_LIT:1> , : , : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> horz = np . dot ( y_cont [ : , <NUM_LIT:1> : , : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) . T , <EOL> y_cont [ : , : - <NUM_LIT:1> , : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> pw = vert + horz <EOL> joint_feature_cont = crf . joint_feature ( x , ( y_cont , pw ) ) <EOL> assert_array_almost_equal ( joint_feature , joint_feature_cont , <NUM_LIT:4> ) <EOL> const = find_constraint ( crf , x , y , w , relaxed = False ) <EOL> const_cont = find_constraint ( crf , x , y , w , relaxed = True ) <EOL> assert_array_almost_equal ( const [ <NUM_LIT:1> ] , const_cont [ <NUM_LIT:1> ] , <NUM_LIT:4> ) <EOL> assert_almost_equal ( const [ <NUM_LIT:2> ] , const_cont [ <NUM_LIT:2> ] , <NUM_LIT:4> ) <EOL> if isinstance ( const_cont [ <NUM_LIT:0> ] , tuple ) : <EOL> assert_array_equal ( const [ <NUM_LIT:0> ] , np . argmax ( const_cont [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , axis = - <NUM_LIT:1> ) ) <EOL> assert_almost_equal ( crf . loss ( y , const [ <NUM_LIT:0> ] ) , <EOL> crf . continuous_loss ( y , const_cont [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) , <NUM_LIT:4> ) </s>
