本文整理汇总了Python中Utils.Parameters.get方法的典型用法代码示例。如果您正苦于以下问题:Python Parameters.get方法的具体用法?Python Parameters.get怎么用?Python Parameters.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Utils.Parameters
的用法示例。
在下文中一共展示了Parameters.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getSteps
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def getSteps(step, omitSteps, mainSteps):
# Determine substep to start from, for the main step from which processing starts
step = Parameters.get(step, mainSteps)
fromMainStep = None
fromSubStep = {} # The substep to start from, for the main step to start from
for mainStep in step.keys():
fromSubStep[mainStep] = step[mainStep] # the sub step to start from
if step[mainStep] != None:
assert fromMainStep == None # processing can start from one place only
fromMainStep = mainStep
if step[mainStep] == True:
fromSubStep[mainStep] = None
else:
assert type(step[mainStep]) in types.StringTypes # no list allowed, processing can start from one place only
# Determine steps to omit
omitSubSteps = {} # Skip these substeps. If the value is True, skip the entire main step.
omitMainSteps = []
omitSteps = Parameters.get(omitSteps, mainSteps)
for mainStep in omitSteps.keys():
omitSubSteps[mainStep] = omitSteps[mainStep]
if omitSteps[mainStep] == True:
omitMainSteps.append(mainStep)
omitSubSteps[mainStep] = None
# Initialize main step selector
if fromMainStep != None:
if fromSubStep[fromMainStep] != None:
print >> sys.stderr, "Starting process from step", fromMainStep + ", substep", fromSubStep[fromMainStep]
else:
print >> sys.stderr, "Starting process from step", fromMainStep
selector = StepSelector(mainSteps, fromStep=fromMainStep, omitSteps=omitMainSteps)
return selector, fromSubStep, omitSubSteps
示例2: trainUnmergingDetector
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def trainUnmergingDetector(self):
xml = None
if not self.unmerging:
print >> sys.stderr, "No unmerging"
if self.checkStep("SELF-TRAIN-EXAMPLES-FOR-UNMERGING", self.unmerging) and self.unmerging:
# Self-classified train data for unmerging
if self.doUnmergingSelfTraining:
# This allows limiting to a subcorpus
triggerStyle = copy.copy(Parameters.get(self.triggerExampleStyle))
edgeStyle = copy.copy(Parameters.get(self.edgeExampleStyle))
unmergingStyle = Parameters.get(self.unmergingExampleStyle)
if "sentenceLimit" in unmergingStyle and unmergingStyle["sentenceLimit"]:
triggerStyle["sentenceLimit"] = unmergingStyle["sentenceLimit"]
edgeStyle["sentenceLimit"] = unmergingStyle["sentenceLimit"]
# Build the examples
xml = self.triggerDetector.classifyToXML(self.trainData, self.model, None, self.workDir+"unmerging-extra-", exampleStyle=triggerStyle)#, recallAdjust=0.5)
xml = self.edgeDetector.classifyToXML(xml, self.model, None, self.workDir+"unmerging-extra-", exampleStyle=edgeStyle)#, recallAdjust=0.5)
assert xml != None
EvaluateInteractionXML.run(self.edgeDetector.evaluator, xml, self.trainData, self.parse)
else:
print >> sys.stderr, "No self-training for unmerging"
if self.checkStep("UNMERGING-EXAMPLES", self.unmerging) and self.unmerging:
# Unmerging example generation
GOLD_TEST_FILE = self.optData.replace("-nodup", "")
GOLD_TRAIN_FILE = self.trainData.replace("-nodup", "")
if self.doUnmergingSelfTraining:
if xml == None:
xml = self.workDir+"unmerging-extra-edge-pred.xml.gz"
self.unmergingDetector.buildExamples(self.model, [self.optData.replace("-nodup", ""), [self.trainData.replace("-nodup", ""), xml]],
[self.workDir+"unmerging-opt-examples.gz", self.workDir+"unmerging-train-examples.gz"],
[GOLD_TEST_FILE, [GOLD_TRAIN_FILE, GOLD_TRAIN_FILE]],
exampleStyle=self.unmergingExampleStyle, saveIdsToModel=True)
xml = None
else:
self.unmergingDetector.buildExamples(self.model, [self.optData.replace("-nodup", ""), self.trainData.replace("-nodup", "")],
[self.workDir+"unmerging-opt-examples.gz", self.workDir+"unmerging-train-examples.gz"],
[GOLD_TEST_FILE, GOLD_TRAIN_FILE],
exampleStyle=self.unmergingExampleStyle, saveIdsToModel=True)
xml = None
#UnmergingExampleBuilder.run("/home/jari/biotext/EventExtension/TrainSelfClassify/test-predicted-edges.xml", GOLD_TRAIN_FILE, UNMERGING_TRAIN_EXAMPLE_FILE, PARSE, TOK, UNMERGING_FEATURE_PARAMS, UNMERGING_IDS, append=True)
if self.checkStep("BEGIN-UNMERGING-MODEL", self.unmerging) and self.unmerging:
self.unmergingDetector.beginModel(None, self.model, self.workDir+"unmerging-train-examples.gz", self.workDir+"unmerging-opt-examples.gz")
if self.checkStep("END-UNMERGING-MODEL", self.unmerging) and self.unmerging:
self.unmergingDetector.endModel(None, self.model, self.workDir+"unmerging-opt-examples.gz")
print >> sys.stderr, "Adding unmerging classifier model to test-set event model"
if self.combinedModel != None:
self.combinedModel.addStr("unmerging-example-style", self.model.getStr("unmerging-example-style"))
self.combinedModel.insert(self.model.get("unmerging-ids.classes"), "unmerging-ids.classes")
self.combinedModel.insert(self.model.get("unmerging-ids.features"), "unmerging-ids.features")
self.unmergingDetector.addClassifierModel(self.combinedModel, self.model.get("unmerging-classifier-model", True),
self.model.getStr("unmerging-classifier-parameter"))
self.combinedModel.save()
示例3: doGrid
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def doGrid(self):
print >> sys.stderr, "--------- Booster parameter search ---------"
# Build trigger examples
self.triggerDetector.buildExamples(self.model, [self.optData], [self.workDir+"grid-trigger-examples.gz"])
if self.fullGrid:
# Parameters to optimize
ALL_PARAMS={
"trigger":[int(i) for i in Parameters.get(self.triggerClassifierParameters, valueListKey="c")["c"]],
"booster":[float(i) for i in self.recallAdjustParameters.split(",")],
"edge":[int(i) for i in Parameters.get(self.edgeClassifierParameters, valueListKey="c")["c"]] }
else:
ALL_PARAMS={"trigger":Parameters.get(self.model.getStr(self.triggerDetector.tag+"classifier-parameter"), valueListKey="c")["c"],
"booster":[float(i) for i in self.recallAdjustParameters.split(",")],
"edge":Parameters.get(self.model.getStr(self.edgeDetector.tag+"classifier-parameter"), valueListKey="c")["c"]}
paramCombinations = Parameters.getCombinations(ALL_PARAMS, ["trigger", "booster", "edge"])
prevParams = None
EDGE_MODEL_STEM = os.path.join(self.edgeDetector.workDir, os.path.normpath(self.model.path)+"-edge-models/model-c_")
TRIGGER_MODEL_STEM = os.path.join(self.triggerDetector.workDir, os.path.normpath(self.model.path)+"-trigger-models/model-c_")
bestResults = None
for i in range(len(paramCombinations)):
params = paramCombinations[i]
print >> sys.stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
print >> sys.stderr, "Processing params", str(i+1) + "/" + str(len(paramCombinations)), params
print >> sys.stderr, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
# Triggers and Boost
if prevParams == None or prevParams["trigger"] != params["trigger"] or prevParams["booster"] != params["booster"]:
print >> sys.stderr, "Classifying trigger examples for parameters", "trigger:" + str(params["trigger"]), "booster:" + str(params["booster"])
xml = self.triggerDetector.classifyToXML(self.optData, self.model, self.workDir+"grid-trigger-examples.gz", self.workDir+"grid-", classifierModel=TRIGGER_MODEL_STEM+str(params["trigger"]), recallAdjust=params["booster"])
prevParams = params
# Build edge examples
self.edgeDetector.buildExamples(self.model, [xml], [self.workDir+"grid-edge-examples.gz"], [self.optData])
# Classify with pre-defined model
edgeClassifierModel=EDGE_MODEL_STEM+str(params["edge"])
xml = self.edgeDetector.classifyToXML(xml, self.model, self.workDir+"grid-edge-examples.gz", self.workDir+"grid-", classifierModel=edgeClassifierModel)
bestResults = self.evaluateGrid(xml, params, bestResults)
print >> sys.stderr, "Booster search complete"
print >> sys.stderr, "Tested", len(paramCombinations), "combinations"
print >> sys.stderr, "Best parameters:", bestResults[0]
print >> sys.stderr, "Best result:", bestResults[2] # f-score
# Save grid model
self.saveStr("recallAdjustParameter", str(bestResults[0]["booster"]), self.model)
self.saveStr("recallAdjustParameter", str(bestResults[0]["booster"]), self.combinedModel, False)
if self.fullGrid: # define best models
self.triggerDetector.addClassifierModel(self.model, TRIGGER_MODEL_STEM+str(bestResults[0]["trigger"]), bestResults[0]["trigger"])
self.edgeDetector.addClassifierModel(self.model, EDGE_MODEL_STEM+str(bestResults[0]["edge"]), bestResults[0]["edge"])
# Remove work files
for stepTag in [self.workDir+"grid-trigger", self.workDir+"grid-edge", self.workDir+"grid-unmerging"]:
for fileStem in ["-classifications", "-classifications.log", "examples.gz", "pred.xml.gz"]:
if os.path.exists(stepTag+fileStem):
os.remove(stepTag+fileStem)
示例4: addClassifierModel
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def addClassifierModel(self, model, classifierModelPath, classifierParameters, threshold=None):
classifierModel = model.get(self.tag+"classifier-model", True)
shutil.copy2(classifierModelPath, classifierModel)
model.addStr(self.tag+"classifier-parameter", Parameters.toString(Parameters.get(classifierParameters)))
if threshold != None:
model.addStr(self.tag+"threshold", str(threshold))
return classifierModel
示例5: train
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def train(self, examples, outDir, parameters, classifyExamples=None, dummy=False):
outDir = os.path.abspath(outDir)
examples = self.getExampleFile(examples, dummy=dummy)
classifyExamples = self.getExampleFile(classifyExamples, dummy=dummy)
# Return a new classifier instance for following the training process and using the model
classifier = copy.copy(self)
classifier.parameters = parameters
classifier._filesToRelease = [examples, classifyExamples]
if not os.path.exists(outDir):
os.makedirs(outDir)
trainFeatures, trainClasses = datasets.load_svmlight_file(examples)
if classifyExamples != None:
develFeatures, develClasses = datasets.load_svmlight_file(classifyExamples, trainFeatures.shape[1])
binarizer = preprocessing.LabelBinarizer()
binarizer.fit(trainClasses)
trainClasses = binarizer.transform(trainClasses)
if classifyExamples != None:
develClasses = binarizer.transform(develClasses)
print >> sys.stderr, "Training Keras model with parameters:", parameters
parameters = Parameters.get(parameters, {"TEES.classifier":"KerasClassifier", "layers":5, "lr":0.001, "epochs":1, "batch_size":64, "patience":10})
np.random.seed(10)
classifier.kerasModel = classifier._defineModel(outDir, parameters, trainFeatures, trainClasses, develFeatures, develClasses)
classifier._fitModel(outDir, parameters, trainFeatures, trainClasses, develFeatures, develClasses)
示例6: getConnection
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def getConnection(connection): #, account=None, workDirBase=None, remoteSettingsPath=None):
if connection == None: # return a "dummy" local connection
return getConnection("connection=Unix:jobLimit=1")
elif type(connection) in types.StringTypes and hasattr(Settings, connection): # connection is a Settings key
print >> sys.stderr, "Using connection", connection
return getConnection(getattr(Settings, connection))
#return getConnection(*getattr(Settings, connection))
else: # connection is a parameter string or dictionary
defaultParams = dict.fromkeys(["connection", "account", "workdir", "settings", "memory", "cores", "modules", "wallTime", "jobLimit", "preamble", "debug"])
defaultParams["debug"] = False
connection = Parameters.get(connection, valueListKey="connection", valueTypes={"debug":[bool]}, defaults=defaultParams)
if connection["connection"] == None:
connection["connection"] = "Unix"
if connection["account"] == None:
assert connection["workdir"] == None
#assert remoteSettingsPath == None
print >> sys.stderr, "New local connection", Parameters.toString(connection)
else:
print >> sys.stderr, "New remote connection:", Parameters.toString(connection)
# Make the connection
exec "ConnectionClass = " + connection["connection"] + "Connection"
connectionArgs = {}
for key in connection:
if key != "connection" and connection[key] != None:
connectionArgs[key] = connection[key]
return ConnectionClass(**connectionArgs)
示例7: classifyToXML
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def classifyToXML(self, data, model, exampleFileName=None, tag="", classifierModel=None, goldData=None, parse=None, recallAdjust=None, compressExamples=True, exampleStyle=None):
model = self.openModel(model, "r")
if parse == None:
parse = self.getStr(self.tag+"parse", model)
if exampleFileName == None:
exampleFileName = tag+self.tag+"examples"
if compressExamples:
exampleFileName += ".gz"
self.buildExamples(model, [data], [exampleFileName], [goldData], parse=parse, exampleStyle=exampleStyle)
if classifierModel == None:
classifierModel = model.get(self.tag+"classifier-model")
else:
assert os.path.exists(classifierModel), classifierModel
classifier = self.Classifier()
classifier.classify(exampleFileName, tag+self.tag+"classifications", classifierModel, finishBeforeReturn=True)
threshold = model.getStr(self.tag+"threshold", defaultIfNotExist=None, asType=float)
predictions = ExampleUtils.loadPredictions(tag+self.tag+"classifications", recallAdjust, threshold=threshold)
evaluator = self.evaluator.evaluate(exampleFileName, predictions, model.get(self.tag+"ids.classes"))
#outputFileName = tag+"-"+self.tag+"pred.xml.gz"
#exampleStyle = self.exampleBuilder.getParameters(model.getStr(self.tag+"example-style"))
if exampleStyle == None:
exampleStyle = Parameters.get(model.getStr(self.tag+"example-style")) # no checking, but these should already have passed the ExampleBuilder
return self.exampleWriter.write(exampleFileName, predictions, data, tag+self.tag+"pred.xml.gz", model.get(self.tag+"ids.classes"), parse, exampleStyle=exampleStyle)
# if evaluator.getData().getTP() + evaluator.getData().getFP() > 0:
# return self.exampleWriter.write(exampleFileName, predictions, data, outputFileName, model.get(self.tag+"ids.classes"), parse)
# else:
# # TODO: e.g. interactions must be removed if task does unmerging
# print >> sys.stderr, "No positive", self.tag + "predictions, XML file", outputFileName, "unchanged from input"
# if type(data) in types.StringTypes: # assume its a file
# shutil.copy(data, outputFileName)
# else: # assume its an elementtree
# ETUtils.write(data, outputFileName)
# #print >> sys.stderr, "No positive predictions, XML file", tag+self.tag+"pred.xml", "not written"
# return data #None
示例8: saveModel
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def saveModel(self, teesModel, tag=""):
if hasattr(self, "model") and self.model != None:
teesModelPath = teesModel.get(tag+"classifier-model", True)
shutil.copy2(self.model, teesModelPath)
if hasattr(self, "parameters") and self.parameters != None:
teesModel.addStr(tag+"classifier-parameter", Parameters.toString(Parameters.get(self.parameters)))
if hasattr(self, "threshold") and self.threshold != None:
teesModel.addStr(tag+"threshold", str(self.threshold))
示例9: getBioNLPSharedTaskParams
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def getBioNLPSharedTaskParams(self, parameters=None, model=None):
if parameters == None:
if model != None:
model = self.openModel(model, "r")
parameters = model.getStr("BioNLPSTParams", defaultIfNotExist=None)
else:
parameters = {}
return Parameters.get(parameters, ["convert", "evaluate", "scores", "a2Tag"])
示例10: getClassifier
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def getClassifier(self, parameters):
#parameters = Parameters.get(parameters, ["TEES.threshold", "TEES.classifier", "c"], valueListKey="c")
parameters = Parameters.get(parameters, ["TEES.threshold", "TEES.classifier"], allowNew=True, valueListKey="c")
if parameters["TEES.classifier"] == None:
return self.Classifier
else:
exec "from Classifiers." + parameters["TEES.classifier"] + " import " + parameters["TEES.classifier"] + " as " + parameters["TEES.classifier"]
return eval(parameters["TEES.classifier"])
示例11: optimize
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def optimize(self, examples, outDir, parameters, classifyExamples, classIds, step="BOTH", evaluator=None, determineThreshold=False, timeout=None, downloadAllModels=False):
assert step in ["BOTH", "SUBMIT", "RESULTS"], step
outDir = os.path.abspath(outDir)
# Initialize training (or reconnect to existing jobs)
combinations = Parameters.getCombinations(Parameters.get(parameters, valueListKey="c")) #Core.OptimizeParameters.getParameterCombinations(parameters)
trained = []
for combination in combinations:
trained.append( self.train(examples, outDir, combination, classifyExamples, replaceRemoteExamples=(len(trained) == 0), dummy=(step == "RESULTS")) )
if step == "SUBMIT": # Return already
classifier = copy.copy(self)
classifier.setState("OPTIMIZE")
return classifier
# Wait for the training to finish
finalJobStatus = self.connection.waitForJobs([x.getJob() for x in trained])
# Evaluate the results
print >> sys.stderr, "Evaluating results"
#Stream.setIndent(" ")
bestResult = None
if evaluator == None:
evaluator = self.defaultEvaluator
for i in range(len(combinations)):
id = trained[i].parameterIdStr
#Stream.setIndent(" ")
# Get predictions
predictions = None
if trained[i].getStatus() == "FINISHED":
predictions = trained[i].downloadPredictions()
else:
print >> sys.stderr, "No results for combination" + id
continue
if downloadAllModels:
trained[i].downloadModel()
# Compare to other results
print >> sys.stderr, "*** Evaluating results for combination" + id + " ***"
threshold = None
if determineThreshold:
print >> sys.stderr, "Thresholding, original micro =",
evaluation = evaluator.evaluate(classifyExamples, predictions, classIds, os.path.join(outDir, "evaluation-before-threshold" + id + ".csv"), verbose=False)
print >> sys.stderr, evaluation.microF.toStringConcise()
threshold, bestF = evaluator.threshold(classifyExamples, predictions)
print >> sys.stderr, "threshold =", threshold, "at binary fscore", str(bestF)[0:6]
evaluation = evaluator.evaluate(classifyExamples, ExampleUtils.loadPredictions(predictions, threshold=threshold), classIds, os.path.join(outDir, "evaluation" + id + ".csv"))
if bestResult == None or evaluation.compare(bestResult[0]) > 0: #: averageResult.fScore > bestResult[1].fScore:
bestResult = [evaluation, trained[i], combinations[i], threshold]
if not self.connection.isLocal():
os.remove(predictions) # remove predictions to save space
#Stream.setIndent()
if bestResult == None:
raise Exception("No results for any parameter combination")
print >> sys.stderr, "*** Evaluation complete", finalJobStatus, "***"
print >> sys.stderr, "Selected parameters", bestResult[2]
classifier = copy.copy(bestResult[1])
classifier.threshold = bestResult[3]
classifier.downloadModel()
return classifier
示例12: getParameters
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def getParameters(self, parameters=None, model=None, defaultValue=None, modelParameterStringName=None):
if modelParameterStringName == None:
modelParameterStringName = self.modelParameterStringName
if parameters == None and model != None:
model = self.openModel(model, "r")
parameters = model.getStr(modelParameterStringName, defaultIfNotExist=None)
defaultStepNames = [x[0] for x in self.getDefaultSteps()]
valueLimits={"omitSteps":defaultStepNames + [None], "intermediateFiles":defaultStepNames + [True, None]}
defaults = self.getDefaultParameters(defaultValue=defaultValue)
return Parameters.get(parameters, defaults, valueLimits=valueLimits)
示例13: train
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def train(self, examples, outDir, parameters, classifyExamples=None, finishBeforeReturn=False, replaceRemoteExamples=True, dummy=False):
outDir = os.path.abspath(outDir)
examples = self.getExampleFile(examples, replaceRemote=replaceRemoteExamples, dummy=dummy)
classifyExamples = self.getExampleFile(classifyExamples, replaceRemote=replaceRemoteExamples, dummy=dummy)
parameters = Parameters.get(parameters, valueListKey="c")
trainDir = self.connection.getSetting(self.trainDirSetting)
# Return a new classifier instance for following the training process and using the model
classifier = copy.copy(self)
classifier.setState("TRAIN")
classifier.parameters = parameters
classifier._filesToRelease = [examples, classifyExamples]
# Train
if not os.path.exists(outDir):
os.makedirs(outDir)
trainCommand = os.path.join(trainDir, self.trainCommand)
paramKeys = sorted(parameters.keys())
idStr = ""
paramString = ""
for key in paramKeys:
if key.startswith("TEES."):
continue
if len(paramString) > 0 and not paramString.endswith(" "):
paramString += " "
if parameters[key] != None:
paramString += self.parameterFormat.replace("%k", key).replace("%v", str(parameters[key])).strip()
idStr += "-" + str(key) + "_" + str(parameters[key])
else:
paramString += self.parameterFormat.replace("%k", key).replace("%v", "").strip()
idStr += "-" + str(key)
classifier.parameterIdStr = idStr
classifier.model = self.connection.getRemotePath(outDir + "/model" + idStr, True)
modelPath = self.connection.getRemotePath(outDir + "/model" + idStr, False)
trainCommand = trainCommand.replace("%p", paramString).replace("%e", examples).replace("%m", modelPath).strip()
self.connection.addCommand(trainCommand)
# Classify with the trained model (optional)
if classifyExamples != None:
classifier.predictions = self.connection.getRemotePath(outDir + "/predictions" + idStr, True)
predictionsPath = self.connection.getRemotePath(outDir + "/predictions" + idStr, False)
classifyDir = self.connection.getSetting(self.classifyDirSetting)
classifyCommand = os.path.join(classifyDir, self.classifyCommand).replace("%e", classifyExamples).replace("%m", modelPath).replace("%c", predictionsPath).strip()
self.connection.addCommand(classifyCommand)
# Run the process
jobName = self.trainCommand.split()[0] + idStr
logPath = outDir + "/" + jobName
if dummy: # return a classifier that connects to an existing job
self.connection.clearCommands()
classifier._job = self.connection.getJob(jobDir=outDir, jobName=jobName)
else: # submit the job
classifier._job = self.connection.submit(jobDir=outDir, jobName=jobName, stdout=logPath+".stdout")
if finishBeforeReturn:
self.connection.waitForJob(classifier._job)
self.getStatus()
return classifier
示例14: train
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def train(self, examples, outDir, parameters, classifyExamples=None, finishBeforeReturn=False, replaceRemoteExamples=True, dummy=False):
outDir = os.path.abspath(outDir)
examples = self.getExampleFile(examples, replaceRemote=replaceRemoteExamples, dummy=dummy)
classifyExamples = self.getExampleFile(classifyExamples, replaceRemote=replaceRemoteExamples, dummy=dummy)
#parameters = Parameters.get(parameters, valueListKey="c")
trainDir = ""
if self.trainDirSetting:
trainDir = os.path.normpath(self.connection.getSetting(self.trainDirSetting)) + os.path.sep
# Return a new classifier instance for following the training process and using the model
classifier = copy.copy(self)
classifier.setState("TRAIN")
classifier.parameters = parameters
classifier._filesToRelease = [examples, classifyExamples]
# Train
if not os.path.exists(outDir):
os.makedirs(outDir)
#trainCommand = os.path.join(trainDir, self.trainCommand)
trainCommand = self.trainCommand.replace("%d", trainDir)
parameters = Parameters.get(parameters, self.parameterDefaults["train"], self.parameterAllowNew["train"],
self.parameterValueListKey["train"], self.parameterValueLimits["train"],
self.parameterValueTypes["train"])
paramString, idStr = self._getParameterString(parameters)
classifier.parameterIdStr = idStr
classifier.model = self.connection.getRemotePath(outDir + "/model" + idStr, True)
modelPath = self.connection.getRemotePath(outDir + "/model" + idStr, False)
trainCommand = trainCommand.replace("%p", paramString).replace("%e", examples).replace("%m", modelPath).strip()
self.connection.addCommand(trainCommand)
# Classify with the trained model (optional)
if classifyExamples != None:
classifier.predictions = self.connection.getRemotePath(outDir + "/predictions" + idStr, True)
predictionsPath = self.connection.getRemotePath(outDir + "/predictions" + idStr, False)
classifyDir = ""
if self.classifyDirSetting:
classifyDir = os.path.normpath(self.connection.getSetting(self.classifyDirSetting)) + os.path.sep
classifyCommand = self.classifyCommand.replace("%d", classifyDir).replace("%e", classifyExamples).replace("%m", modelPath).replace("%c", predictionsPath).strip()
self.connection.addCommand(classifyCommand)
# Run the process
jobName = self.trainCommand.split()[0].replace("%d", "") + idStr
logPath = outDir + "/" + jobName
if dummy: # return a classifier that connects to an existing job
self.connection.clearCommands()
classifier._job = self.connection.getJob(jobDir=outDir, jobName=jobName)
else: # submit the job
classifier._job = self.connection.submit(jobDir=outDir, jobName=jobName, stdout=logPath+".stdout")
if finishBeforeReturn:
self.connection.waitForJob(classifier._job)
self.getStatus()
return classifier
示例15: train
# 需要导入模块: from Utils import Parameters [as 别名]
# 或者: from Utils.Parameters import get [as 别名]
def train(self, examples, outDir, parameters, classifyExamples=None, finishBeforeReturn=False, replaceRemoteExamples=True, dummy=False):
outDir = os.path.abspath(outDir)
examples = self.getExampleFile(examples, replaceRemote=replaceRemoteExamples, dummy=dummy)
classifyExamples = self.getExampleFile(classifyExamples, replaceRemote=replaceRemoteExamples, dummy=dummy)
parameters = Parameters.get(parameters, valueListKey="c")
svmMulticlassDir = self.connection.getSetting("SVM_MULTICLASS_DIR")
# Return a new classifier instance for following the training process and using the model
classifier = copy.copy(self)
classifier.setState("TRAIN")
classifier.parameters = parameters
# Train
if not os.path.exists(outDir):
os.makedirs(outDir)
trainCommand = svmMulticlassDir + "/svm_multiclass_learn "
paramKeys = sorted(parameters.keys())
idStr = ""
for key in paramKeys:
trainCommand += "-" + str(key) + " "
idStr += "-" + str(key)
if parameters[key] != None:
trainCommand += str(parameters[key]) + " "
idStr += "_" + str(parameters[key])
classifier.parameterIdStr = idStr
classifier.model = self.connection.getRemotePath(outDir + "/model" + idStr, True)
modelPath = self.connection.getRemotePath(outDir + "/model" + idStr, False)
trainCommand += examples + " " + modelPath
self.connection.addCommand(trainCommand)
# Classify with the trained model (optional)
if classifyExamples != None:
classifier.predictions = self.connection.getRemotePath(outDir + "/predictions" + idStr, True)
predictionsPath = self.connection.getRemotePath(outDir + "/predictions" + idStr, False)
classifyCommand = svmMulticlassDir + "/svm_multiclass_classify " + classifyExamples + " " + modelPath + " " + predictionsPath
self.connection.addCommand(classifyCommand)
# Run the process
jobName = "svm_multiclass_learn" + idStr
logPath = outDir + "/" + jobName
if dummy: # return a classifier that connects to an existing job
self.connection.clearCommands()
classifier._job = self.connection.getJob(jobDir=outDir, jobName=jobName)
else: # submit the job
classifier._job = self.connection.submit(jobDir=outDir, jobName=jobName, stdout=logPath+".stdout")
if finishBeforeReturn:
self.connection.waitForJob(classifier._job)
return classifier