# coding: utf-8 from layer_naive import MulLayer, AddLayer apple = 100 apple_num = 2 orange = 150 orange_num = 3 tax = 1.1 # layer mul_apple_layer = MulLayer() mul_orange_layer = MulLayer() add_apple_orange_layer = AddLayer() mul_tax_layer = MulLayer() # forward apple_price = mul_apple_layer.forward(apple, apple_num) # (1) orange_price = mul_orange_layer.forward(orange, orange_num) # (2) all_price = add_apple_orange_layer.forward(apple_price, orange_price) # (3) price = mul_tax_layer.forward(all_price, tax) # (4) # backward dprice = 1 dall_price, dtax = mul_tax_layer.backward(dprice) # (4) dapple_price, dorange_price = add_apple_orange_layer.backward(dall_price) # (3 dorange, dorange_num = mul_orange_layer.backward(dorange_price) # (2) dapple, dapple_num = mul_apple_layer.backward(dapple_price) # (1) print("price:", int(price)) print("dApple:", dapple) print("dApple_num:", int(dapple_num))
from layer_naive import MulLayer from layer_naive import AddLayer apple = 100 apple_num = 2 orange = 150 orange_num = 3 tax = 1.1 # Layer mul_apple_layer = MulLayer() mul_orange_layer = MulLayer() add_fruit_layer = AddLayer() mul_tax_layer = MulLayer() # Forward apple_price = mul_apple_layer.forward(apple, apple_num) orange_price = mul_orange_layer.forward(orange, orange_num) price = add_fruit_layer.forward(apple_price, orange_price) total_price = mul_tax_layer.forward(price, tax) # Backward dtotal_price = 1 dprice, dtax = mul_tax_layer.backward(dtotal_price) dapple_price, dorange_price = add_fruit_layer.backward(dprice) dapple, dapple_num = mul_apple_layer.backward(dapple_price) dorange, dorange_num = mul_orange_layer.backward(dorange_price) print('apple_price: ', apple_price) print('orange_price:', orange_price) print('price:', price)
from layer_naive import MulLayer apple = 100 apple_num = 2 tax = 1.1 # layer mul_apple_layer = MulLayer() mul_tax_layer = MulLayer() # forward apple_price = mul_apple_layer.forward(apple, apple_num) price = mul_tax_layer.forward(apple_price, tax) print("price", price) # 220 # backward dprice = 1 dapple_price, dtax = mul_tax_layer.backward(dprice) dapple, dapple_num = mul_apple_layer.backward(dapple_price) print("dapple", dapple) # 2.2 print("dapple_num", dapple_num) # 110 print("dtax", dtax) # 200
from layer_naive import MulLayer apple = 100 apple_num = 2 tax = 1.1 # Layer mul_apple_layer = MulLayer() mul_tax_layer = MulLayer() # Forward apple_price = mul_apple_layer.forward(apple, apple_num) # 200 total_price = mul_tax_layer.forward(apple_price, tax) # 220 # Backward dprice = 1 dapple_price, dtax = mul_tax_layer.backward(dprice) dapple, dapple_num = mul_apple_layer.backward(dapple_price) # print('total price:', int(total_price)) # print('dapple:', dapple) # print('dapple_num:', int(dapple_num)) # print('dtax:', dtax) print('apple_price: ', apple_price) print('total_price: ', total_price) print('=' * 50) print('dapple_price', dapple_price) print('dtax', dtax) print('dapple', apple) print('dapple_num', dapple_num)