Skip to content
Snippets Groups Projects
Commit c4988405 authored by J. Gergaud's avatar J. Gergaud
Browse files

foo

parent 2cfefa0e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Neural Network
Complete the descent with constant rate function
%% Cell type:code id: tags:
```
function my_descent(f,x0::Vector{<:Real};η=0.1,AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)# to complete
return xₖ, flag, fₖ, ∇fₖ, k c
end
```
%% Cell type:markdown id: tags:
And apply this code for solving the following problem
$$(P)\left\{\begin{array}{l}
Min\;f(\beta) = \frac{1}{n}\|X\beta - y\|^2\\
\beta\in\R^2
\end{array}\right.
$$
with
$$X = \begin{pmatrix}
1 & x_1\\
\vdots & \vdots\\
1 & x_5
\end{pmatrix}
\;\;\textrm{and}\;\;
y = \begin{pmatrix}
y_1\\
\vdots\\
y_5
\end{pmatrix}
$$
You 'll print the first 7 iterations for the initual guess
$x0= (n/2)(9,1)$.
%% Cell type:code id: tags:
```
using Plots
# n = 5
x1 = 1.; x2 = sqrt(5*9/2-x1^2);
x = [-x2,-x1,0.,x1,x2]
n = length(x)
a₁ = 1; a₀ = 2
y = a₁*x .+ a₀ # model
```
%% Cell type:markdown id: tags:
##
%% Cell type:code id: tags:
```
function descent_backtrac(f,x0::Vector{<:Real};AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)
# to complete
return xₖ, flag, fₖ, ∇fₖ, k c
end
```
%% Cell type:code id: tags:
```
using Plots
x = range(-10*(n/2)-xsol[1],stop=10*(n/2)-xsol[1],length=100)
y = range(-9*(n/2)-xsol[2],stop=9*(n/2)-xsol[2],length=100)
f_contour(x,y) = f([x,y])
z = @. f_contour(x', y)
nb_levels = 7
x0 = (n/2)*[9,1]
```
%% Cell type:markdown id: tags:
# Steepest descent for a quadratic function
$f(x) = x^Ax + b^Tx + c$, where $A$ is symetric and positive-definite
Complete the steepest_descent_quad function
%% Cell type:code id: tags:
```
function steepest_descent_quad(A::Matrix{<:Real},b::Vector{<:Real},c::Real,x0::Vector{<:Real};AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)
# to complete
return xₖ, flag, fₖ, ∇fₖ, k c
end
```
%% Cell type:code id: tags:
```
using Plots
# n = 5
x1 = 1.; x2 = sqrt(5*9/2-x1^2);
x = [-x2,-x1,0.,x1,x2]
n = length(x)
X = [ones(n) x]
a₁ = 1; a₀ = 2
y = a₁*x .+ a₀ # model
A = (1/n)*X'*X # A=[1 0 ; 0 9]
println("A = ", A)
b = -(2/n)*X'*y
c = (1/n)*y'*y
println("b = ", b)
println("n= ",n)
f(x) = x'*A*x + b'*x + c
xsol = -(A' + A)\b
xx = range(-10*(n/2)-xsol[1],stop=10*(n/2)-xsol[1],length=100)
yy = range(-9*(n/2)-xsol[2],stop=9*(n/2)-xsol[2],length=100)
f_contour(x,y) = f([x,y])
z = @. f_contour(xx', yy)
nb_levels = 7
x0 = (n/2)*[9,1]
xₖ = x0
Xsol = zeros(nb_levels+1,2)
Xsol[1,:] = x0
p1 = plot()
for nbit in 1:nb_levels
xsol, flag, fsol, ∇f_xsol , nb_iter = steepest_descent_quad(A,b,c,x0,nbit_max=nbit)
plot!(p1,[xₖ[1],xsol[1]],[xₖ[2],xsol[2]],arrow=true)
xₖ = xsol
Xsol[nbit+1,:] = xsol
end
levels = [f(Xsol[k,:]) for k in nb_levels+1:-1:1]
contour!(p1,xx,yy,z,levels=levels,cbar=false,color=:turbo)
xsol, flag, fsol, ∇f_xsol , nb_iter = algo_Newton(f,x0)
println()
println("Result with Newton's algorithm : ")
println("xsol = ", xsol)
println("flag = ", flag)
println("fsol = ", fsol)
println("∇f_xsol = ", ∇f_xsol)
println("nb_iter = ", nb_iter)
scatter!(p1,[xsol[1]],[xsol[2]])
plot!(p1,[x0[1],xsol[1]],[x0[2],xsol[2]],arrow=true)
plot(p1,legend=false)
```
%% Cell type:markdown id: tags:
##
%% Cell type:code id: tags:
```
function descent_backtrac(f,x0::Vector{<:Real};AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)
# to complete
return xₖ, flag, fₖ, ∇fₖ, k c
end
```
%% Cell type:code id: tags:
```
using Plots
x = range(-10*(n/2)-xsol[1],stop=10*(n/2)-xsol[1],length=100)
y = range(-9*(n/2)-xsol[2],stop=9*(n/2)-xsol[2],length=100)
f_contour(x,y) = f([x,y])
z = @. f_contour(x', y)
nb_levels = 7
x0 = (n/2)*[9,1]
xₖ = x0
p1 = plot()
for nbit in 1:nb_levels
xsol, flag, fsol, ∇f_xsol , nb_iter = descent_backtrac(f,x0,nbit_max=nbit)
#xsol, flag, fsol, ∇f_xsol , nb_iter = my_descent(f,x0,nbit_max=nbit)
println("xsol = ", xsol)
println("flag = ", flag)
println("fsol = ", fsol)
println("∇f_xsol = ", ∇f_xsol)
println("nb_iter = ", nb_iter)
plot!(p1,[xₖ[1],xsol[1]],[xₖ[2],xsol[2]],arrow=true)
xₖ = xsol
end
levels = [f(Xsol[k,:]) for k in nb_levels+1:-1:1]
contour!(p1,xx,yy,z,levels=levels,cbar=false,color=:turbo)
plot(p1,legend=false)
```
File added
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment