activeLearningGPemoc.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import math
  2. import scipy
  3. import numpy
  4. import sklearn.kernel_ridge
  5. import sys
  6. import os
  7. sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),os.pardir))
  8. import helperFunctions
  9. import activeLearningGPprototype
  10. class Regressor(activeLearningGPprototype.RegressorPrototype):
  11. def __init__(self, sigmaN = 0.01, gamma = None, kernel = 'rbf', norm = 1, configFile=None, verbose=True):
  12. activeLearningGPprototype.RegressorPrototype.__init__(self, sigmaN=sigmaN, gamma=gamma, kernel=kernel, configFile=configFile, verbose=verbose)
  13. self.norm = helperFunctions.getConfig(configFile, 'activeLearning', 'norm', norm, 'int', verbose)
  14. def gaussianAbsoluteMoment(self, muTilde, predVar):
  15. f11 = scipy.special.hyp1f1(-0.5*self.norm, 0.5, -0.5*numpy.divide(muTilde**2,predVar))
  16. prefactors = ((2 * predVar**2)**(self.norm/2.0) * math.gamma((1 + self.norm)/2.0)) / numpy.sqrt(numpy.pi)
  17. return numpy.multiply(prefactors,f11)
  18. def calcEMOC(self, x):
  19. emocScores = numpy.asmatrix(numpy.empty([x.shape[0],1], dtype=numpy.float))
  20. muTilde =numpy.asmatrix(numpy.zeros([x.shape[0],1], dtype=numpy.float))
  21. kAll = self.kernelFunc(numpy.vstack([self.X, x]))
  22. k = kAll[0:self.X.shape[0],self.X.shape[0]:]
  23. selfKdiag = numpy.asmatrix(numpy.diag(kAll[self.X.shape[0]:,self.X.shape[0]:])).T
  24. sigmaF = self.calcSigmaF(x, k, selfKdiag)
  25. moments = numpy.asmatrix(self.gaussianAbsoluteMoment(numpy.asarray(muTilde), numpy.asarray(sigmaF)))
  26. term1 = 1.0 / (sigmaF + self.sigmaN)
  27. term2 = numpy.asmatrix(numpy.ones((self.X.shape[0] + 1,x.shape[0])), dtype=numpy.float)*(-1.0)
  28. term2[0:self.X.shape[0],:] = numpy.linalg.solve(self.K + numpy.identity(self.X.shape[0], dtype=numpy.float)*self.sigmaN, k)
  29. preCalcMult = numpy.dot(term2[:-1,:].T, kAll[0:self.X.shape[0],:])
  30. for idx in range(x.shape[0]):
  31. vAll = term1[idx,:]*(preCalcMult[idx,:] + numpy.dot(term2[-1,idx].T, kAll[self.X.shape[0] + idx,:]))
  32. emocScores[idx,:] = numpy.mean(numpy.power(numpy.abs(vAll),self.norm))
  33. return numpy.multiply(emocScores,moments)
  34. def calcAlScores(self, x):
  35. return self.calcEMOC(x)