<s> import unittest <EOL> import numpy <EOL> import chainer <EOL> from chainer import cuda <EOL> from chainer import function_hooks <EOL> from chainer import functions <EOL> from chainer . functions . connection import linear <EOL> from chainer import links <EOL> from chainer import testing <EOL> from chainer . testing import attr <EOL> def check_history ( self , t , function_type , return_type ) : <EOL> self . assertIsInstance ( t [ <NUM_LIT:0> ] , function_type ) <EOL> self . assertIsInstance ( t [ <NUM_LIT:1> ] , return_type ) <EOL> class TestTimerHookToLink ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . h = function_hooks . TimerHook ( ) <EOL> self . l = links . Linear ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> self . x = numpy . random . uniform ( - <NUM_LIT:0.1> , <NUM_LIT:0.1> , ( <NUM_LIT:3> , <NUM_LIT:5> ) ) . astype ( numpy . float32 ) <EOL> self . gy = numpy . random . uniform ( - <NUM_LIT:0.1> , <NUM_LIT:0.1> , ( <NUM_LIT:3> , <NUM_LIT:5> ) ) . astype ( numpy . float32 ) <EOL> def test_name ( self ) : <EOL> self . assertEqual ( self . h . name , '<STR_LIT>' ) <EOL> def check_forward ( self , x ) : <EOL> with self . h : <EOL> self . l ( chainer . Variable ( x ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . h . call_history ) ) <EOL> check_history ( self , self . h . call_history [ <NUM_LIT:0> ] , <EOL> linear . LinearFunction , float ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x ) <EOL> @ attr . gpu <EOL> def test_forward_gpu ( self ) : <EOL> self . l . to_gpu ( ) <EOL> self . check_forward ( cuda . to_gpu ( self . x ) ) <EOL> def check_backward ( self , x , gy ) : <EOL> x = chainer . Variable ( x ) <EOL> y = self . l ( x ) <EOL> y . grad = gy <EOL> with self . h : <EOL> y . backward ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . h . call_history ) ) <EOL> check_history ( self , self . h . call_history [ <NUM_LIT:0> ] , <EOL> linear . LinearFunction , float ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x , self . gy ) <EOL> @ attr . gpu <EOL> def test_backward_gpu ( self ) : <EOL> self . l . to_gpu ( ) <EOL> self . check_backward ( cuda . to_gpu ( self . x ) , cuda . to_gpu ( self . gy ) ) <EOL> class TestTimerHookToFunction ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . h = function_hooks . TimerHook ( ) <EOL> self . f = functions . Exp ( ) <EOL> self . f . add_hook ( self . h ) <EOL> self . x = numpy . random . uniform ( - <NUM_LIT:0.1> , <NUM_LIT:0.1> , ( <NUM_LIT:3> , <NUM_LIT:5> ) ) . astype ( numpy . float32 ) <EOL> self . gy = numpy . random . uniform ( - <NUM_LIT:0.1> , <NUM_LIT:0.1> , ( <NUM_LIT:3> , <NUM_LIT:5> ) ) . astype ( numpy . float32 ) <EOL> def check_forward ( self , x ) : <EOL> self . f ( chainer . Variable ( x ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( self . h . call_history ) ) <EOL> check_history ( self , self . h . call_history [ <NUM_LIT:0> ] , functions . Exp , float ) <EOL> def test_forward_cpu ( self ) : <EOL> self . check_forward ( self . x ) <EOL> @ attr . gpu <EOL> def test_fowward_gpu ( self ) : <EOL> self . check_forward ( cuda . to_gpu ( self . x ) ) <EOL> def check_backward ( self , x , gy ) : <EOL> x = chainer . Variable ( x ) <EOL> y = self . f ( x ) <EOL> y . grad = gy <EOL> y . backward ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . h . call_history ) ) <EOL> check_history ( self , self . h . call_history [ <NUM_LIT:1> ] , functions . Exp , float ) <EOL> def test_backward_cpu ( self ) : <EOL> self . check_backward ( self . x , self . gy ) <EOL> @ attr . gpu <EOL> def test_backward_gpu ( self ) : <EOL> self . check_backward ( cuda . to_gpu ( self . x ) , cuda . to_gpu ( self . gy ) ) <EOL> testing . run_module ( __name__ , __file__ ) </s>
