Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom kernel cookbook #4366

Merged
merged 2 commits into from Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions doc/cookbook/source/examples/kernel/custom_kernel.rst
@@ -0,0 +1,37 @@
=============
Custom Kernel
=============

The Custom Kernel allows for custom user provided kernel matrices.

-------
Example
-------
In this example, we initialize a :sgclass:`CGaussianKernel` and then use the kernel matrix to create a custom kernel.

Imagine we have files with data.
We create :sgclass:`CDenseFeatures` (here 64 bit floats aka RealFeatures) as

.. sgexample:: custom_kernel_machine:create_features

We create an instance of :sgclass:`CGaussianKernel`.
We initialize with :sgclass:`CDenseFeatures` and then get the kernel matrix.

.. sgexample:: custom_kernel_machine:create_kernel

We use the provided kernel matrix to create custom kernels.

.. sgexample:: custom_kernel_machine:create_custom_kernel

We create an instance of :sgclass:`CLibSVM` with the custom kernel.

.. sgexample:: custom_kernel_machine:create_machine

Then we train the :sgclass:`CLibSVM` and we apply it to the test data, which gives the predicted :sgclass:`CBinaryLabels`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess we dont talk about specialized label classes anymore, just CLabels, but that is totally minor


.. sgexample:: custom_kernel_machine:train_and_apply

Finally, we can evaluate the performance, e.g. using :sgclass:`CAccuracyMeasure`.

.. sgexample:: custom_kernel_machine:evaluate_accuracy

5 changes: 4 additions & 1 deletion examples/meta/src/kernel/custom_kernel_machine.sg
Expand Up @@ -16,9 +16,12 @@ k_obj.init(features_train, features_train)
RealMatrix km_train = k_obj.get_kernel_matrix()
k_obj.init(features_train, features_test)
RealMatrix km_test = k_obj.get_kernel_matrix()
#![create_kernel]

#![create_custom_kernel]
Kernel k_train = kernel(km_train)
Kernel k_test = kernel(km_test)
#![create_kernel]
#![create_custom_kernel]

#![create_machine]
Machine svm = machine("LibSVM", C1=1.0, C2=1.0, kernel=k_train, labels=labels_train, epsilon=0.001)
Expand Down