Commit 2208738b authored by William F Godoy's avatar William F Godoy
Browse files

Adding Flux capability

parent 3a66012a
import Flux
import Statistics
using Plots;
pyplot();
using LaTeXStrings
function FluxTrain(nepocs::Int32, X::Array{Float64,1}, Y::Array{Float64,1})
W = [500.0]
b = [10000.0]
model = Flux.Dense(W, b)
parameters = Flux.params(model)
println("Initial parameter: ", parameters)
# set up data
Xd = reduce(hcat, X)
Yd = reduce(hcat, Y)
data = [(Xd, Yd)]
println("Data", data)
# optimizer = Flux.Descent(1)
# optimizer = Flux.ADAM()
optimizer = Flux.ADAM(1, (0.99, 0.999))
loss(x, y) = Statistics.mean((model(x) .- y) .^ 2)
Yd_0 = model(Xd)
println("Initial solution: ", Yd_0)
plot(X, Y, st = :scatter, label = L"y")
Yd_nE = model(Xd)
for iter = 1:nepocs
Flux.train!(loss, parameters, data, optimizer)
end
println("Module: ", model)
println("Parameters_nE: ", parameters)
println("Final solution: ", Yd_nE)
Yd_nE = model(Xd)
plot!(Xd', Yd_0', lc = :green, label = L"y_0")
plot!(Xd', Yd_nE', lc = :red, label = L"y_{n_\mathrm{E}}")
plot!(xlabel = L"x", ylabel = L"y", title = "Data for linear model")
gui()
end
function test()
x::Array{Float64,1} = [256, 4096, 512, 512, 8192]
y::Array{Float64,1} = [435867, 2959963, 1475489, 1485569, 4518030]
nepocs::Int32 = 100000
FluxTrain(nepocs, x, y)
end
test()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment