activeLearningWlinGPprototype.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #! /usr/bin/python
  2. import numpy
  3. import pickle
  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 ClassifierPrototype:
  9. def __init__(self,
  10. sigmaN = 0.00178,
  11. configFile=None):
  12. self.sigmaN = helperFunctions.getConfig(configFile, 'activeLearning', 'sigmaN', sigmaN, 'float', True)
  13. self.X = []
  14. self.yBin = [] # .shape = (number of samples, number of unique classes)
  15. self.yUni = []
  16. self.w = [] # .shape = (feat dim, number of unique classes)
  17. def checkModel(self):
  18. if not numpy.all(numpy.isfinite(self.w)):
  19. raise Exception('not numpy.all(numpy.isfinite(self.w))')
  20. if not numpy.all(numpy.isfinite(self.X)):
  21. raise Exception('not numpy.all(numpy.isfinite(self.X))')
  22. if not numpy.all(numpy.isfinite(self.yUni)):
  23. raise Exception('not numpy.all(numpy.isfinite(self.yUni))')
  24. def train2(self, X, y, sigmaN=None):
  25. self.train(X[0,:],y[0,:],sigmaN)
  26. for idx in range(1,X.shape[0]):
  27. self.update(X[idx,:],y[idx,:])
  28. # X.shape = (number of samples, feat dim), y.shape = (number of samples, 1)
  29. def train(self, X, y, sigmaN=None):
  30. if sigmaN is not None:
  31. self.sigmaN = sigmaN
  32. # get all known classes
  33. self.yUni = numpy.asmatrix(numpy.unique(numpy.asarray(y)))
  34. # save stuff
  35. self.X = X
  36. # get binary labels for each ovr - classifier
  37. self.yBin = numpy.asmatrix(numpy.empty((y.shape[0],self.yUni.shape[1])), dtype=numpy.int)
  38. for cls in range(self.yUni.shape[1]):
  39. self.yBin[:,cls] = 2*(y == self.yUni[0,cls]) - 1
  40. # sample reweighting
  41. rewDiagMat = helperFunctions.getReweightDiagMat(y, self.yUni)
  42. rewX = rewDiagMat*self.X
  43. rewY = rewDiagMat*self.yBin
  44. # calculate w
  45. #self.w = numpy.linalg.solve((rewX.T*rewX) + numpy.identity(X.shape[1])*self.sigmaN, rewX.T)*rewY
  46. self.w = helperFunctions.solveW(rewX, rewY, self.sigmaN)
  47. self.checkModel()
  48. # x.shape = (1, feat dim), y.shape = (1, 1)
  49. def update(self, x, y):
  50. # update samples
  51. self.X = numpy.append(self.X, x, axis=0)
  52. # update binary labels for known class
  53. self.yBin = numpy.append(self.yBin, 2*(y == self.yUni) - 1, axis=0)
  54. # create labels and w for new class
  55. if not (self.yUni == y).any():
  56. # get insertion idx
  57. idx = numpy.searchsorted(numpy.ravel(numpy.asarray(self.yUni)), y[0,0])
  58. # store new label and find new class index
  59. self.yUni = numpy.insert(self.yUni, [idx], y, axis=1)
  60. ## add new binary label vector for new class
  61. self.yBin = numpy.insert(self.yBin, [idx], numpy.zeros((self.yBin.shape[0],1), dtype=numpy.int), axis=1)
  62. self.yBin[:-1,idx] = -1
  63. self.yBin[-1,idx] = 1
  64. # sample reweighting
  65. rewDiagMat = helperFunctions.getReweightDiagMat(helperFunctions.getYfromYbin(self.yBin, self.yUni), self.yUni)
  66. rewX = rewDiagMat*self.X
  67. rewY = rewDiagMat*self.yBin[:,idx]
  68. # train new w for new class
  69. #self.w = numpy.insert(self.w, [idx], numpy.linalg.solve((rewX.T*rewX) + numpy.identity(x.shape[1])*self.sigmaN, rewX.T)*rewY, axis=1)
  70. self.w = numpy.insert(self.w, [idx], helperFunctions.solveW(rewX, rewY, self.sigmaN), axis=1)
  71. # sample reweighting
  72. rewDiagMat = helperFunctions.getReweightDiagMat(helperFunctions.getYfromYbin(self.yBin, self.yUni), self.yUni)
  73. rewX = rewDiagMat*self.X
  74. rewY = rewDiagMat*self.yBin
  75. # update w
  76. self.w = helperFunctions.solveW(rewX, rewY, self.sigmaN, self.w)
  77. self.checkModel()
  78. # X.shape = (number of samples, feat dim), loNoise = {0,1}
  79. def infer(self, x, loNoise=False):
  80. loNoise = loNoise and (self.yUni == -1).any()
  81. pred = numpy.asmatrix(numpy.dot(x,self.w[:,int(loNoise):]))
  82. if not numpy.all(numpy.isfinite(pred)):
  83. raise Exception('not numpy.all(numpy.isfinite(pred))')
  84. return pred
  85. # X.shape = (number of samples, feat dim)
  86. def test(self, x, loNoise=False):
  87. loNoise = loNoise and (self.yUni == -1).any()
  88. return self.yUni[0,numpy.argmax(self.infer(x, loNoise), axis=1) + int(loNoise)]
  89. # X.shape = (number of samples, feat dim)
  90. def calcSigmaF(self, x, tmpVec=None, rewX=None):
  91. if tmpVec is None:
  92. if rewX is None:
  93. rewDiagMat = helperFunctions.getReweightDiagMat(helperFunctions.getYfromYbin(self.yBin, self.yUni), self.yUni)
  94. rewX = numpy.dot(rewDiagMat,self.X)
  95. sigmaF = numpy.sum(numpy.multiply(x,(self.sigmaN*numpy.linalg.solve(numpy.add(numpy.dot(rewX.T,rewX), numpy.identity(x.shape[1])*self.sigmaN), x.T)).T),axis=1)
  96. else:
  97. sigmaF = self.sigmaN*tmpVec.T
  98. if not numpy.all(numpy.isfinite(sigmaF)):
  99. raise Exception('not numpy.all(numpy.isfinite(sigmaF))')
  100. return sigmaF
  101. # X.shape = (number of samples, feat dim)
  102. def calcProbs(self, x, mu=None, sigmaF=None, nmb=1000):
  103. # get mu and sigma
  104. if mu is None:
  105. mu = self.infer(x,(self.yUni == -1).any())
  106. if sigmaF is None:
  107. sigmaF = self.calcSigmaF(x)
  108. # prepare
  109. probs = numpy.asmatrix(numpy.zeros(mu.shape))
  110. for idx in range(nmb):
  111. draws = numpy.asmatrix(numpy.random.randn(mu.shape[0],mu.shape[1]))
  112. draws = numpy.add(numpy.multiply(draws, numpy.repeat(sigmaF, draws.shape[1], axis=1)), mu)
  113. maxIdx = numpy.argmax(draws, axis=1)
  114. idxs = (range(len(maxIdx)),numpy.squeeze(maxIdx))
  115. probs[idxs] = probs[idxs] + 1
  116. # convert absolute to relative amount
  117. return probs/float(nmb)
  118. # x.shape = (feat dim, number of samples)
  119. def calcAlScores(self, x):
  120. return None
  121. # x.shape = (feat dim, number of samples)
  122. def getAlScores(self, x):
  123. alScores = self.calcAlScores(x)
  124. if not numpy.all(numpy.isfinite(alScores)):
  125. raise Exception('not numpy.all(numpy.isfinite(alScores))')
  126. if alScores.shape[0] != x.shape[0] or alScores.shape[1] != 1:
  127. raise Exception('alScores.shape[0] != x.shape[0] or alScores.shape[1] != 1')
  128. return alScores
  129. # x.shape = (feat dim, number of samples)
  130. def chooseSample(self, x):
  131. return numpy.argmax(self.getAlScores(x), axis=0).item(0)