-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHImputation.py
64 lines (52 loc) · 1.9 KB
/
HImputation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import copy
import logging
import numpy
from numpy import linalg
import random
from scipy import sparse
import sys
from imageImplementation import CommonApp
import SSVM
import Timer
import utils
def updateRow(lilmatrix, lilrow, index):
lilmatrix.rows[index] = lilrow.rows[0]
lilmatrix.data[index] = lilrow.data[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
def fractionToUCCCP(iteration):
if iteration<4:
return 1.0 / (iteration + .0001)
else:
return 0
def imputeSingle(optState, example):
if example.params.UCCCP:
if random.random() < fractionToUCCCP(optState.outerIter):
logging.debug("UCCCPing an example")
wNew = reoptimizeW(optState,example)
else:
wNew = optState.w
else:
wNew = optState.w
(bestH, score, psivect) = example.highestScoringLV(wNew, example.trueY)
(bestHOldW, score, psivect) = example.highestScoringLV(optState.w, example.trueY)
example.h = bestH
def impute(optState, params):
logging.debug("Imputing H")
CommonApp.accessExamples(params, optState, imputeSingle, None)