项目作者: sirCamp

项目描述 :
Tensorflow implementation of most popular Kernel for kernel methods like SVM
高级语言: Python
项目地址: git://github.com/sirCamp/tensorflow-kernels.git
创建时间: 2019-05-31T13:13:30Z
项目社区:https://github.com/sirCamp/tensorflow-kernels

开源协议:MIT License

下载


Tensorflow-kernels

Logo

A package with Tensorflow (both CPU and GPU) implementation of most popular Kernels for kernels methods (SVM, MKL…).

Those kernels works with tensor as inputs.

The main idea of this project is to exploit the powerfull of GPUs and modern CPUs on matrix and kernels elaborations.
Actually the implemented kernels are:

  • Linear
  • RBF
  • Polynomial
  • CosineSimilarity
  • Fourier
  • Spline

An Experimental Kernel have been added:

  • PSpectrum

Attention: Due to the GPUs usage the precision of decimal numbers may be different, and hence, the results may be slightly differs as well

Attention 2: Due to exploit the power of GPUs it’s strongly recommended to work with float32 or even in half precision float16.

Installation:

from pip:

  1. pip install tensorflow-kernels

from source:

  1. pip install https://github.com/sirCamp/tensorflow-kernels/archive/master.zip

Examples:

A simple example with PolynomialKernel

  1. import numpy as np
  2. import tensorflow as tf
  3. from kernels.polynomial_kernel import PolynomialKernel
  4. from kernels import array_to_tensor, tensor_to_array
  5. n = 2000
  6. p = 1000
  7. a = np.random.random((n, p)).astype(np.float32)
  8. b = np.random.random((n, p)).astype(np.float32)
  9. x = array_to_tensor(a, dtype=tf.float32)
  10. y = array_to_tensor(b, dtype=tf.float32)
  11. poly = PolynomialKernel(scale=1, bias=0, degree=4)
  12. kernel = poly.compute(x, y)
  13. print(tensor_to_array(kernel, dtype=np.float32))

A simple example with PSpectrumKernel.

Attention: PSpectrum is still experimental and it exploits eager computation in order to work properly.
Furthermore it maybe won’t works with Tensorflow 2.0 since some packages have been removed.

Attention 2: Due to the usage of the type tf.string computation will be shared between GPUs and CPUs.

Attention 3: This kernel return tensor with type tf.int64.

  1. import numpy as np
  2. import tensorflow as tf
  3. from kernels.experimental.p_spectrum_kernel import PSpectrumKernel
  4. from kernels import array_to_tensor, tensor_to_array
  5. a = np.array(['aaaaaaaa','bbbbbbb','ccccc','aaaaaaa','cccccc','bbbbbb'])
  6. b = np.array(['aaaaaaaa','bbbbbbb','aaaaaaa','cccccc'])
  7. x = array_to_tensor(a, dtype=tf.string)
  8. y = array_to_tensor(b, dtype=tf.string)
  9. p_spectrum = PSpectrumKernel(p=3)
  10. kernel = p_spectrum.compute(x, y)
  11. print(tensor_to_array(kernel, dtype=np.float32))

Credits:

The idea was born by using methods available here: https://github.com/gmum/pykernels