<s> import math <EOL> import unittest <EOL> from scipy import stats <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> from neupy . utils import asfloat <EOL> from neupy import layers <EOL> from neupy . algorithms import GradientDescent <EOL> from neupy . layers . connections import NetworkConnectionError <EOL> from neupy . layers import * <EOL> from base import BaseTestCase <EOL> class LayersBasicsTestCase ( BaseTestCase ) : <EOL> def test_without_output_layer ( self ) : <EOL> with self . assertRaises ( NetworkConnectionError ) : <EOL> GradientDescent ( layers . Sigmoid ( <NUM_LIT:10> ) > layers . Sigmoid ( <NUM_LIT:1> ) ) <EOL> def test_list_of_layers ( self ) : <EOL> bpnet = GradientDescent ( [ Sigmoid ( <NUM_LIT:2> ) , Sigmoid ( <NUM_LIT:3> ) , <EOL> Sigmoid ( <NUM_LIT:1> ) , Output ( <NUM_LIT:10> ) ] ) <EOL> self . assertEqual ( <EOL> [ layer . size for layer in bpnet . all_layers ] , <EOL> [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:10> ] <EOL> ) <EOL> def test_layers_iteratinos ( self ) : <EOL> network = GradientDescent ( ( <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> layers = list ( network . all_layers ) <EOL> output_layer = layers . pop ( ) <EOL> self . assertIsNone ( output_layer . relate_to_layer ) <EOL> for layer in layers : <EOL> self . assertIsNotNone ( layer . relate_to_layer ) <EOL> def test_connection_initializations ( self ) : <EOL> possible_connections = ( <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) , <EOL> [ Sigmoid ( <NUM_LIT:2> ) , Tanh ( <NUM_LIT:3> ) , Output ( <NUM_LIT:1> ) ] , <EOL> Relu ( <NUM_LIT:2> ) > Tanh ( <NUM_LIT:10> ) > Output ( <NUM_LIT:1> ) , <EOL> ) <EOL> for connection in possible_connections : <EOL> network = GradientDescent ( connection ) <EOL> self . assertEqual ( len ( network . all_layers ) , <NUM_LIT:3> ) <EOL> @ unittest . skip ( "<STR_LIT>" ) <EOL> def test_recurrent_connections ( self ) : <EOL> inp = Sigmoid ( <NUM_LIT:2> ) <EOL> hd = [ Sigmoid ( <NUM_LIT:2> ) , Sigmoid ( <NUM_LIT:2> ) ] <EOL> out = Output ( <NUM_LIT:1> ) <EOL> GradientDescent ( <EOL> connection = ( <EOL> inp > hd [ <NUM_LIT:0> ] > out , <EOL> hd [ <NUM_LIT:0> ] > hd [ <NUM_LIT:1> ] , <EOL> hd [ <NUM_LIT:1> ] > hd [ <NUM_LIT:0> ] , <EOL> ) <EOL> ) <EOL> def test_activation_layers_without_size ( self ) : <EOL> input_data = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , - <NUM_LIT:1> , <NUM_LIT:10> ] ) <EOL> expected_output = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:10> ] ) <EOL> layer = layers . Relu ( ) <EOL> actual_output = layer . output ( input_data ) <EOL> np . testing . assert_array_equal ( actual_output , expected_output ) <EOL> class HiddenLayersOperationsTestCase ( BaseTestCase ) : <EOL> def test_sigmoid_layer ( self ) : <EOL> layer1 = Sigmoid ( <NUM_LIT:1> ) <EOL> self . assertGreater ( <NUM_LIT:1> , layer1 . activation_function ( <NUM_LIT:1> ) . eval ( ) ) <EOL> def test_hard_sigmoid_layer ( self ) : <EOL> layer1 = HardSigmoid ( <NUM_LIT:6> ) <EOL> test_value = asfloat ( np . array ( [ [ - <NUM_LIT:3> , - <NUM_LIT:2> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ] ) ) <EOL> expected = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> x = T . matrix ( ) <EOL> output = layer1 . activation_function ( x ) . eval ( { x : test_value } ) <EOL> np . testing . assert_array_almost_equal ( output , expected ) <EOL> def test_step_layer ( self ) : <EOL> layer1 = Step ( <NUM_LIT:1> ) <EOL> input_vector = theano . shared ( np . array ( [ - <NUM_LIT:10> , - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:10> ] ) ) <EOL> expected = np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> output = layer1 . activation_function ( input_vector ) . eval ( ) <EOL> np . testing . assert_array_equal ( output , expected ) <EOL> def test_linear_layer ( self ) : <EOL> layer = Linear ( <NUM_LIT:1> ) <EOL> self . assertEqual ( layer . activation_function ( <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> def test_tanh_layer ( self ) : <EOL> layer1 = Tanh ( <NUM_LIT:1> ) <EOL> self . assertGreater ( <NUM_LIT:1> , layer1 . activation_function ( <NUM_LIT:1> ) . eval ( ) ) <EOL> def test_relu_layer ( self ) : <EOL> layer = Relu ( <NUM_LIT:1> ) <EOL> self . assertEqual ( <NUM_LIT:0> , layer . activation_function ( - <NUM_LIT:10> ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , layer . activation_function ( <NUM_LIT:0> ) ) <EOL> self . assertEqual ( <NUM_LIT:10> , layer . activation_function ( <NUM_LIT:10> ) ) <EOL> def test_softplus_layer ( self ) : <EOL> layer = Softplus ( <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( <EOL> math . log ( <NUM_LIT:2> ) , <EOL> layer . activation_function ( <NUM_LIT:0> ) . eval ( ) <EOL> ) <EOL> def test_softmax_layer ( self ) : <EOL> test_input = np . array ( [ [ <NUM_LIT:0.5> , <NUM_LIT:0.5> , <NUM_LIT:0.1> ] ] ) <EOL> softmax_layer = Softmax ( <NUM_LIT:3> ) <EOL> correct_result = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> np . testing . assert_array_almost_equal ( <EOL> correct_result , <EOL> softmax_layer . activation_function ( test_input ) . eval ( ) <EOL> ) <EOL> def test_dropout_layer ( self ) : <EOL> test_input = np . ones ( ( <NUM_LIT:50> , <NUM_LIT:20> ) ) <EOL> dropout_layer = Dropout ( proba = <NUM_LIT:0.5> ) <EOL> layer_output = dropout_layer . output ( test_input ) . eval ( ) <EOL> self . assertGreater ( layer_output . sum ( ) , <NUM_LIT> ) <EOL> self . assertLess ( layer_output . sum ( ) , <NUM_LIT> ) <EOL> self . assertTrue ( np . all ( <EOL> np . bitwise_or ( layer_output == <NUM_LIT:0> , layer_output == <NUM_LIT:2> ) <EOL> ) ) <EOL> def test_reshape_layer ( self ) : <EOL> x = np . random . random ( ( <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> reshape_layer = Reshape ( ) <EOL> y = reshape_layer . output ( x ) . eval ( ) <EOL> self . assertEqual ( y . shape , ( <NUM_LIT:5> , <NUM_LIT:4> * <NUM_LIT:3> * <NUM_LIT:2> * <NUM_LIT:1> ) ) </s>
