-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLosses.jl
202 lines (186 loc) · 8 KB
/
Losses.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using LinearAlgebra
include("FEM1D.jl")
include("containers.jl")
include("Iterations.jl")
function ρ_error(dimension::Integer, k::Integer, iteration::Function, batch_size::Integer)
x = randn((dimension, batch_size))
y = x
for i = 1:k
y = iteration(y)
end
ρ = 0
n_y, n_x = 0, 0
for j = 1:batch_size
ρ += (norm(y[:, j])/norm(x[:, j]))^(1/k)
end
ρ = ρ/batch_size
end
function ρ_F(dimension::Integer, k::Integer, iteration::Function, batch_size::Integer)
x = rand((-1, 1), (dimension, batch_size))
for l = 1:k
x = iteration(x)
end
ρ = (norm(x)^2/batch_size)^(1/(2*k))
end
function prec_loss_function(parameters::AbstractArray{<:Real, 1}, prec::preconditioner, params::optimization_parameters)
M = assemble_matrix(parameters, prec)
iteration(x) = Richardson(x, prec.A, x -> M*x, parameters[end]; prec=prec.symmetry)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function spectral_loss_function(parameters::AbstractArray{<:Real, 1}, prec::preconditioner, params::optimization_parameters)
M = assemble_matrix(parameters, prec)
iteration(x) = Richardson(x, prec.A, x -> M*x, 1/spectral_radius(parameters, prec, params); prec=prec.symmetry)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function scalar_loss_function(parameters::AbstractArray{<:Real, 1}, M::SparseMatrixCSC{Float64,Int64}, prec::preconditioner, params::optimization_parameters)
iteration(x) = Richardson(x, prec.A, x -> M*x, parameters[end]; prec=prec.symmetry)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function ρ_error_T(dimension::Tuple{Integer, Integer}, k::Integer, iteration::Function, batch_size::Integer)
n_y, n_x = 0, 0
for j = 1:batch_size
x = randn(dimension)
n_x += norm(x)
y = x
for i = 1:k
y = iteration(y)
end
n_y += norm(y)
end
ρ = (n_y/n_x)^(1/k)
end
function ρ_F_T(dimension::Tuple{Integer, Integer}, k::Integer, iteration::Function, batch_size::Integer)
n_x = 0
for j = 1:batch_size
x = rand((-1, 1), dimension)
for l = 1:k
x = iteration(x)
end
n_x += norm(x)^2
end
ρ = (n_x/batch_size)^(1/(2*k))
end
function prec_loss_function(parameters::AbstractArray{<:Real, 1}, prec::T_preconditioner, params::optimization_parameters)
N = 2^prec.L+1
DOF_x, DOF_y = N - 2 + sum(prec.BCs[1:2] .== "N"), N - 2 + sum(prec.BCs[3:4] .== "N")
dimension = (DOF_y, DOF_x)
iteration(x) = Richardson_T(x, prec.C, x -> apply_T_preconditioner(x, parameters, prec), parameters[end]; prec=prec.symmetry)
if params.error == "Frobenius"
loss = ρ_F_T(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error_T(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function spectral_loss_function(parameters::AbstractArray{<:Real, 1}, prec::T_preconditioner, params::optimization_parameters)
N = 2^prec.L+1
DOF_x, DOF_y = N - 2 + sum(prec.BCs[1:2] .== "N"), N - 2 + sum(prec.BCs[3:4] .== "N")
dimension = (DOF_y, DOF_x)
iteration(x) = Richardson_T(x, prec.C, x -> apply_T_preconditioner(x, parameters, prec), 1/spectral_radius(parameters, prec, params); prec=prec.symmetry)
if params.error == "Frobenius"
loss = ρ_F_T(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error_T(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function Frobenius_loss_function(parameters::AbstractArray{<:Real, 1}, prec::T_preconditioner, params::optimization_parameters)
N = 2^prec.L+1
DOF_x, DOF_y = N - 2 + sum(prec.BCs[1:2] .== "N"), N - 2 + sum(prec.BCs[3:4] .== "N")
dimension = (DOF_y, DOF_x)
iteration(x) = x
if prec.symmetry == "symmetric"
iteration(x) = x - apply_T_preconditioner(reshape(prec.A*reshape(apply_T_preconditioner(x, parameters, prec), (DOF_y*DOF_x, )), dimension), parameters, prec)
else
iteration(x) = x - apply_T_preconditioner(reshape(prec.A*reshape(x, (DOF_y*DOF_x, )), dimension), parameters, prec)
end
loss = ρ_F_T(dimension, 1, iteration, params.k_batch)
end
function Frobenius_loss_function(parameters::AbstractArray{<:Real, 1}, prec::preconditioner, params::optimization_parameters)
M = assemble_matrix(parameters, prec)
iteration(x) = x
if prec.symmetry == "left"
iteration(x) = x - M*(prec.A*x)
else
iteration(x) = x - M*(prec.A*(M*x))
end
loss = ρ_F(size(prec.A)[1], 1, iteration, params.k_batch)
end
function scalar_loss_function(parameters::AbstractArray{<:Real, 1}, M::SparseMatrixCSC{Float64,Int64}, prec::T_preconditioner, params::optimization_parameters)
iteration(x) = Richardson(x, prec.A, x -> M*x, parameters[end]; prec=prec.symmetry)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, iteration, params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, iteration, params.k_batch)
end
return loss
end
function spectral_radius(parameters::AbstractArray{<:Real, 1}, prec::preconditioner, params::optimization_parameters)
function iteration(x::AbstractArray{<:Real, 2}, M::SparseMatrixCSC{<:Real, <:Integer}, A::SparseMatrixCSC{<:Real, <:Integer}, symmetry::String)
if symmetry == "symmetric"
return M*A*M*x
elseif symmetry == "left"
return M*A*x
end
end
M = assemble_matrix(parameters, prec)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, x -> iteration(x, M, prec.A, prec.symmetry), params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, x -> iteration(x, M, prec.A, prec.symmetry), params.k_batch)
end
return loss
end
function spectral_radius(parameters::AbstractArray{<:Real, 1}, prec::T_preconditioner, params::optimization_parameters)
function iteration(x::AbstractArray{<:Real, 2}, M::SparseMatrixCSC{<:Real, <:Integer}, A::SparseMatrixCSC{<:Real, <:Integer}, symmetry::String)
if symmetry == "symmetric"
return M*A*M*x
elseif symmetry == "left"
return M*A*x
end
end
M = assemble_matrix(parameters, prec)
dimension = size(prec.A)[1]
if params.error == "Frobenius"
loss = ρ_F(dimension, params.k_sweeps, x -> iteration(x, M, prec.A, prec.symmetry), params.k_batch)
elseif params.error == "Spectral"
loss = ρ_error(dimension, params.k_sweeps, x -> iteration(x, M, prec.A, prec.symmetry), params.k_batch)
end
return loss
end
function test_ρ_1(k::Integer, N::Integer, L::Integer)
A, b, x = BVP_DD(L, x -> one(x), x -> zero(x), 1, 1, x -> x)
ρ = maximum(abs.(eigvals(Array(A))))
ρ_err = ρ_error(2^L-1, k, x -> A*x)
ρ_Frob = ρ_F(2^L-1, k, N, x -> A*x)
println("Spectral radius ", ρ)
println("Estimation with error ", ρ_err)
println("Estimation with Frobenius ", ρ_Frob)
end
function test_ρ_2(k::Integer, L::Integer)
A, b, x = BVP_DD(L, x -> 1 + x^2, x -> exp(x), 1, 1, x -> x)
ρ = maximum(abs.(eigvals(Array(A))))
ρ_estimated = ρ_error(2^L-1, k, x -> A*x)
println("Spectral radius ", ρ)
println("Estimation ", ρ_estimated)
end