Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming_Assignments
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
asilador
Programming_Assignments
Commits
3694663a
Commit
3694663a
authored
8 years ago
by
asilador
Browse files
Options
Downloads
Patches
Plain Diff
Cleaned up code to have run(epsilon) work in interactive python
parent
7615f6e6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment 1/assignment1.py
+15
-24
15 additions, 24 deletions
Assignment 1/assignment1.py
with
15 additions
and
24 deletions
Assignment 1/assignment1.py
+
15
−
24
View file @
3694663a
import
numpy
as
np
import
numpy
as
np
# Initialize iteration counter
# Import text files
# Import text files
Q
=
np
.
asmatrix
(
np
.
loadtxt
(
'
Q.txt
'
))
Q
=
np
.
asmatrix
(
np
.
loadtxt
(
'
Q.txt
'
))
b
=
np
.
asmatrix
(
np
.
loadtxt
(
'
b.txt
'
))
b
=
np
.
asmatrix
(
np
.
loadtxt
(
'
b.txt
'
))
c
=
np
.
asmatrix
(
np
.
loadtxt
(
'
c.txt
'
))
c
=
np
.
asmatrix
(
np
.
loadtxt
(
'
c.txt
'
))
b
=
np
.
transpose
(
b
)
#make b a column vector
b
=
np
.
transpose
(
b
)
#make b a column vector
D
=
np
.
asmatrix
(
np
.
ones
(
np
.
size
(
b
)))
m
=
1
m
=
1
# Make a guess for x vector
#x = np.asmatrix(np.zeros(np.size(b)))
#x = np.transpose(x) #make column vector
#x=np.matrix(np.random.rand(5,1))
x
=
np
.
transpose
(
np
.
asmatrix
(
np
.
ones
(
np
.
size
(
b
)))
*
1
)
alpha0
=
1.0
count
=
0
# Initialize iteration counter
global
x
global
count
# Define f(x)
# Define f(x)
def
f
(
Q
,
b
,
c
,
x
):
def
f
(
Q
,
b
,
c
,
x
):
return
np
.
transpose
(
x
)
*
Q
*
x
+
np
.
transpose
(
b
)
*
x
+
c
return
np
.
transpose
(
x
)
*
Q
*
x
+
np
.
transpose
(
b
)
*
x
+
c
...
@@ -26,7 +20,7 @@ def gradf(Q,b,x):
...
@@ -26,7 +20,7 @@ def gradf(Q,b,x):
return
2
*
Q
*
x
+
b
return
2
*
Q
*
x
+
b
# Define algorithm for Armijos rule
# Define algorithm for Armijos rule
def
armijo
(
alpha0
,
Q
,
b
,
c
,
D
,
m
):
def
armijo
(
alpha0
,
Q
,
b
,
c
,
D
,
m
,
x
):
alpha
=
alpha0
alpha
=
alpha0
#print('alpha is ', alpha)
#print('alpha is ', alpha)
...
@@ -41,13 +35,6 @@ def armijo(alpha0,Q,b,c,D,m):
...
@@ -41,13 +35,6 @@ def armijo(alpha0,Q,b,c,D,m):
return
alpha
return
alpha
def
xval
():
return
x
def
countval
():
return
count
# Begin Gradient Descent Algorithm
# Begin Gradient Descent Algorithm
def
grad_opt
(
epsilon
,
x
,
count
,
alpha
):
def
grad_opt
(
epsilon
,
x
,
count
,
alpha
):
...
@@ -55,7 +42,7 @@ def grad_opt(epsilon,x,count,alpha):
...
@@ -55,7 +42,7 @@ def grad_opt(epsilon,x,count,alpha):
while
np
.
linalg
.
norm
(
gradf
(
Q
,
b
,
x
))
>=
epsilon
:
while
np
.
linalg
.
norm
(
gradf
(
Q
,
b
,
x
))
>=
epsilon
:
D
=
-
1
*
np
.
transpose
(
gradf
(
Q
,
b
,
x
))
/
np
.
linalg
.
norm
(
gradf
(
Q
,
b
,
x
))
D
=
-
1
*
np
.
transpose
(
gradf
(
Q
,
b
,
x
))
/
np
.
linalg
.
norm
(
gradf
(
Q
,
b
,
x
))
#print('D is ', D)
#print('D is ', D)
alpha
=
armijo
(
alpha
,
Q
,
b
,
c
,
D
,
m
)
alpha
=
armijo
(
alpha
,
Q
,
b
,
c
,
D
,
m
,
x
)
#print('alpha0 is ', alpha)
#print('alpha0 is ', alpha)
count
+=
1
count
+=
1
if
count
%
1000
==
0
:
if
count
%
1000
==
0
:
...
@@ -71,11 +58,15 @@ def grad_opt(epsilon,x,count,alpha):
...
@@ -71,11 +58,15 @@ def grad_opt(epsilon,x,count,alpha):
print
'
epsilon is
'
,
epsilon
print
'
epsilon is
'
,
epsilon
return
0
return
0
def
run
(
epsilon
):
def
run
(
epsilon
):
xstart
=
xval
()
# Make a guess for x vector
countstart
=
countval
()
#x = np.asmatrix(np.zeros(np.size(b)))
grad_opt
(
epsilon
,
xstart
,
countstart
,
alpha0
)
#x = np.transpose(x) #make column vector
#x=np.matrix(np.random.rand(5,1))
x
=
np
.
transpose
(
np
.
asmatrix
(
np
.
ones
(
np
.
size
(
b
)))
*
1
)
count
=
0
alpha0
=
1.0
grad_opt
(
epsilon
,
x
,
count
,
alpha0
)
return
0
return
0
run
(
0.1
)
run
(
0.00001
)
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment