You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using broadcasting to subtract the average face from the training faces, makes the code more readable.
X = trainingFaces - np.tile(avgFace,(trainingFaces.shape[1],1)).T
to:
X = trainingFaces - avgFace.reshape(-1,1).
avgFace.reshape(-1,1) is an array with shape=(32256,1). For conducting the subtraction, numpy would broadcast this array to (322561,n) so the shape become equal to trainingFaces.shape.
cvxpy pakage can manage the L1-norm optimization much faster than SLSQP method in scipy. The process of defining the problem is the same but I add comments to make it more readable.
import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt
# defining parameters same as in the previous example
n = 1000 # dimension of s
p = 200 # number of measurements, dim(y)
Theta = np.random.randn(p,n)
y = np.random.randn(p)
# Creating cvxpy variables(the variable that we want to optimize)
s = cp.Variable(n) # s is the variable that we want to optimize
# Creating constraints (y = Theta * s)
constr = [Theta @ s == y] # y = Theta * s
# creating objective
obj = cp.norm(s,1) # minimize the L1 norm of s
# Solving the problem
prob = cp.Problem(cp.Minimize(obj),constr)
prob.solve()
s_L1_cvxpy = s.value # s_L1_cvxpy is the optimized solution
s_L2 = np.linalg.pinv(Theta) @ y # L2 solution from before
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Using broadcasting to subtract the average face from the training faces, makes the code more readable.
X = trainingFaces - np.tile(avgFace,(trainingFaces.shape[1],1)).T
to:
X = trainingFaces - avgFace.reshape(-1,1).
avgFace.reshape(-1,1) is an array with shape=(32256,1). For conducting the subtraction, numpy would broadcast this array to (322561,n) so the shape become equal to trainingFaces.shape.