reweightGPGenK.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #! /usr/bin/python
  2. import numpy
  3. import scipy.special
  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. class Reweighter:
  10. def __init__(self, sigmaN=0.01, gamma=None, kernel='rbf', configFile=None):
  11. self.sigmaN = helperFunctions.getConfig(configFile, 'activeLearning', 'sigmaN', sigmaN, 'float', True)
  12. self.kernel = helperFunctions.getConfig(configFile, 'activeLearning', 'kernel', kernel, 'str', True)
  13. self.gamma = helperFunctions.getConfig(configFile, 'activeLearning', 'gamma', gamma, 'float', True)
  14. self.numKernelCores = helperFunctions.getConfig(configFile, 'activeLearning', 'numKernelCores', 1, 'int', True)
  15. self.K = []
  16. self.alpha = []
  17. self.X = []
  18. self.y = []
  19. self.allowedKernels = ['rbf', 'sigmoid', 'polynomial', 'poly', 'linear', 'cosine']
  20. if self.kernel not in self.allowedKernels:
  21. raise Exception('Unknown kernel %s!'%self.kernel)
  22. def kernelFunc(self, x1, x2=None, gamma=None):
  23. if gamma is not None:
  24. self.gamma = gamma
  25. if self.kernel in ['rbf']:
  26. return numpy.asmatrix(sklearn.kernel_ridge.pairwise_kernels(x1, x2, metric=self.kernel, gamma=self.gamma, n_jobs=self.numKernelCores), dtype=numpy.float)
  27. else:
  28. return numpy.asmatrix(sklearn.kernel_ridge.pairwise_kernels(x1, x2, metric=self.kernel, n_jobs=self.numKernelCores), dtype=numpy.float)
  29. def checkModel(self):
  30. if not numpy.all(numpy.isfinite(self.K)):
  31. raise Exception('not numpy.all(numpy.isfinite(self.K))')
  32. if not numpy.all(numpy.isfinite(self.alpha)):
  33. raise Exception('not numpy.all(numpy.isfinite(self.alpha))')
  34. if not numpy.all(numpy.isfinite(self.X)):
  35. raise Exception('not numpy.all(numpy.isfinite(self.X))')
  36. if not numpy.all(numpy.isfinite(self.y)):
  37. raise Exception('not numpy.all(numpy.isfinite(self.y))')
  38. # X.shape = (number of samples, feat dim), y.shape = (number of samples, 1)
  39. def train(self, X, y):
  40. self.X = X
  41. self.y = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  42. self.K = self.kernelFunc(X,X)
  43. self.alpha = numpy.linalg.solve(numpy.add(self.K, numpy.identity(self.X.shape[0], dtype=numpy.float32))*self.sigmaN, self.y)
  44. self.checkModel()
  45. # x.shape = (1, feat dim), y.shape = (1, 1)
  46. def update(self, x, y):
  47. # relabel
  48. relY = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  49. # get new kernel columns
  50. k = self.kernelFunc(self.X, x)
  51. selfK = self.getSelfK(x)
  52. # update alpha
  53. term1 = 1.0 / (self.sigmaN + self.calcSigmaF(x, k, selfK).item(0))
  54. term2 = numpy.asmatrix(numpy.ones((self.X.shape[0] + 1,x.shape[0])), dtype=numpy.float) * (-1.0)
  55. term2[0:self.X.shape[0],:] = numpy.linalg.solve(numpy.add(self.K, numpy.identity(self.X.shape[0], dtype=numpy.float)*self.sigmaN), k)
  56. term3 = self.infer(x, k=k) - relY
  57. self.alpha = numpy.add(numpy.append(self.alpha, numpy.zeros((1,self.alpha.shape[1])), axis=0), numpy.dot(numpy.dot(term1,term2),term3))
  58. # update K
  59. self.K = numpy.append(numpy.append(self.K, k, axis=1), numpy.append(k.T, selfK, axis=1), axis=0)
  60. # update samples
  61. self.X = numpy.append(self.X, x, axis=0)
  62. self.y = numpy.append(self.y, y, axis=0)
  63. self.checkModel()
  64. # X.shape = (number of samples, feat dim), loNoise = {0,1}
  65. def infer(self, x, loNoise=False, k=None):
  66. if k is None:
  67. k = self.kernelFunc(self.X, x)
  68. loNoise = loNoise and (self.yUni == -1).any()
  69. pred = numpy.asmatrix(numpy.dot(k.T,self.alpha[:,int(loNoise):]))
  70. if not numpy.all(numpy.isfinite(pred)):
  71. raise Exception('not numpy.all(numpy.isfinite(pred))')
  72. return pred
  73. def calcSigmaF(self, x, k=None, selfK=None):
  74. if k is None:
  75. k = self.kernelFunc(self.X, x)
  76. if selfK is None:
  77. selfK = self.getSelfK(x)
  78. sigmaF = numpy.subtract(selfK, numpy.sum(numpy.multiply(numpy.linalg.solve(numpy.add(self.K, numpy.identity(self.X.shape[0], dtype=numpy.float)*self.sigmaN), k),k).T, axis=1))
  79. if not numpy.all(numpy.isfinite(sigmaF)):
  80. raise Exception('not numpy.all(numpy.isfinite(sigmaF))')
  81. return sigmaF
  82. def getSelfK(self, x):
  83. selfK = numpy.asmatrix(numpy.empty([x.shape[0],1], dtype=numpy.float))
  84. for idx in range(x.shape[0]):
  85. selfK[idx,:] = self.kernelFunc(x[idx,:])
  86. return selfK
  87. # X.shape = (number of samples, feat dim)
  88. def reweight(self, alScores, x, k=None):
  89. if k is None:
  90. k = self.kernelFunc(self.X, x)
  91. clsScores = self.infer(x, k=k)
  92. var = self.calcSigmaF(x, k=k)
  93. probs = 0.5 - 0.5*scipy.special.erf(clsScores*(-1)/numpy.sqrt(2*var))
  94. newAlScores = numpy.multiply(alScores,(1 - probs))
  95. if not numpy.all(numpy.isfinite(newAlScores)):
  96. raise Exception('not numpy.all(numpy.isfinite(newAlScores))')
  97. return newAlScores