Esempio n. 1
0
def contributionForEachExample(taskEachExample):
	k = taskEachExample.curK
	ybar = taskEachExample.curYbar
	splMode = taskEachExample.splMode
	setSelected(taskEachExample, splMode, k, ybar, 1.0)
	(garbageA, garbageB, slackOn) = CommonApp.singleFMVC(taskEachExample.fmvcJob)
	setSelected(taskEachExample, splMode, k, ybar, 0.0)
	(garbageA, garbageB, slackOff) = CommonApp.singleFMVC(taskEachExample.fmvcJob)
	contribTuple = (taskEachExample, (slackOn - slackOff))
	return contribTuple
Esempio n. 2
0
def select(globalSPLVars, w, params):
	logging.debug("Fraction %f" % globalSPLVars.fraction)
	labels, violations= zip(*CommonApp.accessExamples(params,w, findYAndViolation, None))
	cutoffDict = findCutoffs(violations, labels, params.numYLabels,globalSPLVars.fraction)
	trueYAndSelections = CommonApp.accessExamples(params, (w, cutoffDict,globalSPLVars.fraction), updateSelectionSPL, None)	

	for label in range(params.numYLabels):
		total = 0
		totalOn = 0
		for trueYandSelection in trueYAndSelections:
			if trueYandSelection[0]==label:
				totalOn += trueYandSelection[1]
				total+=1
		logging.debug("For label %d totalOn %f and total %f" %( label, totalOn, total))
Esempio n. 3
0
def dumpCurrentLatentVariables(params, lvFile):
	idLVpairs = CommonApp.accessExamples(params, None, grabLatentVariables, None)
	fh = open(lvFile,'w')
	idLVpairs.sort( lambda x,y: int(x[0])-int(y[0]))

	for id,LV in idLVpairs:
		fh.write("%s %d %d %d %d\n" % (id, LV.x_min, LV.x_max, LV.y_min, LV.y_max))

	fh.close()
Esempio n. 4
0
def reoptimizeW(optState,example):
	assert(example.params.splParams.splMode == 'CCCP')
	marginsNew = []
	margins = optState.margins
	FNewtranspose = numpy.asmatrix(numpy.zeros(optState.F.T.shape))
	Ftranspose = optState.F.T
	latents = optState.latents
	for iteration in range(len(margins)):
		ybar, hbar = latents[iteration][example.id]
		contributedMargin = CommonApp.delta(ybar, example.trueY)
		contributedConstraintGiven = CommonApp.padCanonicalPsi(example.psis()[example.h,:].T, example.trueY, example.params)
		contributedConstraintHBar = CommonApp.padCanonicalPsi(example.psis()[hbar,:].T, ybar, example.params)
		FNewtranspose[iteration,:] = (Ftranspose[iteration,:] - (contributedConstraintGiven - contributedConstraintHBar).T)
		marginsNew.append(margins[iteration]-contributedMargin)

	FNew = FNewtranspose.T
	FTF = FNew.T*FNew

	wNew, objective, gap = SSVM.solveDualQPV2(FTF, FNew,marginsNew,example.params, optState.env,optState.task)


	return wNew
Esempio n. 5
0
def main():
	try:
		params = UserInput.getUserInput('train')	
		ExampleLoader.loadExamples(params)
		CommonApp.setExampleCosts(params)
		w = None
		if params.initialModelFile:
			w = CacheObj.loadObject(params.initialModelFile)
		else:
			w = CommonApp.PsiObject(params,False)

		globalSPLVars = SPLSelector.SPLVar()
		
		if params.splParams.splMode != 'CCCP':
			SPLSelector.setupSPL(params)
	
		w = LSSVM.optimize(w, globalSPLVars, params)
		CacheObj.cacheObject(params.modelFile,w)
		Performance.printStrongAndWeakTrainError(params, w)
	except Exception, e :
		import traceback
		traceback.print_exc(file=sys.stdout)
Esempio n. 6
0
def impute(optState, params):
	logging.debug("Imputing H")
	CommonApp.accessExamples(params, optState, imputeSingle, None)
Esempio n. 7
0
def computeObjective(w, params):
	objective = 0.5 * (w.T * w)[0,0]
	(margin, constraint,lvs) = CommonApp.findCuttingPlane(w, params)
	objective += params.C*(margin - ((w.T * constraint)[0,0]))
	return (objective, margin, constraint,lvs)
Esempio n. 8
0
def setupSPL(params):
	CommonApp.accessExamples(params, None, setupSPLEachExample, None)