lcmd-epfl / Q-stack

Stack of codes for dedicated pre- and post-processing tasks for Quantum Machine Learning (QML)
MIT License
13 stars 5 forks source link

local kernels #66

Open briling opened 2 weeks ago

briling commented 2 weeks ago
briling commented 2 weeks ago

@YAY-C @liam-o-marsh Kernel names such as 'myL', 'myLfast', 'myG' are probably not very good, how would you guys replace them? (I would still keep the old ones as well for backward compatibility)

liam-o-marsh commented 2 weeks ago

I'm not even sure what the code of 'myL' does, sorry. what's the second argument in np.exp(K,K) for? (edit: nevermind the second K is for out)

liam-o-marsh commented 2 weeks ago

follow-up: I tried to rewrite the custom laplacian kernel. Is there a place where it wouldn't behave like the old version?


def my_laplacian_kernel_old(X, Y, gamma):
  """ Compute Laplacian kernel between X and Y

  .. todo::
      Write the docstring
  """
  def cdist(X, Y):
    K = np.zeros((len(X),len(Y)))
    for i,x in enumerate(X):
      x = np.array([x] * len(Y))
      d = np.abs(x-Y)
      while len(d.shape)>1:
        d = np.sum(d, axis=1) # several axis available for np > 1.7.0  (TODO shall we move this)
      K[i,:] = d
    return K
  K = -gamma * cdist(X, Y)
  np.exp(K, K)
  return K

def my_laplacian_kernel(X, Y, gamma):
  """ Compute Laplacian kernel between X and Y

  .. todo::
      Write the docstring
  """

  assert X.shape[1:] == Y.shape[1:]
  innerdims = X.ndim-1
  transposer = tuple(range(1,X.ndim)) + (0,)
  K = np.tensordot(X, Y.transpose(*transposer), innerdims)
  K *= -gamma
  np.exp(K, out=K)
  return K