-
-
Notifications
You must be signed in to change notification settings - Fork 106
NFQ #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
NFQ #897
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
5e6f09d
NFQ before refactor
CasBex c423048
NFQ after refactor
CasBex c88559e
Move to dqns
CasBex 1560ad1
Refactor
CasBex 02ab01b
Add NFQ to RLZoo
CasBex 093f1f4
Set up experiment
CasBex 7402cb2
Merge remote-tracking branch 'origin/main' into NFQ
CasBex c1e49da
Update algorithm for refactor
CasBex 0edd287
rng and loss type
HenriDeh 8461c15
remove duplicate
HenriDeh b89f67e
dispatch on trajectory
HenriDeh 19e0a97
optimise is dummy by default
HenriDeh 98444e4
optimise! is dispatched on traj and loops it
HenriDeh 6be2450
Fix precompilation warnings
CasBex 2ed5ffb
Avoid running post episode optimise! multiple times
CasBex da384b2
Tune experiment
CasBex b30a08e
Merge branch 'main' into NFQ
CasBex 5ab7a1c
Remove commented code
CasBex afc21b6
Drop gpu call
CasBex 033dcdf
Use `sample` to get batch from trajectory
CasBex 31b55b5
optimise! for AbstractLearner
CasBex 8605dbf
Merge remote-tracking branch 'origin/main' into NFQ
CasBex b53c96b
NFQ optimise! calls at the correct time
CasBex e949807
Merge branch 'main' into NFQ
HenriDeh f77e198
Remove superfluous function due to main merge
CasBex 66ea89b
Anonymous loop variable
CasBex 37be2a6
Update NFQ docs
CasBex c43f37a
Update julia_words.txt
HenriDeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/ReinforcementLearningZoo/src/algorithms/offline_rl/NFQ.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """ | ||
| NFQ{A<:AbstractApproximator, F, R} <: AbstractLearner | ||
| NFQ(approximator::A, num_iterations::Integer epochs::Integer, loss_function::F, batch_size::Integer, rng::R, γ::Float32) where {A<:AbstractApproximator, F, R} | ||
| Neural Fitted Q-iteration as implemented in [1] | ||
|
|
||
| # Keyword arguments | ||
| - `approximator::AbstractApproximator` neural network | ||
| - `num_iterations::Integer` number of value iteration iterations in FQI loop (i.e. the outer loop) | ||
| - `epochs` number of epochs to train neural network per iteration | ||
| - `loss_function::F` loss function of the NN | ||
| - `sampler::BatchSampler{SARTS}` data sampler | ||
| - `rng::R` random number generator | ||
| - `γ::Float32` discount rate | ||
|
|
||
| # References | ||
| [1] Riedmiller, M. (2005). Neural Fitted Q Iteration – First Experiences with a Data Efficient Neural Reinforcement Learning Method. In: Gama, J., Camacho, R., Brazdil, P.B., Jorge, A.M., Torgo, L. (eds) Machine Learning: ECML 2005. ECML 2005. Lecture Notes in Computer Science(), vol 3720. Springer, Berlin, Heidelberg. https://doi.org/10.1007/11564096_32 | ||
| """ | ||
| Base.@kwdef struct NFQ{A<:NeuralNetworkApproximator, F, R} <: AbstractLearner | ||
| approximator::A | ||
| num_iterations::Integer = 20 | ||
| epochs::Integer = 100 | ||
| loss_function::F = mse | ||
| rng::R = Random.GLOBAL_RNG | ||
| γ::Float32 = 0.9f0 | ||
| end | ||
|
|
||
| function NFQ(; | ||
| approximator::A, | ||
| num_iterations::Integer = 20, | ||
| epochs::Integer = 1000, | ||
| loss_function::F = mse, | ||
| rng=Random.GLOBAL_RNG, | ||
| γ::Float32 = 0.9f0, | ||
| ) where {A<:NeuralNetworkApproximator, F} | ||
| NFQ(approximator, num_iterations, epochs, loss_function, rng, γ) | ||
| end | ||
|
|
||
| # Copied from BasicDQN but sure whether it's appropriate | ||
| Flux.functor(x::NFQ) = (Q = x.approximator,), y -> begin | ||
| x = @set x.approximator = y.Q | ||
| x | ||
| end | ||
|
|
||
| function RLBase.plan!(learner::NFQ, env::AbstractEnv) | ||
| as = action_space(env) | ||
| return vcat(repeat(state(env), inner=(1, length(as))), transpose(as)) |> x -> send_to_device(device(learner.approximator), x) |> learner.approximator |> send_to_host |> vec | ||
| end | ||
|
|
||
| # Avoid optimisation in the middle of an episode | ||
| function RLBase.optimise!(::NFQ, ::NamedTuple) end | ||
CasBex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Instead do optimisation at the end of an episode | ||
| function Base.push!(agent::Agent{<:QBasedPolicy{<:NFQ}}, ::PostEpisodeStage, env::AbstractEnv) | ||
| for batch in agent.trajectory | ||
| _optimise!(agent.policy.learner, batch, env) | ||
| end | ||
| end | ||
|
|
||
| function _optimise!(learner::NFQ, batch::NamedTuple, env::AbstractEnv) | ||
| Q = learner.approximator | ||
| γ = learner.γ | ||
| loss_func = learner.loss_function | ||
|
|
||
| as = action_space(env) | ||
| las = length(as) | ||
|
|
||
|
|
||
| (s, a, r, ss) = batch[[:state, :action, :reward, :next_state]] | ||
| a = Float32.(a) | ||
| s, a, r, ss = map(x->send_to_device(device(Q), x), (s, a, r, ss)) | ||
| for i = 1:learner.num_iterations | ||
| # Make an input x samples x |action space| array -- Q --> samples x |action space| -- max --> samples | ||
| G = r .+ γ .* (cat(repeat(ss, inner=(1, 1, las)), reshape(repeat(as, outer=(1, size(ss, 2))), (1, size(ss, 2), las)), dims=1) |> Q |> x -> maximum(x, dims=3) |> vec) | ||
| for e = 1:learner.epochs | ||
| Flux.train!((x, y) -> loss_func(Q(x), y), params(Q.model), [(vcat(s, transpose(a)), transpose(G))], Q.optimizer) | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.