|
| 1 | +export MACLearner |
| 2 | + |
| 3 | +using Flux |
| 4 | + |
| 5 | +""" |
| 6 | + MACLearner(;kwargs...) |
| 7 | + Keyword arguments |
| 8 | +- `approximator`::[`ActorCritic`](@ref) |
| 9 | +- `γ::Float32`, reward discount rate |
| 10 | +- `bootstrap::bool`, if false then Q function is approximated using monte carlo returns. |
| 11 | +""" |
| 12 | + |
| 13 | +Base.@kwdef mutable struct MACLearner{A<:ActorCritic} <:AbstractLearner |
| 14 | + approximator::A |
| 15 | + γ::Float32 |
| 16 | + max_grad_norm::Union{Nothing,Float32} = nothing |
| 17 | + norm::Float32 = 0.0f0 |
| 18 | + actor_loss::Float32 = 0.0f0 |
| 19 | + critic_loss::Float32 = 0.0f0 |
| 20 | + loss::Float32 = 0.0f0 |
| 21 | + bootstrap::Bool = true |
| 22 | +end |
| 23 | + |
| 24 | +function (learner::MACLearner)(env::MultiThreadEnv) |
| 25 | + learner.approximator.actor(send_to_device( |
| 26 | + device(learner.approximator), |
| 27 | + get_state(env), |
| 28 | + )) |> send_to_host |
| 29 | +end |
| 30 | + |
| 31 | +function (learner::MACLearner)(env) |
| 32 | + s = get_state(env) |
| 33 | + s = Flux.unsqueeze(s, ndims(s) + 1) |
| 34 | + s = send_to_device(device(learner.approximator), s) |
| 35 | + learner.approximator.actor(s) |> vec |> send_to_host |
| 36 | +end |
| 37 | + |
| 38 | +function RLBase.update!(learner::MACLearner, t::AbstractTrajectory) |
| 39 | + isfull(t) || return |
| 40 | + |
| 41 | + states = t[:state] |
| 42 | + actions = t[:action] |
| 43 | + rewards = t[:reward] |
| 44 | + terminals = t[:terminal] |
| 45 | + |
| 46 | + AC = learner.approximator |
| 47 | + γ = learner.γ |
| 48 | + D = device(AC) |
| 49 | + |
| 50 | + states = send_to_device(D, states) |
| 51 | + states_flattened = flatten_batch(states) # (state_size..., n_thread * update_step) |
| 52 | + |
| 53 | + |
| 54 | + actions = flatten_batch(actions) |
| 55 | + actions = CartesianIndex.(actions, 1:length(actions)) |
| 56 | + |
| 57 | + if learner.bootstrap |
| 58 | + next_state = select_last_frame(t[:next_state]) |
| 59 | + next_state = send_to_device(D, next_state) |
| 60 | + next_state_values = AC.critic(next_state) |
| 61 | + |
| 62 | + gains = discount_rewards( |
| 63 | + rewards, |
| 64 | + γ; |
| 65 | + dims = 2, |
| 66 | + init = send_to_host(next_state_values), |
| 67 | + terminal = terminals, |
| 68 | + ) |
| 69 | + gains = send_to_device(D, gains) |
| 70 | + else |
| 71 | + next_state_flattened = flatten_batch(t[:next_state]) |
| 72 | + next_state_flattened = send_to_device(D, next_state_flattened) |
| 73 | + rewards_flattened = flatten_batch(rewards) |
| 74 | + rewards_flattened = send_to_device(D, rewards_flattened) |
| 75 | + end |
| 76 | + |
| 77 | + action_values = AC.critic(states_flattened) |
| 78 | + |
| 79 | + ps1 = Flux.params(AC.actor) |
| 80 | + gs1 = gradient(ps1) do |
| 81 | + logits = AC.actor(states_flattened) |
| 82 | + probs = softmax(logits) |
| 83 | + actor_loss = -mean(sum((probs .* Zygote.dropgrad(action_values)),dims=1)) |
| 84 | + loss = actor_loss |
| 85 | + ignore() do |
| 86 | + learner.actor_loss = actor_loss |
| 87 | + end |
| 88 | + loss |
| 89 | + end |
| 90 | + if !isnothing(learner.max_grad_norm) |
| 91 | + learner.norm = clip_by_global_norm!(gs1, ps1, learner.max_grad_norm) |
| 92 | + end |
| 93 | + update!(AC.actor, gs1) |
| 94 | + |
| 95 | + ps2 = Flux.params(AC.critic) |
| 96 | + gs2 = gradient(ps2) do |
| 97 | + if learner.bootstrap |
| 98 | + critic_loss = mean((vec(gains) .- vec(action_values[actions])).^ 2) |
| 99 | + else |
| 100 | + next_state_values = AC.critic(next_state_flattened) |
| 101 | + target_action_values = vec(rewards_flattened) .+ γ*vec(Zygote.dropgrad(sum(next_state_values.*softmax(AC.actor(next_state_flattened)),dims=1))) |
| 102 | + critic_loss = mean((vec(target_action_values) .- vec(action_values[actions])) .^ 2) |
| 103 | + end |
| 104 | + |
| 105 | + loss = critic_loss |
| 106 | + ignore() do |
| 107 | + learner.critic_loss = critic_loss |
| 108 | + end |
| 109 | + loss |
| 110 | + end |
| 111 | + if !isnothing(learner.max_grad_norm) |
| 112 | + learner.norm = clip_by_global_norm!(gs2, ps2, learner.max_grad_norm) |
| 113 | + end |
| 114 | + update!(AC.critic, gs2) |
| 115 | +end |
| 116 | + |
| 117 | +function (agent::Agent{<:QBasedPolicy{<:MACLearner},<:CircularCompactSARTSATrajectory})( |
| 118 | + ::Training{PreActStage}, |
| 119 | + env, |
| 120 | +) |
| 121 | + action = agent.policy(env) |
| 122 | + state = get_state(env) |
| 123 | + push!(agent.trajectory; state = state, action = action) |
| 124 | + update!(agent.policy, agent.trajectory) |
| 125 | + |
| 126 | + # the main difference is we'd like to flush the buffer after each update! |
| 127 | + if isfull(agent.trajectory) |
| 128 | + empty!(agent.trajectory) |
| 129 | + push!(agent.trajectory; state = state, action = action) |
| 130 | + end |
| 131 | + |
| 132 | + action |
| 133 | +end |
0 commit comments