<s> from tempfile import mkstemp <EOL> import numpy as np <EOL> from numpy . testing import assert_array_equal <EOL> from nose . tools import assert_less <EOL> from sklearn . datasets import load_iris <EOL> from sklearn . cross_validation import train_test_split <EOL> from pystruct . models import GridCRF , GraphCRF <EOL> from pystruct . datasets import generate_blocks_multinomial <EOL> from pystruct . learners import FrankWolfeSSVM <EOL> from pystruct . utils import SaveLogger <EOL> def test_multinomial_blocks_frankwolfe ( ) : <EOL> X , Y = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> , noise = <NUM_LIT:0.5> , seed = <NUM_LIT:0> ) <EOL> crf = GridCRF ( inference_method = '<STR_LIT>' ) <EOL> clf = FrankWolfeSSVM ( model = crf , C = <NUM_LIT:1> , max_iter = <NUM_LIT:50> ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> def test_multinomial_blocks_frankwolfe_batch ( ) : <EOL> X , Y = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> , noise = <NUM_LIT> , seed = <NUM_LIT:0> ) <EOL> crf = GridCRF ( inference_method = '<STR_LIT>' ) <EOL> clf = FrankWolfeSSVM ( model = crf , C = <NUM_LIT:1> , max_iter = <NUM_LIT> , batch_mode = True ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> def test_svm_as_crf_pickling_bcfw ( ) : <EOL> iris = load_iris ( ) <EOL> X , y = iris . data , iris . target <EOL> X_ = [ ( np . atleast_2d ( x ) , np . empty ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , dtype = np . int ) ) for x in X ] <EOL> Y = y . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X_ , Y , random_state = <NUM_LIT:1> ) <EOL> _ , file_name = mkstemp ( ) <EOL> pbl = GraphCRF ( n_features = <NUM_LIT:4> , n_states = <NUM_LIT:3> , inference_method = '<STR_LIT>' ) <EOL> logger = SaveLogger ( file_name ) <EOL> svm = FrankWolfeSSVM ( pbl , C = <NUM_LIT:10> , logger = logger , max_iter = <NUM_LIT:50> ) <EOL> svm . fit ( X_train , y_train ) <EOL> assert_less ( <NUM_LIT> , svm . score ( X_test , y_test ) ) <EOL> assert_less ( <NUM_LIT> , logger . load ( ) . score ( X_test , y_test ) ) <EOL> def test_svm_as_crf_pickling_batch ( ) : <EOL> iris = load_iris ( ) <EOL> X , y = iris . data , iris . target <EOL> X_ = [ ( np . atleast_2d ( x ) , np . empty ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , dtype = np . int ) ) for x in X ] <EOL> Y = y . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> X_train , X_test , y_train , y_test = train_test_split ( X_ , Y , random_state = <NUM_LIT:1> ) <EOL> _ , file_name = mkstemp ( ) <EOL> pbl = GraphCRF ( n_features = <NUM_LIT:4> , n_states = <NUM_LIT:3> , inference_method = '<STR_LIT>' ) <EOL> logger = SaveLogger ( file_name ) <EOL> svm = FrankWolfeSSVM ( pbl , C = <NUM_LIT:10> , logger = logger , max_iter = <NUM_LIT:50> , batch_mode = False ) <EOL> svm . fit ( X_train , y_train ) <EOL> assert_less ( <NUM_LIT> , svm . score ( X_test , y_test ) ) <EOL> assert_less ( <NUM_LIT> , logger . load ( ) . score ( X_test , y_test ) ) </s>
