def get(self, name, **kwargs):
        """Retrieves a :py:class:`Parameter` with name ``self.prefix+name``. If not found,
        :py:func:`get` will first try to retrieve it from "shared" dict. If still not
        found, :py:func:`get` will create a new :py:class:`Parameter` with key-word arguments and
        insert it to self.
        Parameters
        ----------
        name : str
            Name of the desired Parameter. It will be prepended with this dictionary's
            prefix.
        **kwargs : dict
            The rest of key-word arguments for the created :py:class:`Parameter`.
        Returns
        -------
        Parameter
            The created or retrieved :py:class:`Parameter`.
        """
        name = self.prefix + name
        param = self._get_impl(name)
        if param is None:  # pylint: disable=too-many-nested-blocks
            param = Parameter(name, **kwargs)
            self._params[name] = param
        else:
            for k, v in kwargs.items():
                if hasattr(param, k) and getattr(param, k) is not None:
                    existing = getattr(param, k)
                    if k == 'shape' and len(v) == len(existing):
                        inferred_shape = []
                        matched = True
                        for dim1, dim2 in zip(v, existing):
                            if dim1 != dim2 and dim1 > 0 and dim2 > 0:
                                matched = False
                                break
                            elif dim1 == dim2:
                                inferred_shape.append(dim1)
                            elif dim1 in (
                                    0, -1
                            ):  # -1 means unknown dim size in np_shape mode
                                inferred_shape.append(dim2)
                            else:
                                inferred_shape.append(dim1)

                        if matched:
                            param._shape = tuple(inferred_shape)
                            continue
                    elif k == 'dtype' and anp.dtype(v) == anp.dtype(existing):
                        continue

                    assert v is None or v == existing, \
                        "Cannot retrieve Parameter '%s' because desired attribute " \
                        "does not match with stored for attribute '%s': " \
                        "desired '%s' vs stored '%s'."%(
                            name, k, str(v), str(getattr(param, k)))
                else:
                    setattr(param, k, v)
        return param
示例#2
0
文件: dssl.py 项目: royadams/dsslpy
 def fit(self,X,T,ranked_pairs,smoothed_pairs,ranked_pair_weights=None,smoothed_pair_weights=None):
     """
         Fit the DSSL loss
         Args:
             X - (n_samples,n_features) ndarray:
                 Design matrix
     
             T - (n_samples,) ndarray of:
                 Vector of continuous timestamps
     
             ranked_pairs - (n_ranked_pairs,2) integer ndarray:
                 Contains ranked pairs of samples. Model will try to find 
                 parameters such that score(ranked_pairs[i,0]) > score(ranked_pairs[i,1])
                 for all i.
         
             smoothed_pairs - (n_smoothed_pairs,2) integer ndarray:
                 Contains pairs of samples that are close in time. Model will 
                 try to find parameters such that minimizes 
                 (score(ranked_pairs[i,0]) - score(ranked_pairs[i,1]))**2/(T(ranked_pairs[i,0]) - T(ranked_pairs[i,1]))**2
                 for all i.
     
             ranked_pair_weights - (n_ranked_pairs,) float ndarray:
                 Contains sample weights for each of the ranked pairs.
 
             smoothed_pair_weights - (n_smoothed_pairs,) float ndarray:
                 Contains sample weights for each of the smoothed pairs.
     """
     assert X.shape[0] > 0
     assert T.shape == (X.shape[0],)
     assert ranked_pairs is None or np.issubdtype(ranked_pairs.dtype, np.dtype(int).type)
     assert smoothed_pairs is None or np.issubdtype(smoothed_pairs.dtype, np.dtype(int).type)
     
     assert ranked_pairs is None or np.all(np.logical_and(ranked_pairs >= 0,ranked_pairs <= X.shape[0]))
     assert smoothed_pairs is None or np.all(np.logical_and(smoothed_pairs >= 0,smoothed_pairs <= X.shape[0]))
     
     assert ranked_pairs is None or np.all(ranked_pairs[:,0] != ranked_pairs[:,1])
     assert smoothed_pairs is None or np.all(smoothed_pairs[:,0] != smoothed_pairs[:,1])
     
     # get obj
     obj = self.get_obj(X,T,ranked_pairs,smoothed_pairs,ranked_pair_weights,smoothed_pair_weights)
     
     # get the gradient function using autograd  
     gfun = grad(obj)
     
     # init params
     w0 = np.zeros(X.shape[1])
     
     # optimize objective
     self.res = minimize(obj,w0,method="L-BFGS-B",jac=gfun,options={"gtol":self.gtol,"maxiter":self.maxiter,"disp":self.disp},tol=self.tol)
     
     self.set_params(self.res.x)
     
     return self
示例#3
0
def test_astype():
    x = np.arange(3, dtype='float32')

    def f(x):
        return np.sum(np.sin(x.astype('float64')))

    assert grad(f)(x).dtype == np.dtype('float32')
示例#4
0
def _read32(bytestream):
    dt = np.dtype(np.uint32).newbyteorder('>')
    return np.frombuffer(bytestream.read(4), dtype=dt)[0]
示例#5
0
def one_hot(labels, num_classes):
    return np.eye(num_classes, dtype=np.dtype("uint8"))[labels]
示例#6
0
    dtype, einsum, empty, empty_like, equal, exp, expand_dims, eye, flip,
    float32, float64, floor, greater, hsplit, hstack, int32, int64, isclose,
    isnan, less, less_equal, linspace, log, logical_and, logical_or, matmul,
    maximum, mean, meshgrid, mod, ones, ones_like, outer, power, repeat,
    reshape, shape, sign, sin, sinh, split, sqrt, squeeze, stack, std, sum,
    tan, tanh, tile, trace, transpose, triu_indices, tril_indices,
    searchsorted, tril, uint8, vstack, where, zeros, zeros_like)
from autograd.scipy.special import polygamma  # NOQA
from scipy.sparse import coo_matrix

from . import linalg  # NOQA
from . import random  # NOQA
from .common import to_ndarray  # NOQA

DTYPES = {
    dtype('int32'): 0,
    dtype('int64'): 1,
    dtype('float32'): 2,
    dtype('float64'): 3
}


def to_numpy(x):
    return x


def convert_to_wider_dtype(tensor_list):
    dtype_list = [DTYPES[x.dtype] for x in tensor_list]
    wider_dtype_index = max(dtype_list)

    wider_dtype = list(DTYPES.keys())[wider_dtype_index]
示例#7
0
def read_int32(f):
    return np.fromstring(f.read(4), dtype=np.dtype('>i4'))[0]
示例#8
0
def read_int32(f):
    return np.fromstring(f.read(4), dtype=np.dtype('>i4'))[0]