Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor hopper training and add brain state analysis with resonance space #67

Merged
merged 2 commits into from
Dec 7, 2024

Conversation

leonvanbokhorst
Copy link
Owner

@leonvanbokhorst leonvanbokhorst commented Dec 7, 2024

  • Clean up the hopper training loop by removing unnecessary code and improving readability.
  • Update model loading to handle scenarios where no saved model is available.
  • Introduce weight checkpointing and automatic recovery mechanisms to stabilize training.
  • Implement adaptive noise scaling and warmup periods to enhance performance.
  • Improve tensor handling for state inputs across different devices.

These changes aim to improve the maintainability and robustness of the training process.

Summary by Sourcery

Refactor and enhance the hopper training process by cleaning up the training loop, updating model loading, and improving tensor handling. Introduce new features such as weight checkpointing, automatic recovery, adaptive noise scaling, and warmup periods to stabilize and enhance training. Add a new component for brain state analysis and develop a resonance space for audio processing.

New Features:

  • Introduce weight checkpointing and automatic recovery mechanisms to stabilize the training process.
  • Implement adaptive noise scaling and warmup periods to enhance model performance.
  • Add a new component for analyzing brain states, improving the model's ability to track and analyze state characteristics.
  • Develop a resonance space for audio processing, including resonance pattern generation and audio interface for real-time interaction.

Enhancements:

  • Refactor the hopper training loop to remove unnecessary code and improve readability.
  • Update model loading to handle scenarios where no saved model is available, enhancing robustness.
  • Improve tensor handling for state inputs across different devices, ensuring compatibility and performance.

- Clean up the hopper training loop by removing unnecessary code and improving readability.
- Update model loading to handle scenarios where no saved model is available.
- Introduce weight checkpointing and automatic recovery mechanisms to stabilize training.
- Implement adaptive noise scaling and warmup periods to enhance performance.
- Improve tensor handling for state inputs across different devices.

These changes aim to improve the maintainability and robustness of the training process.
Copy link
Contributor

sourcery-ai bot commented Dec 7, 2024

Reviewer's Guide by Sourcery

The pull request implements a comprehensive refactoring and enhancement of the hopper training process through two main components: a layered brain architecture (BrainInABoxV4) and a resonance space system. The changes focus on improving model training stability, state handling, and monitoring capabilities through enhanced architectures, adaptive mechanisms, and visualization tools.

Class diagram for BrainInABoxV4

classDiagram
    class BrainInABoxV4 {
        +int vocab_size
        +int embed_dim
        +int hidden_dim
        +float dropout
        +int memory_size
        +int num_layers
        +int num_heads
        +string activation
        +nn.Embedding embedding
        +nn.TransformerEncoder reasoning
        +nn.Sequential state_repr
        +nn.Linear output_projection
        +deque memory_states
        +deque attention_patterns
        +list attention_maps
        +dict metrics
        +nn.Parameter adaptive_threshold
        +nn.Parameter confidence_weights
        +StateAnalyzer state_analyzer
        +forward(x, state=None, return_attention=False)
        +initialize_state(x)
        +visualize_brain_activity(show_memory=True, show_attention=True)
    }
    class StateAnalyzer {
        +int hidden_dim
        +list state_history
        +analyze_state(state)
    }
Loading

Class diagram for ResonanceSpace and AudioInterface

classDiagram
    class ResonanceSpace {
        +int sample_rate
        +int buffer_size
        +int memory_depth
        +List memory
        +List resonance_patterns
        +process_input(audio_buffer: np.ndarray) np.ndarray
        +_find_dominant_frequencies(spectrum: np.ndarray, freqs: np.ndarray, num_peaks: int) List[float]
        +_generate_resonance_patterns(frequencies: List[float])
        +_create_response() np.ndarray
    }
    class AudioInterface {
        +ResonanceSpace resonance_space
        +pyaudio.PyAudio p
        +start_stream()
        +stop_stream()
    }
    class ResonancePattern {
        +float frequency
        +float amplitude
        +float phase
        +float duration
    }
    ResonanceSpace --> ResonancePattern
    AudioInterface --> ResonanceSpace
Loading

File-Level Changes

Change Details Files
Implemented a new layered brain architecture with enhanced monitoring and state management
  • Added configurable activation functions and architecture parameters
  • Implemented enhanced state representation with deeper processing layers
  • Added comprehensive metrics tracking for memory, attention, and gradients
  • Implemented adaptive learning components with confidence scoring
  • Added state analysis tools for monitoring complexity and stability
three_layered/layered_brain.py
Added visualization and analysis capabilities for model behavior
  • Implemented attention pattern visualization
  • Added training progress visualization with loss tracking
  • Created comprehensive brain activity visualization tools
  • Added pattern comparison and complexity analysis features
three_layered/layered_brain.py
Implemented a resonance space system for sound interaction
  • Created resonance pattern handling with frequency analysis
  • Implemented adaptive audio processing with memory depth
  • Added real-time audio stream processing capabilities
  • Implemented pattern generation with harmonics and envelopes
sound-interaction/resonance_space.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@leonvanbokhorst leonvanbokhorst self-assigned this Dec 7, 2024
@leonvanbokhorst leonvanbokhorst added the enhancement New feature or request label Dec 7, 2024
@leonvanbokhorst leonvanbokhorst added this to the Phase 1 milestone Dec 7, 2024
- Remove unnecessary "gymnasium[other]" dependency from requirements.txt.
- Ensure pygame is listed correctly for improved compatibility.

These changes streamline the requirements for the project, enhancing clarity and ensuring that the necessary dependencies are properly defined.
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @leonvanbokhorst - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider splitting this into multiple files - separate the core model, visualization, and analysis components. This would improve maintainability and readability.
  • Clean up the duplicate imports at the top of the file. Multiple imports of the same modules indicates poor organization.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

response += wave

max_val = np.max(np.abs(response))
if max_val > 1e-9:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Add more robust handling of near-zero maximum values

The current threshold of 1e-9 could still lead to numerical instability. Consider using np.finfo(response.dtype).eps as a more robust threshold and handle the case where max_val is below this threshold.

audio_buffer = audio_buffer.astype(np.float64)

# Update geheugen
self.memory.append(audio_buffer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Use collections.deque instead of list for memory buffer

Using list.pop(0) is O(n) complexity. Replace the memory list with collections.deque which provides O(1) operations for both ends.

from collections import deque

# Earlier in __init__:
self.memory = deque(maxlen=self.memory_depth)

# Replace the append/pop logic with:
self.memory.append(audio_buffer)

import random


@dataclass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Make ResonancePattern immutable using frozen=True

Since ResonancePattern represents a pattern at a specific point in time, it should be immutable. Add frozen=True to the dataclass decorator.

Suggested change
@dataclass
@dataclass(frozen=True)

}
return activations.get(activation_name.lower(), nn.ReLU())

def forward(self, x, state=None, return_attention=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting monitoring logic into a separate BrainMonitor class to handle state tracking and metrics collection

The forward() method mixes core model logic with monitoring and state management, making it hard to maintain. Consider separating these concerns:

class BrainMonitor:
    def __init__(self):
        self.memory_states = deque(maxlen=100)
        self.attention_patterns = deque(maxlen=100)
        self.metrics = {"memory_evolution": [], "attention_dynamics": []}

    def on_forward(self, attention_weights, state, confidence):
        self.memory_states.append(state.mean(dim=0).detach().cpu())
        if attention_weights:
            self.attention_patterns.append(self._process_attention(attention_weights))
        # Additional monitoring logic...

class BrainInABoxV4(nn.Module):
    def __init__(self, ...):
        self.monitor = BrainMonitor()
        # Other initialization...

    def forward(self, x, state=None):
        if state is None:
            state = self.initialize_state(x)

        # Core neural network operations
        emb = self.embedding(x)
        reasoned = self.reasoning(emb)
        new_state = self.state_repr(reasoned[:, -1, :])

        # Simple confidence calculation
        confidence_score = torch.sigmoid(torch.matmul(new_state, state.transpose(-2, -1)).mean())
        new_state = confidence_score * new_state + (1 - confidence_score) * state

        output = self.output_projection(reasoned)

        # Notify monitor
        self.monitor.on_forward(self.get_attention_weights(), new_state, confidence_score)

        return output, new_state

This approach:

  1. Separates monitoring from core model logic
  2. Eliminates the need for hooks
  3. Makes the forward() method focused and clear
  4. Maintains all existing functionality

Comment on lines +70 to +76
# Voeg subtiele random variatie toe, niet alleen door random selectie
result_freqs = []
for peak in top_peaks:
base_freq = pos_freqs[peak]
result_freqs.append(base_freq * (1 + random.uniform(-0.05, 0.05)))

return result_freqs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
# Voeg subtiele random variatie toe, niet alleen door random selectie
result_freqs = []
for peak in top_peaks:
base_freq = pos_freqs[peak]
result_freqs.append(base_freq * (1 + random.uniform(-0.05, 0.05)))
return result_freqs
return [
pos_freqs[peak] * (1 + random.uniform(-0.05, 0.05))
for peak in top_peaks
]

Comment on lines +263 to +265
if len(self.state_history) < 3:
return None
# Implement pattern detection logic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
if len(self.state_history) < 3:
return None
# Implement pattern detection logic


try:
# Convert to numpy and ensure 2D
attention_display = attention_map.cpu().numpy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Extract code out into function (extract-method)


def compare_text_types(self):
"""Compare patterns between narrative and analytical texts"""
if not all(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): We've found these issues:

Comment on lines +563 to +564
if not all(
len(self.pattern_store[t]["memory"]) > 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Invert any/all to simplify comparisons (invert-any-all)

Suggested change
if not all(
len(self.pattern_store[t]["memory"]) > 0
if any(
len(self.pattern_store[t]["memory"]) <= 0

Comment on lines +599 to +603
temporal_features = {
"memory_evolution": [],
"attention_shifts": [],
"state_transitions": [],
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Inline variable that is immediately returned (inline-immediately-returned-variable)

@leonvanbokhorst leonvanbokhorst changed the title Refactor and enhance hopper training process @sourcesry-ai Dec 7, 2024
@leonvanbokhorst leonvanbokhorst changed the title @sourcesry-ai @sourcery-ai Dec 7, 2024
@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Refactor hopper training and add brain state analysis with resonance space Dec 7, 2024
@leonvanbokhorst leonvanbokhorst merged commit 14c418e into main Dec 7, 2024
1 check failed
@leonvanbokhorst leonvanbokhorst deleted the res_layered branch December 7, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant