Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import numpy as np
  2.  
  3. from util import *
  4.  
  5.  
  6. class MLP():
  7.  
  8. def __init__(self, dim_in, dim_hid, dim_out):
  9. self.dim_in = dim_in
  10. self.dim_hid = dim_hid
  11. self.dim_out = dim_out
  12.  
  13. self.W_hid = np.random.rand(dim_hid,dim_in) # FIXME
  14. self.W_out = np.random.rand(dim_out,dim_hid) # FIXME
  15.  
  16. def f_hid(self, x):
  17. pass
  18.  
  19. def df_hid(self, x):
  20. pass
  21.  
  22. def f_out(self, x):
  23. pass
  24.  
  25. def df_out(self, x):
  26. pass
  27.  
  28.  
  29. def forward(self, x):
  30. a = np.dot(self.W_hid,x) # FIXME
  31. h = self.f_hid(a) # FIXME
  32. b = np.dot(self.W_out,h) # FIXME
  33. y = self.f_out(b) # FIXME
  34.  
  35. return y, b, h, a
  36.  
  37.  
  38. def backward(self, x, d):
  39. y, b, h, a = self.forward(x)
  40. # print(y.shape[0])
  41. # print(a.shape[0])
  42. g_out= np.zeros(y.shape[0]);
  43. g_hid=np.zeros(a.shape[0])
  44. for i in range(y.shape[0]):
  45. g_out[i] = (d[i]-y[i])*self.df_out(b[i]); # FIXME
  46. # g_hid = (np.dot(np.transpose(self.W_out),g_out))*self.df_hid(a) # FIXME
  47. for k in range(a.shape[0]):
  48. halpweight=0
  49. for i in range(g_out.shape[0]):
  50. halpweight+=self.W_out[i,k]*g_out[i]
  51. g_hid[k]=halpweight*self.df_hid(a[k])
  52.  
  53. dW_out = np.outer(g_out,h.T) # FIXME
  54. dW_hid = np.outer(g_hid,x.T) # FIXME
  55.  
  56. return y, dW_hid, dW_out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement