Hi everyone,
I’m trying to train a LSTM model for forecasting a time series.
At this stage training happens, but with a really bad performance: loss function values exhibit really minimal changes. I added a picture that is quite explicative of the problem.
I’m sure something is wrong but I can not tell what.
How can I change it in order to make it work properly?
using Random, Statistics
using CSV, DataFrames, MLDatasets
using ComponentArrays, DataInterpolations, Lux, MLUtils
using Optimization, Zygote
using Optimisers, OptimizationOptimisers
using Optim, OptimizationOptimJL
using Plots
function get_data()
data_fn = raw"File csv/Tinterna uncontrolled.csv"
data_df = CSV.read(data_fn, DataFrame; header = false)
train_D = convert.(Float32, data_df[1:4320, 1])
test_D = convert.(Float32, data_df[10000:11800, 1])
train_D, test_D
end
function get_model()
Lux.LSTMCell(1 => 1)
# Lux.Dense(1 => 1, tanh)
end
function fit_it_1(model, train_X, train_Y)
rng = Random.default_rng()
ps, st = Lux.setup(rng, model)
ps = ComponentArray(ps)
# opt = NewtonTrustRegion()
opt = Lion()
optfunc = OptimizationFunction((p, theta) -> loss(model, p, st, train_X, train_Y), AutoZygote())
optprob = OptimizationProblem(optfunc, ps)
res = solve(optprob, opt; callback, maxiters = 100)
ps .= res.u
ps, st
end
function fit_it_2(model, train_X, train_Y)
rng = Random.default_rng()
ps, st = Lux.setup(rng, model)
ps = ComponentArray(ps)
learningrate = [0.08, 0.05, 0.01, 0.007, 0.005, 0.001]
for lr in learningrate
opt = Optimisers.Adam(lr)
opt_state = Optimisers.setup(opt, ps)
for i in 1:30
gs = gradient(p -> loss(model, p, st, train_X, train_Y), ps)[1]
opt_state, ps = Optimisers.update!(opt_state, ps, gs)
@show loss(model, ps, st, train_X, train_Y)
end
end
ps, st
end
function evaluate_it(model, ps, st, test_X, test_Y) end
function plot_it(model, ps, st, test_X, test_Y, test_mean, test_std)
pred, st = model(test_X', ps, st)
pred = pred[1]
pred = pred[1, :]
pred = (pred .* test_std) .+ test_mean
plt1 = plot(pred; label = "prediction")
plt1 = plot!(plt1, test_Y; ylabel = "Temperatura [°C]", label = "data")
plt1
end
function loss(model, ps, st, train_X, train_Y)
pred, st = model(train_X', ps, st)
pred = pred[1]
pred = pred[1, :]
mean(abs2, train_Y - pred)
end
function callback(ps, l)
@show l
false
end
function main()
train_D, test_D = get_data()
train_mean, train_std = mean(train_D), std(train_D)
test_mean, test_std = mean(test_D), std(test_D)
train_data = (train_D .- train_mean) ./ train_std
train_data = vcat(train_data[2:end], train_data[1])
test_data = (test_D .- test_mean) ./ test_std
test_data = vcat(test_data[2:end], test_data[1])
model = get_model()
ps, st = fit_it_1(model, train_data, train_D)
evaluate_it(model, ps, st, test_data, test_D)
plt1 = plot_it(model, ps, st, train_data, train_D, train_mean, train_std)
savefig(plt1, "fig-train.png")
plt2 = plot_it(model, ps, st, test_data, test_D, test_mean, test_std)
savefig(plt2, "fig-test.png")
end
main()
here is a link for the csv file :