GPy/GPy/mappings/constant.py

54 lines
1.6 KiB
Python
Raw Normal View History

2015-04-30 17:26:34 +01:00
# Copyright (c) 2015, James Hensman, Alan Saul
import numpy as np
from ..core.mapping import Mapping
from ..core.parameterization import Param
class Constant(Mapping):
"""
A Linear mapping.
.. math::
F(\mathbf{x}) = c
:param input_dim: dimension of input.
:type input_dim: int
:param output_dim: dimension of output.
:type output_dim: int
:param: value the value of this constant mapping
"""
2015-04-30 17:29:51 +01:00
def __init__(self, input_dim, output_dim, value=0., name='constmap'):
2015-04-30 17:26:34 +01:00
Mapping.__init__(self, input_dim=input_dim, output_dim=output_dim, name=name)
value = np.atleast_1d(value)
if not len(value.shape) ==1:
raise ValueError("bad constant values: pass a float or flat vectoor")
elif value.size==1:
value = np.ones(self.output_dim)*value
self.C = Param('C', value)
self.link_parameter(self.C)
def f(self, X):
return np.tile(self.C.values[None,:], (X.shape[0], 1))
def update_gradients(self, dL_dF, X):
2015-04-30 18:00:14 +01:00
self.C.gradient = dL_dF.sum(0)
2015-04-30 17:26:34 +01:00
def gradients_X(self, dL_dF, X):
2015-04-30 18:00:14 +01:00
return np.zeros_like(X)
def to_dict(self):
2018-06-07 09:52:13 +01:00
"""
Convert the object into a json serializable dictionary.
Note: It uses the private method _save_to_input_dict of the parent.
:return dict: json serializable dictionary containing the needed information to instantiate the object
"""
input_dict = super(Constant, self)._save_to_input_dict()
input_dict["class"] = "GPy.mappings.Constant"
input_dict["value"] = self.C.values[0]
return input_dict