cudaregress v 1.0.0.002

This commit is contained in:
jlevine18 2019-09-26 13:35:37 -05:00 committed by GitHub
parent daab3e8c5b
commit e47b6efe71

View File

@ -6,10 +6,14 @@
# this module is cuda-optimized and vectorized (except for one small part) # this module is cuda-optimized and vectorized (except for one small part)
# setup: # setup:
__version__ = "1.0.0.001" __version__ = "1.0.0.002"
# changelog should be viewed using print(cudaregress.__changelog__) # changelog should be viewed using print(cudaregress.__changelog__)
__changelog__ = """ __changelog__ = """
1.0.0.002:
-Added more parameters to log, exponential, polynomial
-
1.0.0.001: 1.0.0.001:
-initial release, with linear, log, exponential, polynomial, and sigmoid kernels -initial release, with linear, log, exponential, polynomial, and sigmoid kernels
-already vectorized (except for polynomial generation) and CUDA-optimized -already vectorized (except for polynomial generation) and CUDA-optimized
@ -23,12 +27,14 @@ __author__ = (
__all__ = [ __all__ = [
'factorial', 'factorial',
'take_all_pwrs', 'take_all_pwrs',
'num_poly_terms',
'set_device', 'set_device',
'LinearRegKernel', 'LinearRegKernel',
'SigmoidalRegKernel', 'SigmoidalRegKernel',
'LogRegKernel', 'LogRegKernel',
'PolyRegKernel', 'PolyRegKernel',
'ExpRegKernel', 'ExpRegKernel',
'SigmoidalRegKernelArthur',
'SGDTrain', 'SGDTrain',
'CustomTrain' 'CustomTrain'
] ]
@ -38,8 +44,7 @@ __all__ = [
import torch import torch
#set device device = "cuda:0" if torch.torch.cuda.is_available() else "cpu"
device='cuda:0' if torch.cuda.is_available() else 'cpu'
#todo: document completely #todo: document completely
@ -48,6 +53,10 @@ def factorial(n):
return 1 return 1
else: else:
return n*factorial(n-1) return n*factorial(n-1)
def num_poly_terms(num_vars, power):
if power == 0:
return 0
return int(factorial(num_vars+power-1) / factorial(power) / factorial(num_vars-1)) + nt(num_vars, power-1)
def take_all_pwrs(vec,pwr): def take_all_pwrs(vec,pwr):
#todo: vectorize (kinda) #todo: vectorize (kinda)
@ -55,7 +64,7 @@ def take_all_pwrs(vec,pwr):
out=torch.ones(combins.size()[0]) out=torch.ones(combins.size()[0])
for i in torch.t(combins): for i in torch.t(combins):
out *= i out *= i
return out return torch.cat(out,take_all_pwrs(vec, pwr-1))
def set_device(new_device): def set_device(new_device):
global device global device
@ -86,29 +95,57 @@ class SigmoidalRegKernel():
long_bias=self.bias.repeat([1,mtx.size()[1]]) long_bias=self.bias.repeat([1,mtx.size()[1]])
return self.sigmoid(torch.matmul(self.weights,mtx)+long_bias) return self.sigmoid(torch.matmul(self.weights,mtx)+long_bias)
class SigmoidalRegKernelArthur():
parameters= []
weights=None
in_bias=None
scal_mult=None
out_bias=None
sigmoid=torch.nn.Sigmoid()
def __init__(self, num_vars):
self.weights=torch.rand(num_vars, requires_grad=True, device=device)
self.in_bias=torch.rand(1, requires_grad=True, device=device)
self.scal_mult=torch.rand(1, requires_grad=True, device=device)
self.out_bias==torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
def forward(self,mtx):
long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
return (scal_mult*self.sigmoid(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
class LogRegKernel(): class LogRegKernel():
parameters= [] parameters= []
weights=None weights=None
bias=None in_bias=None
scal_mult=None
out_bias=None
def __init__(self, num_vars): def __init__(self, num_vars):
self.weights=torch.rand(num_vars, requires_grad=True, device=device) self.weights=torch.rand(num_vars, requires_grad=True, device=device)
self.bias=torch.rand(1, requires_grad=True, device=device) self.in_bias=torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.bias] self.scal_mult=torch.rand(1, requires_grad=True, device=device)
self.out_bias==torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
def forward(self,mtx): def forward(self,mtx):
long_bias=self.bias.repeat([1,mtx.size()[1]]) long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
return torch.log(torch.matmul(self.weights,mtx)+long_bias) long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
return (scal_mult*torch.log(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
class ExpRegKernel(): class ExpRegKernel():
parameters= [] parameters= []
weights=None weights=None
bias=None in_bias=None
scal_mult=None
out_bias=None
def __init__(self, num_vars): def __init__(self, num_vars):
self.weights=torch.rand(num_vars, requires_grad=True, device=device) self.weights=torch.rand(num_vars, requires_grad=True, device=device)
self.bias=torch.rand(1, requires_grad=True, device=device) self.in_bias=torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.bias] self.scal_mult=torch.rand(1, requires_grad=True, device=device)
self.out_bias==torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.in_bias, self.scal_mult, self.out_bias]
def forward(self,mtx): def forward(self,mtx):
long_bias=self.bias.repeat([1,mtx.size()[1]]) long_in_bias=self.in_bias.repeat([1,mtx.size()[1]])
return torch.exp(torch.matmul(self.weights,mtx)+long_bias) long_out_bias=self.out_bias.repeat([1,mtx.size()[1]])
return (scal_mult*torch.exp(torch.matmul(self.weights,mtx)+long_in_bias))+long_out_bias
class PolyRegKernel(): class PolyRegKernel():
parameters= [] parameters= []
@ -117,7 +154,7 @@ class PolyRegKernel():
power=None power=None
def __init__(self, num_vars, power): def __init__(self, num_vars, power):
self.power=power self.power=power
num_terms=int(factorial(num_vars+power-1) / factorial(power) / factorial(num_vars-1)) num_terms=num_poly_terms(num_vars, power)
self.weights=torch.rand(num_terms, requires_grad=True, device=device) self.weights=torch.rand(num_terms, requires_grad=True, device=device)
self.bias=torch.rand(1, requires_grad=True, device=device) self.bias=torch.rand(1, requires_grad=True, device=device)
self.parameters=[self.weights,self.bias] self.parameters=[self.weights,self.bias]