Exemplo n.º 1
0
def run_function(f):
    prob = cp.Problem(cp.Minimize(f()))
    obj_val0 = prob.solve()

    status, obj_val1 = ep.solve(prob)
    tol = 1e-2
    assert_equal(status, OPTIMAL)
    assert_less(abs(obj_val1 - obj_val0) / (1 + abs(obj_val0)), tol)
Exemplo n.º 2
0
def run_function(f):
    prob = cp.Problem(cp.Minimize(f()))
    obj_val0 = prob.solve()

    status, obj_val1 = ep.solve(prob)
    tol = 1e-2
    assert_equal(status, OPTIMAL)
    assert_less(abs(obj_val1-obj_val0)/(1+abs(obj_val0)), tol)
Exemplo n.º 3
0
V = pca(mnist["X"], 50)
X = mnist["X"].dot(V)
sigma = median_dist(X)


y = mnist["Y"].ravel()
ytest = mnist["Ytest"].ravel()

# Random features
n = 4000
W = np.random.randn(X.shape[1], n) / sigma
b = np.random.uniform(0, 2 * np.pi, n)
X = np.cos(X.dot(W) + b)
Xtest = np.cos(mnist["Xtest"].dot(V).dot(W) + b)

# Parameters
m, n = X.shape
k = 10
Theta = cp.Variable(n, k)
lam = 10

# Form problem with CVXPY and solve with Epsilon
f = ep.multiclass_hinge_loss(Theta, X, y) + lam * cp.sum_squares(Theta)
prob = cp.Problem(cp.Minimize(f))
ep.solve(prob, verbose=True)

# Get solution and compute train/test error
Theta0 = np.array(Theta.value)
print "Train error:", error(np.argmax(X.dot(Theta0), axis=1), y)
print "Test error:", error(np.argmax(Xtest.dot(Theta0), axis=1), ytest)