A Python library for control memory.
Project description
# 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
ExperienceReplayto store and sample experiences for DQN or other RL algorithms. - Prioritized Experience Replay: Use
PrioritizedExperienceReplayto 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_usageinto your server application to log memory usage. - Dashboard: Use
run_dashboardwith 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:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Follow Python coding standards and add appropriate documentation and tests.
- 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!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memory-1.0.0.tar.gz.
File metadata
- Download URL: memory-1.0.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c964913b5421c9b86ac220c82324d5281978d163410f4daa8d1c11316e532c3f
|
|
| MD5 |
f1f85ebc883f422e31098aaa85881c61
|
|
| BLAKE2b-256 |
d4b11f1a1b44fb4ca2b309fa75011e2e3a5e96b2c7a5b4c470db8a6f3e3c3345
|
File details
Details for the file memory-1.0.0-py3-none-any.whl.
File metadata
- Download URL: memory-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c53dfa6a14fe1ba51bf83e215167a61056f05d54f560250284d108d2a75c242
|
|
| MD5 |
a244beeaa8faa28f51680f44adfb4ddb
|
|
| BLAKE2b-256 |
e8ad2fecb52715532125daa3cadcf0d332f80687c1c15d0bc9031f997d322621
|