def setUp(self): self.apple_layer = MulLayer() self.orange_layer = MulLayer() self.apple_orange_layer = AddLayer() self.tax_layer = MulLayer() self.apple = 100 self.apple_num = 2 self.orange = 150 self.orange_num = 3 self.tax = 1.1
class TestAddLayer(unittest.TestCase): def setUp(self): self.apple_layer = MulLayer() self.orange_layer = MulLayer() self.apple_orange_layer = AddLayer() self.tax_layer = MulLayer() self.apple = 100 self.apple_num = 2 self.orange = 150 self.orange_num = 3 self.tax = 1.1 def test_forward(self): apple_price = self.apple_layer.forward(self.apple, self.apple_num) orange_price = self.orange_layer.forward(self.orange, self.orange_num) apple_orange_price = self.apple_orange_layer.forward( apple_price, orange_price) price = self.tax_layer.forward(apple_orange_price, self.tax) self.assertEqual(715, int(price)) def test_backward(self): apple_price = self.apple_layer.forward(self.apple, self.apple_num) orange_price = self.orange_layer.forward(self.orange, self.orange_num) apple_orange_price = self.apple_orange_layer.forward( apple_price, orange_price) self.tax_layer.forward(apple_orange_price, self.tax) dprice = 1 dall_price, dtax = self.tax_layer.backward(dprice) dapple_price, dorange_price = self.apple_orange_layer.backward( dall_price) dorange, dorange_num = self.orange_layer.backward(dorange_price) dapple, dapple_num = self.apple_layer.backward(dapple_price) self.assertEqual(2.2, dapple) self.assertEqual(110, int(dapple_num)) self.assertEqual(3.3, float('{:.1f}'.format(dorange))) self.assertEqual(165, int(dorange_num)) self.assertEqual(650, dtax)
from mul_layer import MulLayer from add_layer import AddLayer apple = 100 apple_num = 2 orange = 150 orange_num = 3 tax = 1.1 mul_apple_layer = MulLayer() mul_orange_layer = MulLayer() add_apple_orange_layer = AddLayer() mul_tax_layer = MulLayer() apple_price = mul_apple_layer.forward(apple, apple_num) orange_price = mul_orange_layer.forward(orange, orange_num) all_price = add_apple_orange_layer.forward(apple_price, orange_price) price = mul_tax_layer.forward(all_price, tax) print(price) dprice = 1 dall_price, dtax = mul_tax_layer.backward(dprice) dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price) dapple, dapple_num = mul_apple_layer.backward(dapple_price) dorange, dorange_num = mul_orange_layer.backward(dorange_price) print(dapple_num, dapple, dorange, dorange_num, dtax)
from add_layer import AddLayer from mul_layer import MulLayer apple = 100 apple_num = 2 tax = 1.1 orange = 150 orange_num = 3 apple_mul_layer = MulLayer() orange_mul_layer = MulLayer() add_layer = AddLayer() tax_mul_layer = MulLayer() #순전파 apple_price = apple_mul_layer.forward(apple, apple_num) orange_price = orange_mul_layer.forward(orange, orange_num) price = add_layer.forward(apple_price, orange_price) price = tax_mul_layer.forward(price, tax) print(price) #역전파 dprice = 1 dprice, dtax = tax_mul_layer.backward(dprice) dapple_price, dorange_price = add_layer.backward(dprice) dapple_price, dapple_num = apple_mul_layer.backward(dapple_price) dorange_price, dorange_num = orange_mul_layer.backward(dorange_price) print(dapple_price, dapple_num, dorange_price, dorange_num, dtax)