reweightLinGP.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.invCreg = []
  14. self.w = [] # .shape = (feat dim, number of unique classes)
  15. self.X = []
  16. self.y = []
  17. def checkModel(self):
  18. if not numpy.all(numpy.isfinite(self.invCreg)):
  19. raise Exception('not numpy.all(numpy.isfinite(self.invCreg))')
  20. if not numpy.all(numpy.isfinite(self.w)):
  21. raise Exception('not numpy.all(numpy.isfinite(self.w))')
  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. # save stuff
  29. self.X = X
  30. self.y = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  31. # calculate inverse of C_reg
  32. self.invCreg = numpy.linalg.inv((self.X.T*self.X) + numpy.identity(self.X.shape[1])*self.sigmaN)
  33. # calculate w
  34. self.w = self.invCreg*self.X.T*self.y
  35. self.checkModel()
  36. # x.shape = (1, feat dim), y.shape = (1, 1)
  37. def update(self, x, y):
  38. # relabel
  39. relY = numpy.asmatrix(y == -1, dtype=numpy.int)*2 - 1
  40. # update w and C_reg
  41. tmpVec = self.invCreg*x.T
  42. tmpScalar = 1.0 + x*tmpVec
  43. self.w = self.w + tmpVec*((relY - x*self.w)/tmpScalar)
  44. self.invCreg = self.invCreg - ((tmpVec*tmpVec.T)/tmpScalar)
  45. # update samples
  46. self.X = numpy.append(self.X, x, axis=0)
  47. self.y = numpy.append(self.y, relY, axis=0)
  48. self.checkModel()
  49. # X.shape = (number of samples, feat dim)
  50. def infer(self, x):
  51. pred = numpy.asmatrix(x*self.w)
  52. if not numpy.all(numpy.isfinite(pred)):
  53. raise Exception('not numpy.all(numpy.isfinite(pred))')
  54. return pred
  55. # X.shape = (number of samples, feat dim)
  56. def calcSigmaF(self, x):
  57. sigmaF = numpy.sum(numpy.multiply(x,(self.sigmaN*self.invCreg*x.T).T),axis=1)
  58. if not numpy.all(numpy.isfinite(sigmaF)):
  59. raise Exception('not numpy.all(numpy.isfinite(sigmaF))')
  60. return sigmaF
  61. # X.shape = (number of samples, feat dim)
  62. def reweight(self, alScores, x):
  63. clsScores = self.infer(x)
  64. var = self.calcSigmaF(x)
  65. probs = 0.5 - 0.5*scipy.special.erf(clsScores*(-1)/numpy.sqrt(2*var))
  66. newAlScores = numpy.multiply(alScores,(1 - probs))
  67. if not numpy.all(numpy.isfinite(newAlScores)):
  68. raise Exception('not numpy.all(numpy.isfinite(newAlScores))')
  69. return newAlScores