Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import numpy as np
  2.  
  3. import torch
  4. import torch.nn as nn
  5. import torch.optim as optim
  6.  
  7. print("manual calculations...")
  8. print("")
  9.  
  10.  
  11. def sigmoid(z_value):
  12. return 1.0/(1.0+np.exp(-z_value))
  13.  
  14.  
  15. def z(w, a_value, b):
  16. return w * a_value + b
  17.  
  18.  
  19. def a(z_value):
  20. return sigmoid(z_value)
  21.  
  22.  
  23. def sigmoid_prime(z_value):
  24. return sigmoid(z_value)*(1-sigmoid(z_value))
  25.  
  26.  
  27. def dc_db(z_value, dc_da):
  28. return sigmoid_prime(z_value) * dc_da
  29.  
  30.  
  31. def dc_dw(a_prev, dc_db_value):
  32. return a_prev * dc_db_value
  33.  
  34.  
  35. def dc_da_prev(w, dc_db_value):
  36. return w * dc_db_value
  37.  
  38.  
  39. a_l0 = 0.8
  40. w_l1 = 1.58
  41. b_l1 = -0.14
  42. print(f"w_l1 = {round(w_l1, 4)}")
  43. print(f"b_l1 = {round(b_l1, 4)}")
  44.  
  45. z_l1 = z(w_l1, a_l0, b_l1)
  46. a_l1 = sigmoid(z_l1)
  47.  
  48. w_l2 = 2.45
  49. b_l2 = -0.11
  50. print(f"w_l2 = {round(w_l2, 4)}")
  51. print(f"b_l2 = {round(b_l2, 4)}")
  52.  
  53. z_l2 = z(w_l2, a_l1, b_l2)
  54. a_l2 = sigmoid(z_l2)
  55. print(f"a_l2 = {round(a_l2, 4)}")
  56.  
  57. dc_da_l2 = 2 * (a_l2-1)
  58. dc_db_l2 = dc_db(z_l2, dc_da_l2)
  59. dc_dw_l2 = dc_dw(a_l1, dc_db_l2)
  60. dc_da_l1 = dc_da_prev(w_l2, dc_db_l2)
  61.  
  62. step_size = 0.1
  63. updated_b_l2 = b_l2 - dc_db_l2 * step_size
  64. updated_w_l2 = w_l2 - dc_dw_l2 * step_size
  65.  
  66. dc_db_l1 = dc_db(z_l1, dc_da_l1)
  67. dc_dw_l1 = dc_dw(a_l0, dc_db_l1)
  68.  
  69. updated_b_l1 = b_l1 - dc_db_l1 * step_size
  70. updated_w_l1 = w_l1 - dc_dw_l1 * step_size
  71.  
  72. print(f"updated_w_l1 = {round(updated_w_l1, 4)}")
  73. print(f"updated_b_l1 = {round(updated_b_l1, 4)}")
  74.  
  75. print(f"updated_w_l2 = {round(updated_w_l2, 4)}")
  76. print(f"updated_b_l2 = {round(updated_b_l2, 4)}")
  77.  
  78. updated_z_l1 = z(updated_w_l1, a_l0, updated_b_l1)
  79. updated_a_l1 = sigmoid(updated_z_l1)
  80. updated_z_l2 = z(updated_w_l2, updated_a_l1, updated_b_l2)
  81. updated_a_l2 = sigmoid(updated_z_l2)
  82. print(f"updated_a_l2 = {round(updated_a_l2, 4)}")
  83. print("")
  84.  
  85. print("pytorch calculations...")
  86. print("")
  87.  
  88.  
  89. class Net(nn.Module):
  90.  
  91. def __init__(self):
  92. super(Net, self).__init__()
  93. self.hidden_layer = nn.Linear(1, 1)
  94. self.hidden_layer.weight = torch.nn.Parameter(torch.tensor([[1.58]]))
  95. self.hidden_layer.bias = torch.nn.Parameter(torch.tensor([-0.14]))
  96.  
  97. self.output_layer = nn.Linear(1, 1)
  98. self.output_layer.weight = torch.nn.Parameter(torch.tensor([[2.45]]))
  99. self.output_layer.bias = torch.nn.Parameter(torch.tensor([-0.11]))
  100.  
  101. def forward(self, x):
  102. x = torch.sigmoid(self.hidden_layer(x))
  103. x = torch.sigmoid(self.output_layer(x))
  104. return x
  105.  
  106.  
  107. net = Net()
  108. print(net)
  109.  
  110. print(f"w_l1 = {round(net.hidden_layer.weight[0][0].item(), 4)}")
  111. print(f"b_l1 = {round(net.hidden_layer.bias[0].item(), 4)}")
  112. print(f"w_l2 = {round(net.output_layer.weight[0][0].item(), 4)}")
  113. print(f"b_l2 = {round(net.output_layer.bias[0].item(), 4)}")
  114.  
  115. # The outer enclosing array is for mini-batches.
  116. # The second enclosing array is for channels
  117. # The last array contains the input data.
  118. input_data = torch.tensor([[[0.8]]])
  119.  
  120. output = net(input_data)
  121. print(f"a_l2 = {round(output[0].item(), 4)}")
  122.  
  123. target = torch.tensor([[[1.]]])
  124. criterion = nn.MSELoss()
  125. loss = criterion(output, target)
  126. net.zero_grad()
  127. loss.backward()
  128.  
  129. optimizer = optim.SGD(net.parameters(), lr=0.1)
  130. optimizer.step()
  131.  
  132. print(f"updated_w_l1 = {round(net.hidden_layer.weight[0][0].item(), 4)}")
  133. print(f"updated_b_l1 = {round(net.hidden_layer.bias[0].item(), 4)}")
  134. print(f"updated_w_l2 = {round(net.output_layer.weight[0][0].item(), 4)}")
  135. print(f"updated_b_l2 = {round(net.output_layer.bias[0].item(), 4)}")
  136.  
  137. output = net(input_data)
  138. print(f"updated_a_l2 = {round(output[0].item(), 4)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement