Last active
June 21, 2019 11:55
-
-
Save prerakmody/814ecb738dc823cae680dfdd4da52524 to your computer and use it in GitHub Desktop.
Optimization Techniques
This file contains 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
from autograd import grad | |
import autograd.numpy as anp | |
def objective(): | |
return | |
if __name__ == "__main__": | |
W = np.random.normal(0, 1,X_train.shape[1]) | |
gradient = grad(objective) | |
W -= gradient(W, X_11, X_12, X_21, X_22) * LR |
This file contains 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
def scalar1(x): | |
return np.sin(x)*np.exp(-0.1*(x-0.6)**2) | |
if __name__ == "__main__": | |
result = optimize.minimize_scalar(scalar1) | |
print (result) | |
def objective(x): | |
x1 = x[0] | |
x2 = x[1] | |
x3 = x[2] | |
x4 = x[3] | |
return x1*x4*(x1+x2+x3)+x3 | |
def constraint1(x): | |
return x[0]*x[1]*x[2]*x[3] - 25.0 | |
def constraint2(x): | |
sum_sq = 40.0 | |
for i in range(4): | |
sum_sq = sum_sq - x[i]**2 | |
return sum_sq | |
if __name__ == "__main__": | |
# MULTI-VARIATE OPTIMIZATION | |
x0 = [1,5,5,1] | |
print (' - x0_obj : ', objective(x0)) | |
b = (1.0, 5.0) | |
bnds = (b,b,b,b) | |
con1 = {'type' : 'ineq', 'fun':constraint1} | |
con2 = {'type' : 'eq' , 'fun':constraint2} | |
cons = [con1, con2] | |
sol = optimize.minimize(objective, x0, method='SLSQP', \ | |
bounds=bnds, constraints=cons) | |
print (sol) | |
print (constraint1(sol.x)) | |
print (constraint2(sol.x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment