Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from util import *
- class MLP():
- def __init__(self, dim_in, dim_hid, dim_out):
- self.dim_in = dim_in
- self.dim_hid = dim_hid
- self.dim_out = dim_out
- self.W_hid = np.random.rand(dim_hid,dim_in) # FIXME
- self.W_out = np.random.rand(dim_out,dim_hid) # FIXME
- def f_hid(self, x):
- pass
- def df_hid(self, x):
- pass
- def f_out(self, x):
- pass
- def df_out(self, x):
- pass
- def forward(self, x):
- a = np.dot(self.W_hid,x) # FIXME
- h = self.f_hid(a) # FIXME
- b = np.dot(self.W_out,h) # FIXME
- y = self.f_out(b) # FIXME
- return y, b, h, a
- def backward(self, x, d):
- y, b, h, a = self.forward(x)
- # print(y.shape[0])
- # print(a.shape[0])
- g_out= np.zeros(y.shape[0]);
- g_hid=np.zeros(a.shape[0])
- for i in range(y.shape[0]):
- g_out[i] = (d[i]-y[i])*self.df_out(b[i]); # FIXME
- # g_hid = (np.dot(np.transpose(self.W_out),g_out))*self.df_hid(a) # FIXME
- for k in range(a.shape[0]):
- halpweight=0
- for i in range(g_out.shape[0]):
- halpweight+=self.W_out[i,k]*g_out[i]
- g_hid[k]=halpweight*self.df_hid(a[k])
- dW_out = np.outer(g_out,h.T) # FIXME
- dW_hid = np.outer(g_hid,x.T) # FIXME
- return y, dW_hid, dW_out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement