Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an error when applying regularizers to RNN parameters #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
__pycache__/
*.swp
*.egg-info/
28 changes: 13 additions & 15 deletions proximal_gradient/proximalGradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def l21(parameter, bias=None, reg=0.01, lr=0.1):
ones = torch.ones(w_and_b.size(0), device=torch.device("cpu"))
l21T = 1.0 - torch.min(ones, Norm)
update = (parameter*(l21T.unsqueeze(1)))
parameter.data = update
parameter.data[:] = update
# Update bias
if bias is not None:
update_b = (bias*l21T)
bias.data = update_b
bias.data[:] = update_b

def l21_slow(parameter, reg=0.01, lr=0.1):
"""L21 Regularization (Slow implementation. Used for
Expand Down Expand Up @@ -96,11 +96,11 @@ def linf1(parameter, bias=None, reg=0.01, lr=0.1):
# Actually update parameters and bias
if bias is not None:
update = final_w_and_b[:,:cols-1]
parameter.data = update
parameter.data[:] = update
update_b = final_w_and_b[:,-1]
bias.data = update_b
bias.data[:] = update_b
else:
parameter.data = final_w_and_b
parameter.data[:] = final_w_and_b



Expand Down Expand Up @@ -133,7 +133,7 @@ def l2(parameter, bias=None, reg=0.01, lr=0.1):
ones_w = torch.ones(parameter.size(), device=torch.device("cpu"))
l2T = 1.0 - torch.min(ones_w, Norm)
update = (parameter*l2T)
parameter.data = update
parameter.data[:] = update
# Update bias
if bias is not None:
if Norm.is_cuda:
Expand All @@ -142,7 +142,7 @@ def l2(parameter, bias=None, reg=0.01, lr=0.1):
ones_b = torch.ones(bias.size(), device=torch.device("cpu"))
l2T = 1.0 - torch.min(ones_b, bias)
update_b = (bias*l2T)
bias.data = update_b
bias.data[:] = update_b

def l1(parameter, bias=None, reg=0.01, lr=0.1):
"""L1 Regularization using Proximal Gradients"""
Expand All @@ -156,7 +156,7 @@ def l1(parameter, bias=None, reg=0.01, lr=0.1):
pos = torch.min(Norms_w, Norm*torch.clamp(parameter, min=0))
neg = torch.min(Norms_w, -1.0*Norm*torch.clamp(parameter, max=0))
update_w = parameter - pos + neg
parameter.data = update_w
parameter.data[:] = update_w

if bias is not None:
if bias.is_cuda:
Expand All @@ -166,7 +166,7 @@ def l1(parameter, bias=None, reg=0.01, lr=0.1):
pos = torch.min(Norms_b, Norm*torch.clamp(bias, min=0))
neg = torch.min(Norms_b, -1.0*Norm*torch.clamp(bias, max=0))
update_b = bias - pos + neg
bias.data = update_b
bias.data[:] = update_b

def elasticnet(parameter, bias=None, reg=0.01, lr=0.1, gamma=1.0):
"""Elastic Net Regularization using Proximal Gradients.
Expand All @@ -177,10 +177,10 @@ def elasticnet(parameter, bias=None, reg=0.01, lr=0.1, gamma=1.0):
Norm = reg*lr*gamma
l1(parameter, bias, reg, lr)
update_w = (1.0/(1.0 + Norm))*parameter
parameter.data = update_w
parameter.data[:] = update_w
if bias is not None:
update_b = (1.0/(1.0 + Norm))*bias
bias.data = update_b
bias.data[:] = update_b

def logbarrier(parameter, bias=None, reg=0.01, lr=0.1):
"""Project onto logbarrier. Useful for minimization
Expand All @@ -194,13 +194,11 @@ def logbarrier(parameter, bias=None, reg=0.01, lr=0.1):
squared = squared + 4*Norm
squareroot = torch.sqrt(squared)
update_w = (parameter + squareroot)/2.0
parameter.data = update_w
parameter.data[:] = update_w

if bias is not None:
squared = torch.mul(bias, bias)
squared = squared + 4*Norm
squareroot = torch.sqrt(squared)
update_b = (bias + squareroot)/2.0
bias.data = update_b


bias.data[:] = update_b