Skip to main content
# Memory Library

Memory is a comprehensive Python library designed for efficient memory management and experience storage in artificial intelligence projects, especially in reinforcement learning. It provides a wide range of features, including caching with TTL, system and memory information collection, plugin extensibility, uniform and prioritized experience replay buffers, memory monitoring tools, and an interactive dashboard using Streamlit.

Users can easily install this library via pip:

```bash
pip install memory

Table of Contents


Features

  • INFO Class: Collects and displays complete system information including virtual memory and swap details.
  • Memoize Decorator: Caches results of expensive function calls with an optional time-to-live (TTL) to improve performance.
  • Plugin System: Easily extend the library's functionality with custom plugins that respond to events such as storing or sampling experiences.
  • Uniform Experience Replay Buffer: Store and uniformly sample experiences, ideal for reinforcement learning algorithms.
  • Prioritized Experience Replay Buffer: Uses a SumTree data structure to sample experiences based on priority, which can accelerate learning.
  • Memory Monitoring: Tools to monitor system memory usage periodically.
  • Interactive Dashboard: A simple dashboard built with Streamlit to visualize the status of your experience buffers and memory usage.

Installation

Memory requires Python 3.6 or above and the following packages:

You can install these dependencies using pip:

pip install numpy psutil streamlit

After installing the dependencies, simply install Memory with:

pip install memory

Usage

INFO Class

The INFO class gathers system, virtual memory, and swap memory information. Use it to quickly get details about your hardware and software environment.

from memory import INFO

info = INFO()
print(info.display_info())

Memoize Decorator

Cache expensive function calls with an optional TTL to avoid redundant computations.

from memory import memoize
import time

@memoize(ttl=5)
def expensive_computation(x):
    time.sleep(1)  # Simulate heavy computation
    return x * x

print(expensive_computation(4))  # Computation is performed
print(expensive_computation(4))  # Result is fetched from the cache
time.sleep(6)
print(expensive_computation(4))  # TTL expired, computation is performed again

Plugin System

Extend the library by creating custom plugins. For example, a plugin that logs experience storage and sampling events:

from memory import MemoryPlugin, PluginManager

class LoggerPlugin(MemoryPlugin):
    def on_experience_stored(self, experience):
        print("LoggerPlugin: Experience stored:", experience)
    def on_sample(self, batch):
        print("LoggerPlugin: Sampled a batch of", len(batch), "experiences.")

plugin_manager = PluginManager()
logger_plugin = LoggerPlugin()
plugin_manager.register_plugin(logger_plugin)

Experience Replay

Store and uniformly sample experiences with the ExperienceReplay class.

from memory import ExperienceReplay

replay_buffer = ExperienceReplay(capacity=1000, plugin_manager=plugin_manager)

# Example: storing an experience (state, action, reward, next_state, done)
state = [0.1, 0.2, 0.3, 0.4]
action = 1
reward = 0.5
next_state = [0.2, 0.3, 0.4, 0.5]
done = False

replay_buffer.store(state, action, reward, next_state, done)
batch = replay_buffer.sample(5)
print("Uniform sample batch:", batch)

Prioritized Experience Replay

Store and sample experiences based on their priority using the PrioritizedExperienceReplay class.

from memory import PrioritizedExperienceReplay

prioritized_buffer = PrioritizedExperienceReplay(capacity=1000, plugin_manager=plugin_manager)

# Store an experience
prioritized_buffer.store(state, action, reward, next_state, done)
# Sample experiences with importance sampling weights
idxs, batch, weights = prioritized_buffer.sample(5)
print("Prioritized sample batch:", batch)
print("Importance sampling weights:", weights)

Memory Monitoring and Dashboard

Monitor your system's memory usage or launch an interactive dashboard with Streamlit.

Memory Monitoring

from memory import monitor_memory_usage
import threading

monitor_thread = threading.Thread(target=monitor_memory_usage, args=(2,), daemon=True)
monitor_thread.start()

Interactive Dashboard

To run the dashboard, execute the following command in your terminal:

streamlit run memory.py

Example Projects

Project 1: Reinforcement Learning

  • Uniform Experience Replay: Use ExperienceReplay to store and sample experiences for DQN or other RL algorithms.
  • Prioritized Experience Replay: Use PrioritizedExperienceReplay to improve learning efficiency by sampling important experiences more frequently.

Project 2: Optimizing Heavy Computations

  • Memoize Decorator: Cache results of expensive functions to reduce redundant computations in data processing pipelines.

Project 3: System Monitoring and Dashboard

  • Memory Monitoring: Integrate monitor_memory_usage into your server application to log memory usage.
  • Dashboard: Use run_dashboard with Streamlit to create a real-time visualization of system memory and experience buffer status.

Project 4: Custom Plugin Development

  • Extend Memory's functionality by developing plugins that can log activities, send alerts, or perform custom analytics when experiences are stored or sampled.

Contributing

Contributions are welcome! If you would like to contribute to Memory:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Follow Python coding standards and add appropriate documentation and tests.
  4. Submit a pull request with a detailed description of your changes.

License

This library is licensed under the MIT License. See the LICENSE file for more details.


Author

Mohammad Taha Gorji

For issues, feature requests, or other inquiries, please open an issue on the GitHub repository or contact the author directly.


Enjoy using the Memory Library in your projects!

Metadata

Release files for memory 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for memory 1.0.0
File Size Uploaded Provenance
memory-1.0.0.tar.gz 9.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for memory 1.0.0
File Interpreter ABI Platform
memory-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.8 kB

Release files / memory-1.0.0.tar.gz

Download URL memory-1.0.0.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c964913b5421c9b86ac220c82324d5281978d163410f4daa8d1c11316e532c3f
BLAKE2b-256 checksum
How to use checksums
d4b11f1a1b44fb4ca2b309fa75011e2e3a5e96b2c7a5b4c470db8a6f3e3c3345
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.1

Release files / memory-1.0.0-py3-none-any.whl

Download URL memory-1.0.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c53dfa6a14fe1ba51bf83e215167a61056f05d54f560250284d108d2a75c242
BLAKE2b-256 checksum
How to use checksums
e8ad2fecb52715532125daa3cadcf0d332f80687c1c15d0bc9031f997d322621
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.1

Security for memory 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page