<s> import numpy as np <EOL> import pytest <EOL> import theanets <EOL> import util as u <EOL> REG_LAYERS = [ <EOL> u . NUM_INPUTS , <EOL> dict ( size = u . NUM_HID1 , form = '<STR_LIT>' , filter_size = u . CNN . FILTER_SIZE ) , <EOL> dict ( size = u . NUM_HID2 , form = '<STR_LIT>' , filter_size = u . CNN . FILTER_SIZE ) , <EOL> ( '<STR_LIT>' , u . NUM_HID2 * <EOL> ( u . CNN . NUM_WIDTH - <NUM_LIT:2> * u . CNN . FILTER_WIDTH + <NUM_LIT:2> ) * <EOL> ( u . CNN . NUM_HEIGHT - <NUM_LIT:2> * u . CNN . FILTER_HEIGHT + <NUM_LIT:2> ) ) , <EOL> u . NUM_OUTPUTS ] <EOL> CLF_LAYERS = [ <EOL> u . NUM_INPUTS , <EOL> dict ( size = u . NUM_HID1 , form = '<STR_LIT>' , filter_size = u . CNN . FILTER_SIZE ) , <EOL> dict ( size = u . NUM_HID2 , form = '<STR_LIT>' , filter_size = u . CNN . FILTER_SIZE ) , <EOL> ( '<STR_LIT>' , u . NUM_HID2 * <EOL> ( u . CNN . NUM_WIDTH - <NUM_LIT:2> * u . CNN . FILTER_WIDTH + <NUM_LIT:2> ) * <EOL> ( u . CNN . NUM_HEIGHT - <NUM_LIT:2> * u . CNN . FILTER_HEIGHT + <NUM_LIT:2> ) ) , <EOL> u . NUM_CLASSES ] <EOL> def assert_shape ( actual , width , height , channels ) : <EOL> assert actual == ( u . NUM_EXAMPLES , width , height , channels ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ <EOL> ( theanets . convolution . Regressor , REG_LAYERS , False , u . CNN . REG_DATA ) , <EOL> ( theanets . convolution . Classifier , CLF_LAYERS , False , u . CNN . CLF_DATA ) , <EOL> ( theanets . convolution . Regressor , REG_LAYERS , True , u . CNN . WREG_DATA ) , <EOL> ( theanets . convolution . Classifier , CLF_LAYERS , True , u . CNN . WCLF_DATA ) , <EOL> ] ) <EOL> def test_sgd ( Model , layers , weighted , data ) : <EOL> u . assert_progress ( Model ( layers , weighted = weighted ) , data ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ <EOL> ( theanets . convolution . Regressor , REG_LAYERS , u . NUM_OUTPUTS ) , <EOL> ( theanets . convolution . Classifier , CLF_LAYERS , ( u . NUM_EXAMPLES , ) ) , <EOL> ] ) <EOL> def test_predict ( Model , layers , output ) : <EOL> u . assert_shape ( Model ( layers ) . predict ( u . CNN . INPUTS ) . shape , output ) <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ <EOL> ( theanets . convolution . Regressor , REG_LAYERS , u . OUTPUTS , - <NUM_LIT> ) , <EOL> ( theanets . convolution . Classifier , CLF_LAYERS , u . CLASSES , <NUM_LIT> ) , <EOL> ] ) <EOL> def test_score ( Model , layers , target , score ) : <EOL> assert Model ( layers ) . score ( u . CNN . INPUTS , target ) == score <EOL> @ pytest . mark . parametrize ( '<STR_LIT>' , [ <EOL> ( theanets . convolution . Regressor , REG_LAYERS , u . NUM_OUTPUTS ) , <EOL> ( theanets . convolution . Classifier , CLF_LAYERS , u . NUM_CLASSES ) , <EOL> ] ) <EOL> def test_predict ( Model , layers , target ) : <EOL> outs = Model ( layers ) . feed_forward ( u . CNN . INPUTS ) <EOL> assert len ( list ( outs ) ) == <NUM_LIT:8> <EOL> W , H = u . CNN . NUM_WIDTH , u . CNN . NUM_HEIGHT <EOL> w , h = u . CNN . FILTER_WIDTH , u . CNN . FILTER_HEIGHT <EOL> assert_shape ( outs [ '<STR_LIT>' ] . shape , W , H , u . NUM_INPUTS ) <EOL> assert_shape ( outs [ '<STR_LIT>' ] . shape , W - w + <NUM_LIT:1> , H - h + <NUM_LIT:1> , u . NUM_HID1 ) <EOL> assert_shape ( outs [ '<STR_LIT>' ] . shape , W - <NUM_LIT:2> * w + <NUM_LIT:2> , H - <NUM_LIT:2> * h + <NUM_LIT:2> , u . NUM_HID2 ) <EOL> u . assert_shape ( outs [ '<STR_LIT>' ] . shape , target ) <EOL> class TestClassifier : <EOL> @ pytest . fixture <EOL> def net ( self ) : <EOL> return theanets . convolution . Classifier ( CLF_LAYERS ) <EOL> def test_predict_proba ( self , net ) : <EOL> u . assert_shape ( net . predict_proba ( u . CNN . INPUTS ) . shape , u . NUM_CLASSES ) <EOL> def test_predict_logit ( self , net ) : <EOL> u . assert_shape ( net . predict_logit ( u . CNN . INPUTS ) . shape , u . NUM_CLASSES ) <EOL> def test_score ( self , net ) : <EOL> w = <NUM_LIT:0.5> * np . ones ( u . CLASSES . shape , '<STR_LIT:f>' ) <EOL> assert <NUM_LIT:0> <= net . score ( u . CNN . INPUTS , u . CLASSES , w ) <= <NUM_LIT:1> </s>
