Skip to content

Instantly share code, notes, and snippets.

@emilemathieu
Created August 9, 2018 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilemathieu/87938f3e9e9acd00372dc7224a832574 to your computer and use it in GitHub Desktop.
Save emilemathieu/87938f3e9e9acd00372dc7224a832574 to your computer and use it in GitHub Desktop.
class Linear(Module):
""" Applies a linear transformation to the incoming data: y=Ax+b
Parameters
----------
in_features : int
size of each input sample
out_features : int
size of each output sample
Variables
----------
weight : array-like, shape = [out_features x in_features]
the learnable weights of the module
bias : array-like, shape = [out_features]
the learnable bias of the module
"""
def __init__(self, in_features, out_features):
law_bound = 1/np.sqrt(in_features)
self._bias = np.random.uniform(-law_bound,law_bound,size=(out_features)).astype(np.float32)
self._weight = np.random.uniform(-law_bound,law_bound,size=(out_features, in_features)).astype(np.float32)
def forward(self, X):
self._last_input = X
return np.matmul(X, self._weight.T) + np.tile(self._bias, (X.shape[0],1))
def backward(self, output_grad):
self._grad_bias = np.sum(output_grad,axis=0)
self._grad_weight = np.dot(output_grad.T, self._last_input)
return np.dot(output_grad, self._weight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment