Esempio n. 1
0
    def test_SVMC(self):

        # Train a svm
        svmL = AZorngCvSVM.CvSVMLearner(scaleData=False,
                                        svm_type=103,
                                        gamma=0.01,
                                        C=1,
                                        nu=0.5,
                                        p=1,
                                        eps=0.001,
                                        coef0=0,
                                        degree=3)
        svm = svmL(self.inDataC)
        trainedAcc = evalUtilities.getRMSE(self.inDataC, svm)

        self.assertEqual(round(trainedAcc, 7), round(2.8525863999999999,
                                                     7))  # ver 0.3

        # Save model
        rc = svm.write(self.modelPath)
        self.assertEqual(rc, True)
        # Load the saved model
        loadedsvm = AZorngCvSVM.CvSVMread(self.modelPath)
        loadedAcc = evalUtilities.getRMSE(self.inDataC, loadedsvm)
        # Assure equal accuracy
        self.assertEqual(trainedAcc, loadedAcc)

        svmLearner = AZorngCvSVM.CvSVMLearner(scaleData=False)

        svmLearner.name = "CvSVMLearner"
        svmLearner.eps = 0.001
        svmLearner.p = 1
        svmLearner.nu = 0.6
        svmLearner.kernel_type = 2
        svmLearner.svm_type = 103
        svmLearner.gamma = 0.0033
        svmLearner.C = 47
        svmLearner.scaleData = True
        svmLearner.scaleClass = False

        Res = orngTest.crossValidation(
            [svmLearner],
            self.inDataC,
            folds=5,
            strat=orange.MakeRandomIndices.StratifiedIfPossible)
        RMSE = evalUtilities.RMSE(Res)[0]
        self.assertEqual(round(RMSE, 2), round(2.96, 2))  #Ver 0.3

        newSVM = svmLearner(self.inDataC)
        trainedAcc = evalUtilities.getRMSE(self.inDataC, newSVM)
        # Save model
        rc = newSVM.write(self.modelPath)
        self.assertEqual(rc, True)
        # Load the saved model
        loadedsvm = AZorngCvSVM.CvSVMread(self.modelPath)
        loadedAcc = evalUtilities.getRMSE(self.inDataC, loadedsvm)
        # Assure equal accuracy
        self.assertEqual(round(trainedAcc, 4), round(2.8289, 4))  #Ver 0.3
        self.assertEqual(round(trainedAcc, 4), round(loadedAcc, 4))
Esempio n. 2
0
    def test_SVMC(self):

        # Train a svm
        svmL = AZorngCvSVM.CvSVMLearner(
            scaleData=False, svm_type=103, gamma=0.01, C=1, nu=0.5, p=1, eps=0.001, coef0=0, degree=3
        )
        svm = svmL(self.inDataC)
        trainedAcc = evalUtilities.getRMSE(self.inDataC, svm)

        self.assertEqual(round(trainedAcc, 7), round(2.8525863999999999, 7))  # ver 0.3

        # Save model
        rc = svm.write(self.modelPath)
        self.assertEqual(rc, True)
        # Load the saved model
        loadedsvm = AZorngCvSVM.CvSVMread(self.modelPath)
        loadedAcc = evalUtilities.getRMSE(self.inDataC, loadedsvm)
        # Assure equal accuracy
        self.assertEqual(trainedAcc, loadedAcc)

        svmLearner = AZorngCvSVM.CvSVMLearner(scaleData=False)

        svmLearner.name = "CvSVMLearner"
        svmLearner.eps = 0.001
        svmLearner.p = 1
        svmLearner.nu = 0.6
        svmLearner.kernel_type = 2
        svmLearner.svm_type = 103
        svmLearner.gamma = 0.0033
        svmLearner.C = 47
        svmLearner.scaleData = True
        svmLearner.scaleClass = False

        Res = orngTest.crossValidation(
            [svmLearner], self.inDataC, folds=5, strat=orange.MakeRandomIndices.StratifiedIfPossible
        )
        RMSE = evalUtilities.RMSE(Res)[0]
        self.assertEqual(round(RMSE, 2), round(2.96, 2))  # Ver 0.3

        newSVM = svmLearner(self.inDataC)
        trainedAcc = evalUtilities.getRMSE(self.inDataC, newSVM)
        # Save model
        rc = newSVM.write(self.modelPath)
        self.assertEqual(rc, True)
        # Load the saved model
        loadedsvm = AZorngCvSVM.CvSVMread(self.modelPath)
        loadedAcc = evalUtilities.getRMSE(self.inDataC, loadedsvm)
        # Assure equal accuracy
        self.assertEqual(round(trainedAcc, 4), round(2.8289, 4))  # Ver 0.3
        self.assertEqual(round(trainedAcc, 4), round(loadedAcc, 4))
Esempio n. 3
0
 def testgetRMSE(self):
     data = dataUtilities.DataTable(self.regDataPath)
     RFlearner = AZorngRF.RFLearner()
     trainData = data[0:int(len(data)/2)]
     testData = data[int(len(data)/2)+1:]
     classifier = RFlearner(data)
     RMSE = evalUtilities.getRMSE(testData,classifier)
     self.assert_(RMSE-2.07396535555 < 0.05, "Got:"+str(RMSE))
Esempio n. 4
0
 def testgetRMSE(self):
     data = dataUtilities.DataTable(self.regDataPath)
     RFlearner = AZorngRF.RFLearner()
     trainData = data[0:int(len(data) / 2)]
     testData = data[int(len(data) / 2) + 1:]
     classifier = RFlearner(data)
     RMSE = evalUtilities.getRMSE(testData, classifier)
     self.assert_(RMSE - 2.07396535555 < 0.05, "Got:" + str(RMSE))
    def test_RFRegression(self):
        """RF - Test of optimizer with continuous class data        
        """
        #Create  the appspack instance
        opt=paramOptUtilities.Appspack()
        #Learner to be optimized
        learner=AZorngRF.RFLearner()
        #dataset to use in the parameters optimization (Discrete class in this example)
        dataSet=self.contTrainDataPath
        # Define the objective function. This requires:
        #    defining the extreme to find (the min or max): findMin=True or findMin=False
        fMin=True
        #    defining the method for evaluation (must be a method that accepts as input an orngTest.ExperimentResults): 
        #       evaluateMethod="AZutilities.evalUtilities.R2"
        evalM="AZutilities.evalUtilities.RMSE"

        # Create an interface for setting optimizer parameters
        pars = AZLearnersParamsConfig.API("RFLearner")
        # Set the parameters in parameterList to be optimized
        pars.setParameter("NumThreads","optimize",False)
        # Change the default
        pars.setParameter("NumThreads","default","1")

        # Create a directory for running the appspack (if not defined it will use the present working directory)
        runPath = miscUtilities.createScratchDir(desc="ParamOptTest")

        # Run the appspack which will configure the input learner and aditionaly return 
        #[<minimum of objective function found>, <optimized parameters>]        
        tunedPars = opt(learner=learner,\
                        dataSet=dataSet,\
                        evaluateMethod = evalM,\
                        findMin=fMin,\
                        runPath = runPath,\
                        useStd = False,\
                        useParameters = pars.getParametersDict(),\
                        verbose = 0)
        print "Returned: ", tunedPars
        print "====================== optimization Done ==========================="
        print "Learner optimized flag = ", learner.optimized
        print "Tuned parameters = ", tunedPars[1]
        print "Best optimization result = ", tunedPars[0]
        print "check the file intRes.txt to see the intermediate results of optimizer!"

        self.assertEqual(opt.usedMPI,False)
        self.assertEqual(learner.optimized,True)
        self.assertEqual(round(tunedPars[0],2),round(3.1499999999999999,2))

        #The learner is now with its optimized parameters already set, so we can now make a classifier out of it
        classifier = learner(self.contTrain)
        RMSE = evalUtilities.getRMSE(self.contTest,classifier)
        self.assertEqual(round(RMSE,2),round(2.02,2)) #Ver 0.3

        #Check if the best result was not the one with numThreads different of 1 since that way we can get 
        #different results among runs
        self.assertEqual(int(tunedPars[1]["NumThreads"]),1)

        miscUtilities.removeDir(runPath)
Esempio n. 6
0
    def test_RFRegression(self):
        """RF - Test of optimizer with continuous class data        
        """
        #Create  the appspack instance
        opt = paramOptUtilities.Appspack()
        #Learner to be optimized
        learner = AZorngRF.RFLearner()
        #dataset to use in the parameters optimization (Discrete class in this example)
        dataSet = self.contTrainDataPath
        # Define the objective function. This requires:
        #    defining the extreme to find (the min or max): findMin=True or findMin=False
        fMin = True
        #    defining the method for evaluation (must be a method that accepts as input an orngTest.ExperimentResults):
        #       evaluateMethod="AZutilities.evalUtilities.R2"
        evalM = "AZutilities.evalUtilities.RMSE"

        # Create an interface for setting optimizer parameters
        pars = AZLearnersParamsConfig.API("RFLearner")
        # Set the parameters in parameterList to be optimized
        pars.setParameter("NumThreads", "optimize", False)
        # Change the default
        pars.setParameter("NumThreads", "default", "1")

        # Create a directory for running the appspack (if not defined it will use the present working directory)
        runPath = miscUtilities.createScratchDir(desc="ParamOptTest")

        # Run the appspack which will configure the input learner and aditionaly return
        #[<minimum of objective function found>, <optimized parameters>]
        tunedPars = opt(learner=learner,\
                        dataSet=dataSet,\
                        evaluateMethod = evalM,\
                        findMin=fMin,\
                        runPath = runPath,\
                        useStd = False,\
                        useParameters = pars.getParametersDict(),\
                        verbose = 0)
        print "Returned: ", tunedPars
        print "====================== optimization Done ==========================="
        print "Learner optimized flag = ", learner.optimized
        print "Tuned parameters = ", tunedPars[1]
        print "Best optimization result = ", tunedPars[0]
        print "check the file intRes.txt to see the intermediate results of optimizer!"

        self.assertEqual(opt.usedMPI, False)
        self.assertEqual(learner.optimized, True)
        self.assertEqual(round(tunedPars[0], 2), round(3.1499999999999999, 2))

        #The learner is now with its optimized parameters already set, so we can now make a classifier out of it
        classifier = learner(self.contTrain)
        RMSE = evalUtilities.getRMSE(self.contTest, classifier)
        self.assertEqual(round(RMSE, 2), round(2.02, 2))  #Ver 0.3

        #Check if the best result was not the one with numThreads different of 1 since that way we can get
        #different results among runs
        self.assertEqual(int(tunedPars[1]["NumThreads"]), 1)

        miscUtilities.removeDir(runPath)
Esempio n. 7
0
    def testPersistentRegAcc(self):
        """Test the persistence of the learner as Regressor
        Assure that the RMSE is perserved for models trained in the same way. 
        """
        PLSlearner = AZorngPLS.PLSLearner(k="3", method="simpls", precision="1e-6")
        PLSRegressor = PLSlearner(self.contTrain)

        # Calculate RMSE 
        RegressorRMSE = evalUtilities.getRMSE(self.contTest, PLSRegressor)

        # Check that RMSE is what it used to be
        self.assertEqual(round(0.67276457999999995,8),round(RegressorRMSE,8))
Esempio n. 8
0
    def testPersistentRegAcc(self):
        """Test the persistence of the learner as Regressor
        Assure that the RMSE is perserved for models trained in the same way. 
        """
        PLSlearner = AZorngPLS.PLSLearner(k="3",
                                          method="simpls",
                                          precision="1e-6")
        PLSRegressor = PLSlearner(self.contTrain)

        # Calculate RMSE
        RegressorRMSE = evalUtilities.getRMSE(self.contTest, PLSRegressor)

        # Check that RMSE is what it used to be
        self.assertEqual(round(4.39715452, 8), round(RegressorRMSE, 8))
Esempio n. 9
0
    def test_PersistentRegAcc(self): 
        """
        Assure that the accuracy is perserved for models trained in the same way. 
        """
        # Create a RF model
        RFlearner = AZorngRF.RFLearner(NumThreads = 1, maxDepth = "20", minSample = "5", useSurrogates = "false", getVarVariance = "false", \
                                        nActVars = "0", nTrees = "100", forestAcc = "0.1", termCrit = "0")
        RFmodel = RFlearner(self.trainDataReg)

        # Calculate classification accuracy 
        Acc = evalUtilities.getRMSE(self.testDataReg, RFmodel)

        # Check that the accuracy is what it used to be
        self.assertEqual(round(2.0158,5),round(Acc,5)) #opencv1.1:  0.32984999999999998,5
Esempio n. 10
0
    def test_RF_Regression(self):
        """RF - Test of optimizer with continuous class data        
        """
        expectedRes = [
            3.27, 3.2599999999999998, 3.15
        ]  #Ver 0.3 - Artifact: The second value can be expected on other Systems
        #Create  the appspack instance
        opt = paramOptUtilities.Appspack()
        #Learner to be optimized
        learner = AZorngRF.RFLearner()
        #dataset to use in the parameters optimization
        dataSet = self.contTrainDataPath
        # Define the objective function. This requires:
        #    defining the extreme to find (the min or max): findMin=True or findMin=False
        fMin = True
        #    defining the method for evaluation (must be a method that accepts as input an orngTest.ExperimentResults):
        #       evaluateMethod="AZutilities.evalUtilities.R2"
        evalM = "AZutilities.evalUtilities.RMSE"

        # Create a directory for running the appspack (if not defined it will use the present working directory)
        runPath = miscUtilities.createScratchDir(desc="ParamOptTest")

        # Run the appspack which will configure the input learner and aditionaly return
        #[<minimum of objective function found>, <optimized parameters>]
        tunedPars = opt(learner=learner,\
                        dataSet=dataSet,\
                        evaluateMethod = evalM,\
                        findMin=fMin,\
                        runPath = runPath,\
                        useStd = False,\
                        verbose = 0)
        print "Returned: ", tunedPars
        print "====================== optimization Done ==========================="
        print "Learner optimized flag = ", learner.optimized
        print "Tuned parameters = ", tunedPars[1]
        print "Best optimization result = ", tunedPars[0]
        print "check the file intRes.txt to see the intermediate results of optimizer!"
        self.assertEqual(opt.usedMPI, False)
        self.assertEqual(learner.optimized, True)
        self.assert_(
            round(tunedPars[0], 2)
            in [round(x, 2) for x in expectedRes])  #Ver 0.3
        #The learner is now with its optimized parameters already set, so we can now make a classifier out of it
        classifier = learner(self.contTrain)
        RMSE = evalUtilities.getRMSE(self.contTest, classifier)
        expectedRes = [2.89, 2.0158]
        self.assert_(round(RMSE, 2)
                     in [round(x, 2) for x in expectedRes])  #Ver 0.3

        miscUtilities.removeDir(runPath)
Esempio n. 11
0
    def test_PersistentRegAcc(self):
        """
        Assure that the accuracy is perserved for models trained in the same way. 
        """
        # Create a RF model
        RFlearner = AZorngRF.RFLearner(NumThreads = 1, maxDepth = "20", minSample = "5", useSurrogates = "false", getVarVariance = "false", \
                                        nActVars = "0", nTrees = "100", forestAcc = "0.1", termCrit = "0")
        RFmodel = RFlearner(self.trainDataReg)

        # Calculate classification accuracy
        Acc = evalUtilities.getRMSE(self.testDataReg, RFmodel)

        # Check that the accuracy is what it used to be
        self.assertEqual(round(2.0158, 5),
                         round(Acc, 5))  #opencv1.1:  0.32984999999999998,5
Esempio n. 12
0
    def test_PersistentRegAcc(self): 
        """
        Assure that the accuracy is perserved for models trained in the same way. 
        """
        #This data is loaded here to speed up the test suite since it is too big
        contTestDataPath = os.path.join(AZOC.AZORANGEHOME,"tests/source/data/linearTest.tab")
        contTrainDataPath = os.path.join(AZOC.AZORANGEHOME,"tests/source/data/linearTrain.tab")
        contTrain = dataUtilities.DataTable(contTrainDataPath)
        contTest = dataUtilities.DataTable(contTestDataPath)

        # Create a CvANN model
        CvANNlearner = AZorngCvANN.CvANNLearner(randomWeights = False, nHidden = [3], nEpochs = 100,stopUPs=0)
        CvANNmodel = CvANNlearner(contTrain)
        # Calculate classification accuracy 
        Acc = evalUtilities.getRMSE(contTest, CvANNmodel)

        # Check that the accuracy is what it used to be
        self.assertEqual(round(0.109667,6),round(Acc,6))  #opencv1.1: 0.168131
Esempio n. 13
0
    def test_PersistentRegAcc(self): 
        """
        Assure that the accuracy is perserved for models trained in the same way. 
        """
        #This data is loaded here to speed up the test suite since it is too big
        contTestDataPath = os.path.join(AZOC.AZORANGEHOME,"tests/source/data/linearTest.tab")
        contTrainDataPath = os.path.join(AZOC.AZORANGEHOME,"tests/source/data/linearTrain.tab")
        contTrain = dataUtilities.DataTable(contTrainDataPath)
        contTest = dataUtilities.DataTable(contTestDataPath)

        # Create a CvANN model
        CvANNlearner = AZorngCvANN.CvANNLearner(randomWeights = False, nHidden = [3], nEpochs = 100,stopUPs=0)
        CvANNmodel = CvANNlearner(contTrain)
        # Calculate classification accuracy 
        Acc = evalUtilities.getRMSE(contTest, CvANNmodel)

        # Check that the accuracy is what it used to be
        self.assertEqual(round(0.109667,6),round(Acc,6))  #opencv1.1: 0.168131
Esempio n. 14
0
    def test_PLS_Regression(self):
        """PLS - Test of optimizer with continuous class data        
        """ 
        expectedRes = [3.27, 3.2599999999999998] #Ver 0.3 - Artifact: The second value can be expected on other Systems       
        #Create  the appspack instance
        opt=paramOptUtilities.Appspack()
        #Learner to be optimized
        learner=AZorngPLS.PLSLearner()
        #dataset to use in the parameters optimization 
        dataSet=self.contTrainDataPath
        # Define the objective function. This requires:
        #    defining the extreme to find (the min or max): findMin=True or findMin=False
        fMin=True
        #    defining the method for evaluation (must be a method that accepts as input an orngTest.ExperimentResults): 
        #       evaluateMethod="AZutilities.evalUtilities.R2"
        evalM="AZutilities.evalUtilities.RMSE"
        
        # Create a directory for running the appspack (if not defined it will use the present working directory)
        runPath = miscUtilities.createScratchDir(desc="ParamOptTest")
        
        # Run the appspack which will configure the input learner and aditionaly return 
        #[<minimum of objective function found>, <optimized parameters>]        
        tunedPars = opt(learner=learner,\
                        dataSet=dataSet,\
                        evaluateMethod = evalM,\
                        findMin=fMin,\
                        runPath = runPath,\
                        useStd = False,\
                        verbose = 0)
        print "Returned: ", tunedPars
        print "====================== optimization Done ==========================="
        print "Learner optimized flag = ", learner.optimized
        print "Tuned parameters = ", tunedPars[1]
        print "Best optimization result = ", tunedPars[0]
        print "check the file intRes.txt to see the intermediate results of optimizer!"
        self.assertEqual(opt.usedMPI,False)
        self.assertEqual(learner.optimized,True)
        self.assert_(round(tunedPars[0],2) in [round(x,2) for x in expectedRes]) #Ver 0.3
        #The learner is now with its optimized parameters already set, so we can now make a classifier out of it
        classifier = learner(self.contTrain)
        RMSE = evalUtilities.getRMSE(self.contTest,classifier)
        self.assertEqual(round(RMSE,2),round(2.8900000000000001,2)) #Ver 0.3

        miscUtilities.removeDir(runPath)
Esempio n. 15
0
    def getAcc(self):
        """ For regression problems, it returns the RMSE and the R2 
            For Classification problems, it returns CA and the ConfMat
            The return is made in a Dict: {"RMSE":0.2,"R2":0.1,"CA":0.98,"CM":[[TP, FP],[FN,TN]]}
            For the EvalResults not supported for a specific learner/datase, the respective result will be None
            It some error occurred, the respective values in the Dict will be None
        """
        if not self.__areInputsOK():
            return None
        res = {"RMSE":None,"R2":None,"CA":None,"CM":None}
        # Set the response type
        responseType =  self.data.domain.classVar.varType == orange.VarTypes.Discrete and "Classification"  or "Regression"
        

        #Create the Train and test sets
        DataIdxs = dataUtilities.SeedDataSampler(self.data, self.nExtFolds) 
        
        #Var for saving each Fols result
        results = []

        for foldN in range(self.nExtFolds):
            trainData = self.data.select(DataIdxs[foldN],negate=1)
            runPath = miscUtilities.createScratchDir(desc = "AccWOptParam")
            trainData.save(os.path.join(runPath,"trainData.tab"))
            testData = self.data.select(DataIdxs[foldN])

            paramOptUtilities.optimizeSelectedParam(
                learner = self.learner, 
                learnerName = self.learnerName,
                trainDataFile = os.path.join(runPath,"trainData.tab"), 
                paramList = self.paramList, 
                responseType = responseType, 
                grid = False, 
                useGrid = False, 
                verbose = 0, 
                queueType = "batch.q", 
                runPath = runPath, 
                nExtFolds = None, 
                nFolds = self.nInnerFolds)
            if not self.learner.optimized:
                print "The learner was not optimized."
                return None
            miscUtilities.removeDir(runPath) 
            #Train the model
            model = self.learner(trainData)
            #Test teh model
            if responseType == "Classification":
                results.append((evalUtilities.getClassificationAccuracy(testData, model), evalUtilities.getConfMat(testData, model) ) )
            else:
                results.append((evalUtilities.getRMSE(testData, model), evalUtilities.getRsqrt(testData, model) ) )

        #Calculate the average of results
        #Compute the first result (CA or RMSE)
        if responseType == "Classification":
            resName = "CA"
        else:
            resName = "RMSE"
        res[resName] = 0.0
        for r in results:
            res[resName] += r[0]
        res[resName] = res[resName] / self.nExtFolds
        #Compute the second result (ConfMat or R2)
        if responseType == "Classification":
            res["CM"] = results[0][1]                      # Get the first ConfMat
            for r in results[1:]:
                for Lidx,line in enumerate(r[1]):
                    for idx,val in enumerate(line):
                        res["CM"][Lidx][idx] = res["CM"][Lidx][idx] + val   #Add each same ConfMat position
        else:
            res["R2"] = 0.0
            for r in results:
                res["R2"] += r[1]
            res["R2"] = res["R2"] / self.nExtFolds

        if self.verbose > 0: print "AccWOptParamGetter!Results: ",results, "\n res = ",res
        return res
    def __call__(self, data, weight = None):
        bestSeed = None
        bestAcc = None
        bestNiter = None
        bestModel = None
        #fix self.nDiffIniWeights for the disabled mode
        if self.nDiffIniWeights <= 1:
            self.nDiffIniWeights = 1 #loop over n different initial weights Disabled
        #Fix self.stopUPs for the disabled mode
        if self.stopUPs <=0:
            self.stopUPs = 0  # Optimization of nIter will be disabled

        #Remove from the domain any unused values of discrete attributes including class
        data = dataUtilities.getDataWithoutUnusedValues(data,True)
        #dataUtilities.rmAllMeta(data) 
        if len(data.domain.getmetas()) == 0:
            cleanedData = data
        else:
            cleanedData = dataUtilities.getCopyWithoutMeta(data)
        # Create the imputer
        self.imputer = orange.ImputerConstructor_average(cleanedData)
        # Impute the data 
        self.trainData = self.imputer(cleanedData)
         # If we are not seetin neither weights init optimization or nEphocs optimization (opencvLayer), the do nto split the data
        if self.stopUPs != 0 or self.nDiffIniWeights > 1:
            #Define train-80% and validation set-20% of the input data
            indices = orange.MakeRandomIndices2(p0=0.2, stratified = orange.MakeRandomIndices.StratifiedIfPossible)
            ind = indices(cleanedData)
            self.trainData = cleanedData.select(ind,1)
            validationSet = cleanedData.select(ind,0)
        else:
            validationSet = None

        if self.verbose and self.nDiffIniWeights>1: print "=========== Training ",self.nDiffIniWeights," times with different initial weights =============="
        for n in range(self.nDiffIniWeights):
            if self.nDiffIniWeights <=1:
                seed=0  #in opencv  mmlann seed=0 means the seed is disabled, and original seed will be used
            else:
                seed = len(cleanedData) * len(cleanedData.domain) * (n+1)  #seed can be any integer
            #Create a model with a specific seed for training opencv ANN. 
            #Also passing the step for the nIter optimization (self.stopUPs=0 - disable nIter optimization)
            #Also passing the validation set to be used in internal opencv implemented nEphocs optimization.
            model = self.__train__(weight = None, seed = seed, validationSet = validationSet)
            #Skip evaluation if the weights loop is disabled
            if self.nDiffIniWeights <=1:
                return model
                break
            if cleanedData.domain.classVar.varType == orange.VarTypes.Discrete:
                Acc = evalUtilities.getClassificationAccuracy(validationSet, model)
            else:
                Acc = -evalUtilities.getRMSE(validationSet, model)
            if bestModel == None or (Acc > bestAcc) or (Acc == bestAcc and model.nIter < bestNiter):
                bestSeed = seed
                bestAcc = Acc
                bestNiter = model.nIter
                bestModel = model
            if self.verbose:  print "nIter:%-7s  Acc:%-20s  seed: %s" % (model.nIter,Acc,seed)

        if self.verbose: print "================ Best model Found: ==================="
        if self.verbose: print "nIter:%-7s  Acc:%-20s  seed: %s" % (bestNiter,bestAcc,bestSeed)

        # DEBUG for check if the returned model is indeed the best model, and not the last trainted
        #if cleanedData.domain.classVar.varType == orange.VarTypes.Discrete:
        #    Acc = evalUtilities.getClassificationAccuracy(validationSet, bestModel)
        #else:
        #    Acc = -evalUtilities.getRMSE(validationSet, bestModel)
        #if self.verbose: print "================ Best model returned: ==================="
        #if self.verbose:  print "nIter:%-7s  Acc:%-20s  seed: %s" % (bestModel.nIter,Acc,bestModel.seed)

        return bestModel
