<s> import unittest <EOL> import numpy as np <EOL> import six <EOL> from chainer import computational_graph as c <EOL> from chainer import function <EOL> from chainer import testing <EOL> from chainer import variable <EOL> class MockFunction ( function . Function ) : <EOL> def __init__ ( self , n_in , n_out ) : <EOL> self . n_in = n_in <EOL> self . n_out = n_out <EOL> def forward_cpu ( self , xs ) : <EOL> assert len ( xs ) == self . n_in <EOL> return tuple ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) <EOL> for _ in six . moves . range ( self . n_out ) ) <EOL> def backward_cpu ( self , xs , gys ) : <EOL> assert len ( xs ) == self . n_in <EOL> assert len ( gys ) == self . n_out <EOL> return tuple ( np . zeros_like ( xs ) . astype ( np . float32 ) <EOL> for _ in six . moves . range ( self . n_in ) ) <EOL> def mock_function ( xs , n_out ) : <EOL> return MockFunction ( len ( xs ) , n_out ) ( * xs ) <EOL> def _check ( self , outputs , node_num , edge_num ) : <EOL> g = c . build_computational_graph ( outputs ) <EOL> self . assertEqual ( len ( g . nodes ) , node_num ) <EOL> self . assertEqual ( len ( g . edges ) , edge_num ) <EOL> class TestGraphBuilder ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y = mock_function ( ( self . x , ) , <NUM_LIT:1> ) <EOL> self . z = mock_function ( ( self . y , ) , <NUM_LIT:1> ) <EOL> def test_head_variable ( self ) : <EOL> _check ( self , ( self . x , ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def test_intermediate_variable ( self ) : <EOL> _check ( self , ( self . y , ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_tail_variable ( self ) : <EOL> _check ( self , ( self . z , ) , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> def test_multiple_outputs ( self ) : <EOL> _check ( self , ( self . x , self . y ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_multiple_outputs2 ( self ) : <EOL> _check ( self , ( self . x , self . z ) , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> def test_multiple_outputs3 ( self ) : <EOL> _check ( self , ( self . y , self . z ) , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> def test_multiple_outputs4 ( self ) : <EOL> _check ( self , ( self . x , self . y , self . z ) , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> class TestGraphBuilder2 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y1 = mock_function ( ( self . x , ) , <NUM_LIT:1> ) <EOL> self . y2 = mock_function ( ( self . x , ) , <NUM_LIT:1> ) <EOL> def test_head_node ( self ) : <EOL> _check ( self , ( self . x , ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def test_tail_node ( self ) : <EOL> _check ( self , ( self . y1 , ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_tail_node2 ( self ) : <EOL> _check ( self , ( self . y2 , ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_multiple_tails ( self ) : <EOL> _check ( self , ( self . y1 , self . y2 ) , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> class TestGraphBuilder3 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y1 , self . y2 = mock_function ( ( self . x , ) , <NUM_LIT:2> ) <EOL> def test_head_node ( self ) : <EOL> _check ( self , ( self . x , ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def test_tail_node ( self ) : <EOL> _check ( self , ( self . y1 , ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_tail_node2 ( self ) : <EOL> _check ( self , ( self . y2 , ) , <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> def test_multiple_tails ( self ) : <EOL> _check ( self , ( self . y1 , self . y2 ) , <NUM_LIT:4> , <NUM_LIT:3> ) <EOL> class TestGraphBuilder4 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x1 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . x2 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y = mock_function ( ( self . x1 , self . x2 ) , <NUM_LIT:1> ) <EOL> def test_head_node1 ( self ) : <EOL> _check ( self , ( self . x1 , ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def test_head_node2 ( self ) : <EOL> _check ( self , ( self . x2 , ) , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def test_multiple_heads ( self ) : <EOL> _check ( self , ( self . x1 , self . x2 ) , <NUM_LIT:2> , <NUM_LIT:0> ) <EOL> def test_tail_node ( self ) : <EOL> _check ( self , ( self . y , ) , <NUM_LIT:4> , <NUM_LIT:3> ) <EOL> class TestGraphBuilder5 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y = <NUM_LIT:2> * self . x <EOL> self . f = self . y . creator <EOL> self . g = c . build_computational_graph ( ( self . y , ) ) <EOL> def test_edges ( self ) : <EOL> self . assertEqual ( len ( self . g . edges ) , <NUM_LIT:2> ) <EOL> self . assertSetEqual ( set ( self . g . edges ) , <EOL> { ( self . x , self . f ) , ( self . f , self . y ) } ) <EOL> def test_nodes ( self ) : <EOL> self . assertEqual ( len ( self . g . nodes ) , <NUM_LIT:3> ) <EOL> self . assertSetEqual ( set ( self . g . nodes ) , <EOL> { self . x , self . f , self . y } ) <EOL> class TestGraphBuilder6 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x1 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . x2 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y = self . x1 + self . x2 <EOL> self . f = self . y . creator <EOL> self . g = c . build_computational_graph ( ( self . y , ) ) <EOL> def test_edges ( self ) : <EOL> self . assertEqual ( len ( self . g . edges ) , <NUM_LIT:3> ) <EOL> self . assertSetEqual ( set ( self . g . edges ) , <EOL> { ( self . x1 , self . f ) , <EOL> ( self . x2 , self . f ) , <EOL> ( self . f , self . y ) } ) <EOL> def test_nodes ( self ) : <EOL> self . assertEqual ( len ( self . g . nodes ) , <NUM_LIT:4> ) <EOL> self . assertSetEqual ( set ( self . g . nodes ) , <EOL> { self . x1 , self . x2 , self . f , self . y } ) <EOL> class TestGraphBuilder7 ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . x1 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . x2 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . x3 = variable . Variable ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:2> ) ) . astype ( np . float32 ) ) <EOL> self . y = <NUM_LIT> * ( self . x1 + self . x2 ) + self . x3 <EOL> def test_tail_node ( self ) : <EOL> _check ( self , ( self . y , ) , <NUM_LIT:9> , <NUM_LIT:8> ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
