reweightGPLinK.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #! /usr/bin/python
  2. import numpy
  3. import scipy.special
  4. import sys
  5. import os
  6. sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),os.pardir))
  7. import helperFunctions
  8. class Reweighter:
  9. def __init__(self,
  10. sigmaN = 0.00178,
  11. configFile=None):
  12. self.sigmaN = helperFunctions.getConfig(configFile, 'reweighting', 'sigmaN', sigmaN, 'float', True)
  13. self.K = []
  14. self.alpha = []
  15. self.X = []
  16. self.y = []
  17. def checkModel(self):
  18. if not numpy.all(numpy.isfinite(self.K)):
  19. raise Exception('not numpy.all(numpy.isfinite(self.K))')
  20. if not numpy.all(numpy.isfinite(self.alpha)):
  21. raise Exception('not numpy.all(numpy.isfinite(self.alpha))')
  22. if not numpy.all(numpy.isfinite(self.X)):
  23. raise Exception('not numpy.all(numpy.isfinite(self.X))')
  24. if not numpy.all(numpy.isfinite(self.y)):
  25. raise Exception('not numpy.all(numpy.isfinite(self.y))')
  26. # X.shape = (number of samples, feat dim), y.shape = (number of samples, 1)
  27. def train(self, X, y):
  28. self.X = X
  29. self.y = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  30. self.K = X*X.T
  31. self.alpha = numpy.linalg.solve(self.K + numpy.identity(self.X.shape[0], dtype=numpy.float32)*self.sigmaN, self.y)
  32. self.checkModel()
  33. # x.shape = (1, feat dim), y.shape = (1, 1)
  34. def update(self, x, y):
  35. # relabel
  36. relY = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  37. # get new kernel columns
  38. k = self.X*x.T
  39. selfK = numpy.sum(numpy.multiply(x,x), axis=1)
  40. # get score of new sample
  41. infY = self.infer(x, k=k)
  42. # update alpha
  43. term1 = 1.0 / (self.sigmaN + self.calcSigmaF(x, k, selfK).item(0))
  44. term2 = numpy.asmatrix(numpy.ones((self.X.shape[0] + 1,x.shape[0])), dtype=numpy.float32) * -1.0
  45. term2[0:self.X.shape[0],:] = numpy.linalg.solve(self.K + numpy.identity(self.X.shape[0], dtype=numpy.float32)*self.sigmaN, k)
  46. term3 = infY - relY
  47. self.alpha = numpy.append(self.alpha, numpy.zeros((1,self.alpha.shape[1])), axis=0) + term1*term2*term3
  48. # update K
  49. self.K = numpy.append(numpy.append(self.K, k, axis=1), numpy.append(k.T, selfK, axis=1), axis=0)
  50. # update samples
  51. self.X = numpy.append(self.X, x, axis=0)
  52. self.y = numpy.append(self.y, relY, axis=0)
  53. self.checkModel()
  54. # X.shape = (number of samples, feat dim)
  55. def infer(self, x, k=None):
  56. if k is None:
  57. k = self.X*x.T
  58. pred = numpy.asmatrix(k.T*self.alpha)
  59. if not numpy.all(numpy.isfinite(pred)):
  60. raise Exception('not numpy.all(numpy.isfinite(pred))')
  61. return pred
  62. # X.shape = (number of samples, feat dim)
  63. def calcSigmaF(self, x, k=None, selfK=None):
  64. if k is None:
  65. k = self.X*x.T
  66. if selfK is None:
  67. selfK = numpy.sum(numpy.multiply(x,x), axis=1)
  68. sigmaF = selfK - numpy.sum(numpy.multiply(numpy.linalg.solve((self.K + numpy.identity(self.X.shape[0], dtype=numpy.float32)*self.sigmaN), k),k).T, axis=1)
  69. if not numpy.all(numpy.isfinite(sigmaF)):
  70. raise Exception('not numpy.all(numpy.isfinite(sigmaF))')
  71. return sigmaF
  72. # X.shape = (number of samples, feat dim)
  73. def reweight(self, alScores, x, k=None):
  74. if k is None:
  75. k = self.X*x.T
  76. clsScores = self.infer(x,k)
  77. var = self.calcSigmaF(x,k)
  78. probs = 0.5 - 0.5*scipy.special.erf(clsScores*(-1)/numpy.sqrt(2*var))
  79. newAlScores = numpy.multiply(alScores,(1 - probs))
  80. if not numpy.all(numpy.isfinite(newAlScores)):
  81. raise Exception('not numpy.all(numpy.isfinite(newAlScores))')
  82. return newAlScores