Ejemplo n.º 1
0
    def __init__(self, matrix=None, **kwargs):
        """Initialize PrecomputedSGKernel

        Parameters
        ----------
        matrix : SGKernel or Kernel or ndarray
          Kernel matrix to be used
        """
        # Convert to appropriate kernel for input
        if isinstance(matrix, SGKernel):
            k = matrix._k  # Take internal shogun
        elif isinstance(matrix, Kernel):
            k = matrix.as_raw_np()  # Convert to NP otherwise
        else:
            # Otherwise SG would segfault ;-)
            k = np.array(matrix)

        SGKernel.__init__(self, **kwargs)

        if versions['shogun:rev'] >= 4455:
            self._k = sgk.CustomKernel(k)
        else:
            raise RuntimeError, \
                  "Cannot create PrecomputedSGKernel using current version" \
                  " of shogun -- please upgrade"
Ejemplo n.º 2
0
def _run_custom():
    """Run Custom kernel."""

    params = {
        'name': 'Custom',
        'accuracy': 1e-7,
        'feature_class': 'simple',
        'feature_type': 'Real'
    }
    dim_square = 7
    data = dataop.get_rand(dim_square=dim_square)
    feats = featop.get_features(params['feature_class'],
                                params['feature_type'], data)
    data = data['train']
    symdata = data + data.T

    lowertriangle = numpy.array([
        symdata[(x, y)] for x in xrange(symdata.shape[1])
        for y in xrange(symdata.shape[0]) if y <= x
    ])
    kern = kernel.CustomKernel()
    #kern.init(feats['train'], feats['train']
    kern.set_triangle_kernel_matrix_from_triangle(lowertriangle)
    km_triangletriangle = kern.get_kernel_matrix()
    kern.set_triangle_kernel_matrix_from_full(symdata)
    km_fulltriangle = kern.get_kernel_matrix()
    kern.set_full_kernel_matrix_from_full(data)
    km_fullfull = kern.get_kernel_matrix()

    output = {
        'kernel_matrix_triangletriangle': km_triangletriangle,
        'kernel_matrix_fulltriangle': km_fulltriangle,
        'kernel_matrix_fullfull': km_fullfull,
        'kernel_symdata': numpy.matrix(symdata),
        'kernel_data': numpy.matrix(data),
        'kernel_dim_square': dim_square
    }
    output.update(fileop.get_output(category.KERNEL, params))

    fileop.write(category.KERNEL, output)
Ejemplo n.º 3
0
def _as_raw_sg(kernel):
    """Converts directly to a Shogun kernel"""
    return sgk.CustomKernel(kernel.as_raw_np())