def trainCCCPCP1Slack(self, l): c = 1 / self.lbd t = 0 lg = [] lc = [] gt, ct = self.cuttingPlane(l) lg.append(gt) lc.append(ct) gram = None xi = 0 while (t < self.cpmin) or (t <= self.cpmax and vector.dot(self.w, gt) < ct - xi - self.epsilon): print '.', lc_length = len(lc) if t == self.cpmax: print "#max iter" if (gram is None): gram = np.zeros([lc_length, lc_length]) for i in xrange(lc_length): for j in xrange(lc_length): gram[i][j] = gram[j][i] = vector.dot(lg[j], lg[i]) gram += 1e-8 * np.eye(lc_length, lc_length) else: row_num, col_num = gram.shape gram = np.concatenate((gram, np.zeros((1, col_num))), axis=0) gram = np.concatenate((gram, np.zeros((row_num + 1, 1))), axis=1) lc_length = len(lc) for i in range(lc_length): gram[lc_length - 1][i] = gram[i][lc_length - 1] = vector.dot( lg[lc_length - 1], lg[i]) gram[lc_length - 1][lc_length - 1] += 1e-8 alphas = MosekSolver.solveQP(gram, lc, c) xi = (vector.dot(alphas, lc) - np.dot(np.dot(alphas, gram), alphas)) / c self.w *= 0 for i in range(len(alphas)): self.w += alphas[i] * lg[i] t += 1 gt, ct = self.cuttingPlane(l) lg.append(gt) lc.append(ct) print "cutting plane time:%d" % t
def trainCCCPCP1Slack(self, l): c = 1 / self.lbd t = 0 lg = [] lc = [] gt, ct = self.cuttingPlane(l) lg.append(gt) lc.append(ct) gram = None xi = 0 while (t < self.cpmin) or (t <= self.cpmax and vector.dot(self.w, gt) < ct - xi - self.epsilon): print ".", lc_length = len(lc) if t == self.cpmax: print "#max iter" if gram is None: gram = np.zeros([lc_length, lc_length]) for i in xrange(lc_length): for j in xrange(lc_length): gram[i][j] = gram[j][i] = vector.dot(lg[j], lg[i]) gram += 1e-8 * np.eye(lc_length, lc_length) else: row_num, col_num = gram.shape gram = np.concatenate((gram, np.zeros((1, col_num))), axis=0) gram = np.concatenate((gram, np.zeros((row_num + 1, 1))), axis=1) lc_length = len(lc) for i in range(lc_length): gram[lc_length - 1][i] = gram[i][lc_length - 1] = vector.dot(lg[lc_length - 1], lg[i]) gram[lc_length - 1][lc_length - 1] += 1e-8 alphas = MosekSolver.solveQP(gram, lc, c) xi = (vector.dot(alphas, lc) - np.dot(np.dot(alphas, gram), alphas)) / c self.w *= 0 for i in range(len(alphas)): self.w += alphas[i] * lg[i] t += 1 gt, ct = self.cuttingPlane(l) lg.append(gt) lc.append(ct) print "cutting plane time:%d" % t
def primalObj(self, l): obj = self.lbd * vector.dot(self.w, self.w) / 2 loss = self.loss(l) print "lambda*||w||^2= %f\t\tloss= %f" % (obj, loss) obj += loss return obj