mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-08 11:32:39 +02:00
some work on the linear mapping
This commit is contained in:
parent
6f9c97ee72
commit
dfe325b571
2 changed files with 15 additions and 30 deletions
|
|
@ -10,11 +10,11 @@ class Mapping(Parameterized):
|
||||||
Base model for shared behavior between models that can act like a mapping.
|
Base model for shared behavior between models that can act like a mapping.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, input_dim, output_dim):
|
def __init__(self, input_dim, output_dim, name='mapping'):
|
||||||
self.input_dim = input_dim
|
self.input_dim = input_dim
|
||||||
self.output_dim = output_dim
|
self.output_dim = output_dim
|
||||||
|
|
||||||
super(Mapping, self).__init__()
|
super(Mapping, self).__init__(name=name)
|
||||||
# Model.__init__(self)
|
# Model.__init__(self)
|
||||||
# All leaf nodes should call self._set_params(self._get_params()) at
|
# All leaf nodes should call self._set_params(self._get_params()) at
|
||||||
# the end
|
# the end
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from ..core.mapping import Mapping
|
from ..core.mapping import Mapping
|
||||||
|
from ..core.parameterization import Param
|
||||||
|
|
||||||
class Linear(Mapping):
|
class Linear(Mapping):
|
||||||
"""
|
"""
|
||||||
|
|
@ -19,35 +20,19 @@ class Linear(Mapping):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, input_dim=1, output_dim=1):
|
def __init__(self, input_dim=1, output_dim=1, name='linear_map'):
|
||||||
self.name = 'linear'
|
Mapping.__init__(self, input_dim=input_dim, output_dim=output_dim, name=name)
|
||||||
Mapping.__init__(self, input_dim=input_dim, output_dim=output_dim)
|
self.W = Param('W',np.array((self.input_dim, self.output_dim)))
|
||||||
self.num_params = self.output_dim*(self.input_dim + 1)
|
self.bias = Param('bias',np.array(self.output_dim))
|
||||||
self.W = np.array((self.input_dim, self.output_dim))
|
self.add_parameters(self.W, self.bias)
|
||||||
self.bias = np.array(self.output_dim)
|
|
||||||
self.randomize()
|
|
||||||
|
|
||||||
def _get_param_names(self):
|
|
||||||
return sum([['W_%i_%i' % (n, d) for d in range(self.output_dim)] for n in range(self.input_dim)], []) + ['bias_%i' % d for d in range(self.output_dim)]
|
|
||||||
|
|
||||||
def _get_params(self):
|
|
||||||
return np.hstack((self.W.flatten(), self.bias))
|
|
||||||
|
|
||||||
def _set_params(self, x):
|
|
||||||
self.W = x[:self.input_dim * self.output_dim].reshape(self.input_dim, self.output_dim).copy()
|
|
||||||
self.bias = x[self.input_dim*self.output_dim:].copy()
|
|
||||||
def randomize(self):
|
|
||||||
self.W = np.random.randn(self.input_dim, self.output_dim)/np.sqrt(self.input_dim + 1)
|
|
||||||
self.bias = np.random.randn(self.output_dim)/np.sqrt(self.input_dim + 1)
|
|
||||||
|
|
||||||
def f(self, X):
|
def f(self, X):
|
||||||
return np.dot(X,self.W) + self.bias
|
return np.dot(X,self.W) + self.bias
|
||||||
|
|
||||||
def df_dtheta(self, dL_df, X):
|
def df_dtheta(self, dL_df, X):
|
||||||
self._df_dW = (dL_df[:, :, None]*X[:, None, :]).sum(0).T
|
df_dW = (dL_df[:, :, None]*X[:, None, :]).sum(0).T
|
||||||
self._df_dbias = (dL_df.sum(0))
|
df_dbias = (dL_df.sum(0))
|
||||||
return np.hstack((self._df_dW.flatten(), self._df_dbias))
|
return np.hstack((df_dW.flatten(), df_dbias))
|
||||||
|
|
||||||
def df_dX(self, dL_df, X):
|
def dL_dX(self, dL_df, X):
|
||||||
return (dL_df[:, None, :]*self.W[None, :, :]).sum(2)
|
return (dL_df[:, None, :]*self.W[None, :, :]).sum(2)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue