34 SAM Driver parent class that defines the methods by which models are initialised, trained and saved. 39 Initialisation for SAMDriver. 95 Function to load parameters from the model config.ini file. 97 Method to load parameters from file loaded in parser from within section trainName and store these parameters in self.paramsDict, 100 parser: SafeConfigParser with pre-read config file. 101 trainName: Section from which parameters are to be read. 104 __NotImplementedError__ if this function is not implemented by the inheriting child. 106 raise NotImplementedError(
"this needs to be implemented to use the model class")
110 Method to store variables generated during training which are required during interaction in self.paramsDict. 119 commandString =
'self.paramsDict[\'' + j +
'\'] = self.' + j
121 logging.info(commandString)
126 def testPerformance(self, testModel, Yall, Lall, YtestAll, LtestAll, verbose):
128 Method for testing the whole dataset for overall performance. Returns a confusion matrix. 130 This implementation is a standard performance testing method that can be overridden by an inheriting child. 133 testModel : SAMObject Model to be tested. 134 Yall : Numpy array with training data vectors to be tested. 135 Lall : List with corresponding training data labels. 136 YtestAll : Numpy array with testing data vectors to be tested. 137 LtestAll : List with corresponding testing data labels. 138 verbose : Boolean turning logging to stdout on or off. 141 Square numpy array confusion matrix. 143 yTesting = SAMTesting.formatDataFunc(Yall)
144 [self.
segTrainConf, self.
segTrainPerc, labelsSegTrain, labelComparisonDict] = SAMTesting.testSegments(testModel, yTesting, Lall, verbose,
147 yTesting = SAMTesting.formatDataFunc(YtestAll)
148 [self.
segTestConf, self.
segTestPerc, labelsSegTest, labelComparisonDict] = SAMTesting.testSegments(testModel, yTesting, LtestAll, verbose,
151 return self.
segTestConf, labelsSegTest, labelComparisonDict
153 def training(self, modelNumInducing, modelNumIterations, modelInitIterations, fname, save_model, economy_save,
154 keepIfPresent=True, kernelStr=None):
156 Method to train, store and load the learned model 158 This method tries reloading the model in fname. If unsuccessful or loaded model has mismatching parameters, trains a new model from scratch. 161 modelNumInducing : Integer number of inducing parameters. 162 modelNumIterations : Integer number of training iterations. 163 modelInitIterations : Integer number of initialisation iterations. 164 fname : Filename to save model to. 165 save_model : Boolean to turn saving of the model on or off. 166 economy_save : Boolean to turn economy saving on or off. Economy save saves smaller models by not storing data inside the model but keeping it stored in the data path. 167 keepIfPresent : Boolean to enable or disable loading of a model when one is available. 168 kernelStr : Kernel string with the requested kernel. If `None` the default kernel is used. 178 if not os.path.isfile(fname +
'.pickle')
or economy_save:
179 if not os.path.isfile(fname +
'.pickle'):
180 logging.info(
"Training for " + str(modelInitIterations) +
"|" + str(modelNumIterations) +
" iterations...")
186 if self.
X is not None:
188 elif self.
Quser is not None:
195 if kernelStr
is not None:
196 stringKernel =
'kernel = ' + kernelStr
198 stringKernel =
'kernel = GPy.kern.RBF(Q, ARD=False) + GPy.kern.Bias(Q) + GPy.kern.White(Q)' 200 logging.info(
'stringKernel: ' + str(stringKernel))
206 self.
SAMObject.store(observed=self.
Y, inputs=self.
X, Q=Q, kernel=kernel,
209 self.
SAMObject.model = GPy.models.SparseGPRegression(numpy.hstack((self.
X, self.
L)), self.
Y,
217 if economy_save
and os.path.isfile(fname +
'.pickle')
and keepIfPresent:
219 logging.info(
"Try loading economy size SAMObject: " + fname)
221 SAMCore.load_pruned_model(fname, economy_save, self.
SAMObject.model)
223 logging.error(
"Loading " + fname +
" failed. Parameters not valid. Training new model")
235 logging.info(
"Saving SAMObject: " + fname)
236 SAMCore.save_pruned_model(self.
SAMObject, fname, economy_save)
237 elif not os.path.isfile(fname +
'.pickle')
or not keepIfPresent:
251 logging.info(
"Saving SAMObject: " + fname)
252 SAMCore.save_pruned_model(self.
SAMObject, fname, economy_save)
254 logging.info(
"Loading SAMObject: " + fname)
255 self.
SAMObject = SAMCore.load_pruned_model(fname)
257 def prepareData(self, model='mrd', Ntr=50, randSeed=0, normalise=True):
259 Method for randomly splitting data and preparing Y dictionary. 261 This method splits the data in the model randomly between training and testing. Currently does not take equal number of samples from different classes. 264 model : String with type of model to be trained. Accepted values are `'mrd'` and `'bgplvm'`. 265 Ntr : Float percentage for the training/testing division of data. 266 randSeed : Integer random seed. 267 normalise : Boolean turning normalisation of data on or off. 273 Nts = self.
Y.shape[0] - Ntr
274 numpy.random.seed(randSeed)
275 perm = numpy.random.permutation(self.
Y.shape[0])
277 indTr = perm[Nts:Nts + Ntr]
280 YtestAll = self.
Y[indTs].copy()
281 self.
Ytest = self.
Y[indTs]
282 LtestAll = self.
L[indTs].copy()
283 self.
Ltest = self.
L[indTs]
284 Yall = self.
Y[indTr].copy()
285 self.
Y = self.
Y[indTr]
286 Lall = self.
L[indTr].copy()
287 self.
L = self.
L[indTr]
290 logging.warning(
'Normalising data')
292 self.
Ymean = self.
Y.mean()
310 self.
Y = {
'Y': self.
Yn,
'L': self.
L}
313 self.
X = self.
Y.copy()
314 self.
Y = {
'L': self.
Ln.copy() + 0.08 * numpy.random.randn(self.
Ln.shape[0], self.
Ln.shape[1])}
316 elif model ==
'bgplvm':
318 self.
Y = {
'Y': self.
Yn}
321 logging.warning(
'Not normalising data')
324 self.
Y = {
'Y': self.
Y,
'L': self.
L}
327 self.
X = self.
Y.copy()
328 self.
Y = {
'L': self.
Ln.copy() + 0.08 * numpy.random.randn(self.
Ln.shape[0], self.
Ln.shape[1])}
330 elif model ==
'bgplvm':
332 self.
Y = {
'Y': self.
Y}
335 return Yall, Lall, YtestAll, LtestAll
337 def readData(self, root_data_dir, participant_index, *args, **kw):
339 Method which accepts a data directory, reads all the data in and outputs self.Y which is a numpy array with n instances of m length feature vectors and self.L which is a list of text Labels of length n. 342 root_data_dir: Data directory. 343 participant_index: List of subfolders to consider. Can be left as an empty list. 346 __NotImplementedError__ if not implemented by the inheriting child. Otherwise returns self.Y and self.L which contain the formatted data and labels read from the data directory. 348 raise NotImplementedError(
"this needs to be implemented to use the model class")
352 Method which receives a list of data frames and outputs a classification if available or 'no_classification' if it is not. 355 dataList: List of dataFrames collected. Length of list is variable. 356 thisModel: List of models required for testing. 357 verbose : Boolean turning logging to stdout on or off. 360 __NotImplementedError__ if not implemented by the inheriting child. Otherwise returns a string classification label. 362 raise NotImplementedError(
"this needs to be implemented to use the model class")
366 Method which receives an instance generated from a model via a label and formats the received vector by reshaping it or adding crucial data with it for it to be deciphered at the receiving end. 369 instance: A vector of generated data. 372 __NotImplementedError__ if not implemented by the inheriting child. Otherwise returns a Yarp Bottle or Yarp Image with the formatted generated data. 374 raise NotImplementedError(
"this needs to be implemented to use the model class")
def saveParameters(self)
Method to store variables generated during training which are required during interaction in self...
def formatGeneratedData(self, instance)
Method which receives an instance generated from a model via a label and formats the received vector ...
def __init__(self)
Initialisation for SAMDriver.
SAM Driver parent class that defines the methods by which models are initialised, trained and saved...
def training(self, modelNumInducing, modelNumIterations, modelInitIterations, fname, save_model, economy_save, keepIfPresent=True, kernelStr=None)
Method to train, store and load the learned model.
def processLiveData(self, dataList, thisModel, verbose=False)
Method which receives a list of data frames and outputs a classification if available or 'no_classifi...
def prepareData(self, model='mrd', Ntr=50, randSeed=0, normalise=True)
Method for randomly splitting data and preparing Y dictionary.
def testPerformance(self, testModel, Yall, Lall, YtestAll, LtestAll, verbose)
Method for testing the whole dataset for overall performance.
def loadParameters(self, parser, trainName)
Function to load parameters from the model config.ini file.
def readData(self, root_data_dir, participant_index, args, kw)
Method which accepts a data directory, reads all the data in and outputs self.Y which is a numpy arra...
SAM based on Latent Feature Models.