Esempio n. 17
0
    def __call__(self, data, weight=None):
        bestSeed = None
        bestAcc = None
        bestNiter = None
        bestModel = None
        #fix self.nDiffIniWeights for the disabled mode
        if self.nDiffIniWeights <= 1:
            self.nDiffIniWeights = 1  #loop over n different initial weights Disabled
        #Fix self.stopUPs for the disabled mode
        if self.stopUPs <= 0:
            self.stopUPs = 0  # Optimization of nIter will be disabled

        self.NTrainEx = len(data)
        #Remove from the domain any unused values of discrete attributes including class
        data = dataUtilities.getDataWithoutUnusedValues(data, True)

        #dataUtilities.rmAllMeta(data)
        if len(data.domain.getmetas()) == 0:
            cleanedData = data
        else:
            cleanedData = dataUtilities.getCopyWithoutMeta(data)
        # Create the imputer
        self.imputer = orange.ImputerConstructor_average(cleanedData)
        # Impute the data
        self.trainData = self.imputer(cleanedData)
        # If we are not seetin neither weights init optimization or nEphocs optimization (opencvLayer), the do nto split the data
        if self.stopUPs != 0 or self.nDiffIniWeights > 1:
            #Define train-80% and validation set-20% of the input data
            indices = orange.MakeRandomIndices2(
                p0=0.2,
                stratified=orange.MakeRandomIndices.StratifiedIfPossible)
            ind = indices(cleanedData)
            self.trainData = cleanedData.select(ind, 1)
            validationSet = cleanedData.select(ind, 0)
        else:
            validationSet = None

        if self.verbose and self.nDiffIniWeights > 1:
            print "=========== Training ", self.nDiffIniWeights, " times with different initial weights =============="
        for n in range(self.nDiffIniWeights):
            if self.nDiffIniWeights <= 1:
                seed = 0  #in opencv  mmlann seed=0 means the seed is disabled, and original seed will be used
            else:
                seed = len(cleanedData) * len(cleanedData.domain) * (
                    n + 1)  #seed can be any integer
            #Create a model with a specific seed for training opencv ANN.
            #Also passing the step for the nIter optimization (self.stopUPs=0 - disable nIter optimization)
            #Also passing the validation set to be used in internal opencv implemented nEphocs optimization.
            model = self.__train__(weight=None,
                                   seed=seed,
                                   validationSet=validationSet)
            #Skip evaluation if the weights loop is disabled
            if self.nDiffIniWeights <= 1:
                return model
                break
            if cleanedData.domain.classVar.varType == orange.VarTypes.Discrete:
                Acc = evalUtilities.getClassificationAccuracy(
                    validationSet, model)
            else:
                Acc = -evalUtilities.getRMSE(validationSet, model)
            if bestModel == None or (Acc > bestAcc) or (
                    Acc == bestAcc and model.nIter < bestNiter):
                bestSeed = seed
                bestAcc = Acc
                bestNiter = model.nIter
                bestModel = model
            if self.verbose:
                print "nIter:%-7s  Acc:%-20s  seed: %s" % (model.nIter, Acc,
                                                           seed)

        if self.verbose:
            print "================ Best model Found: ==================="
        if self.verbose:
            print "nIter:%-7s  Acc:%-20s  seed: %s" % (bestNiter, bestAcc,
                                                       bestSeed)

        # DEBUG for check if the returned model is indeed the best model, and not the last trainted
        #if cleanedData.domain.classVar.varType == orange.VarTypes.Discrete:
        #    Acc = evalUtilities.getClassificationAccuracy(validationSet, bestModel)
        #else:
        #    Acc = -evalUtilities.getRMSE(validationSet, bestModel)
        #if self.verbose: print "================ Best model returned: ==================="
        #if self.verbose:  print "nIter:%-7s  Acc:%-20s  seed: %s" % (bestModel.nIter,Acc,bestModel.seed)

        return bestModel