Experience replay memory
up vote
0
down vote
favorite
I have just begun learning Julia. Here is an implementation of experience replay memory for use in a reinforcement learning algorithm. It is pretty simple, essentially a ring buffer with the following requirements:
- Used to store 1D arrays of numbers, typically Float32 or Float64. All the stored arrays are the same size.
- Has a maximum capacity, after which new entries overwrite old ones
- Has a sample function for retrieving a given number of entries
import Base.length
struct Memory{T <: Real}
max_size::UInt32
experiences::Vector{Vector{T}}
end
Memory{T}(max_size) where {T <: Real} = Memory{T}(max_size, Vector{Vector{T}}())
length(memory::Memory) = length(memory.experiences)
function remember!(memory::Memory, experience)
size = length(memory)
if size == memory.max_size
memory.experiences[1 + size % memory.max_size] = experience
else
push!(memory.experiences, experience)
end
end
function sample(memory::Memory{T}, count::Integer) where {T <: Real}
size = length(memory)
@assert count <= size
return [memory.experiences[1 + rand(UInt32) % size] for i in 1:count]
end
julia
New contributor
add a comment |
up vote
0
down vote
favorite
I have just begun learning Julia. Here is an implementation of experience replay memory for use in a reinforcement learning algorithm. It is pretty simple, essentially a ring buffer with the following requirements:
- Used to store 1D arrays of numbers, typically Float32 or Float64. All the stored arrays are the same size.
- Has a maximum capacity, after which new entries overwrite old ones
- Has a sample function for retrieving a given number of entries
import Base.length
struct Memory{T <: Real}
max_size::UInt32
experiences::Vector{Vector{T}}
end
Memory{T}(max_size) where {T <: Real} = Memory{T}(max_size, Vector{Vector{T}}())
length(memory::Memory) = length(memory.experiences)
function remember!(memory::Memory, experience)
size = length(memory)
if size == memory.max_size
memory.experiences[1 + size % memory.max_size] = experience
else
push!(memory.experiences, experience)
end
end
function sample(memory::Memory{T}, count::Integer) where {T <: Real}
size = length(memory)
@assert count <= size
return [memory.experiences[1 + rand(UInt32) % size] for i in 1:count]
end
julia
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have just begun learning Julia. Here is an implementation of experience replay memory for use in a reinforcement learning algorithm. It is pretty simple, essentially a ring buffer with the following requirements:
- Used to store 1D arrays of numbers, typically Float32 or Float64. All the stored arrays are the same size.
- Has a maximum capacity, after which new entries overwrite old ones
- Has a sample function for retrieving a given number of entries
import Base.length
struct Memory{T <: Real}
max_size::UInt32
experiences::Vector{Vector{T}}
end
Memory{T}(max_size) where {T <: Real} = Memory{T}(max_size, Vector{Vector{T}}())
length(memory::Memory) = length(memory.experiences)
function remember!(memory::Memory, experience)
size = length(memory)
if size == memory.max_size
memory.experiences[1 + size % memory.max_size] = experience
else
push!(memory.experiences, experience)
end
end
function sample(memory::Memory{T}, count::Integer) where {T <: Real}
size = length(memory)
@assert count <= size
return [memory.experiences[1 + rand(UInt32) % size] for i in 1:count]
end
julia
New contributor
I have just begun learning Julia. Here is an implementation of experience replay memory for use in a reinforcement learning algorithm. It is pretty simple, essentially a ring buffer with the following requirements:
- Used to store 1D arrays of numbers, typically Float32 or Float64. All the stored arrays are the same size.
- Has a maximum capacity, after which new entries overwrite old ones
- Has a sample function for retrieving a given number of entries
import Base.length
struct Memory{T <: Real}
max_size::UInt32
experiences::Vector{Vector{T}}
end
Memory{T}(max_size) where {T <: Real} = Memory{T}(max_size, Vector{Vector{T}}())
length(memory::Memory) = length(memory.experiences)
function remember!(memory::Memory, experience)
size = length(memory)
if size == memory.max_size
memory.experiences[1 + size % memory.max_size] = experience
else
push!(memory.experiences, experience)
end
end
function sample(memory::Memory{T}, count::Integer) where {T <: Real}
size = length(memory)
@assert count <= size
return [memory.experiences[1 + rand(UInt32) % size] for i in 1:count]
end
julia
julia
New contributor
New contributor
edited 19 mins ago
Jamal♦
30.2k11115226
30.2k11115226
New contributor
asked 6 hours ago
Atuos
101
101
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Atuos is a new contributor. Be nice, and check out our Code of Conduct.
Atuos is a new contributor. Be nice, and check out our Code of Conduct.
Atuos is a new contributor. Be nice, and check out our Code of Conduct.
Atuos is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208190%2fexperience-replay-memory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown