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









share|improve this question









New contributor




Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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









    share|improve this question









    New contributor




    Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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









      share|improve this question









      New contributor




      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 19 mins ago









      Jamal

      30.2k11115226




      30.2k11115226






      New contributor




      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 hours ago









      Atuos

      101




      101




      New contributor




      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Atuos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Atuos is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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






























          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.










           

          draft saved


          draft discarded


















          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.















           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'