Keras Cortex
centimators.keras_cortex
Keras Cortex: A self-improving Keras estimator wrapper using DSPy to self-reflect and improve its architecture.
This module provides KerasCortex, a scikit-learn compatible meta-estimator.
KerasCortex wraps a base Keras estimator (which must have a build_model
method) and iteratively refines the build_model
method's implementation using
a Large Language Model (LLM) through the DSPy library. The goal is to
autonomously improve the model's architecture and performance based on
validation scores.
Highlights
- KerasCortex: Meta-estimator that wraps Keras models and uses an LLM
to iteratively suggest improvements to the
build_model
method. - Think: A DSPy module that orchestrates the LLM interaction to generate Keras code modifications.
- KerasCodeRefinements: A DSPy signature defining the LLM's task for suggesting code changes.
Warning
This module is a work in progress. It is not yet ready for production use.
KerasCodeRefinements
Bases: Signature
Suggest modifications to build_model code to improve performance. Consider the history of attempted code. Use Keras 3, there is no tensorflow or tf.keras. Don't use code fences.
Source code in src/centimators/keras_cortex.py
Think
Bases: Module
DSPy Module for suggesting Keras model code modifications.
This module uses a ChainOfThought
DSPy program with the
KerasCodeRefinements
signature to prompt an LLM for improvements to a
Keras model's build_model
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
bool, default=False
|
If True, prints the LLM's reasoning
and suggested code during the |
False
|
TODO: Add Keras docs, arXiv access, optimize prompts, pass errors back to LLM, etc.
Source code in src/centimators/keras_cortex.py
forward(current_keras_code, performance_log, optimization_goal)
Generates a Keras code modification suggestion using an LLM.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_keras_code
|
str
|
The source code of the current
|
required |
performance_log
|
list[tuple[str, float]]
|
A list of (code, metric)
tuples representing the history of attempted |
required |
optimization_goal
|
str
|
The objective for the LLM, e.g., 'improve validation scores'. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The LLM's suggested |
Source code in src/centimators/keras_cortex.py
KerasCortex
Bases: RegressorMixin
, BaseEstimator
A scikit-learn meta-estimator that iteratively refines a Keras model.
KerasCortex
wraps a base Keras estimator (which must expose a build_model
method) and uses an LLM via DSPy to suggest modifications to this
build_model
method. It iteratively attempts these suggestions, evaluates
their performance on validation data, and keeps the best-performing model
architecture.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_estimator
|
BaseEstimator
|
An instance of a Keras-based
estimator that has a |
None
|
n_iterations
|
int, default=5
|
The number of iterations to run the refinement loop. |
5
|
lm
|
str, default="openai/gpt-4o-mini"
|
The language model to use for
code generation, specified as a string recognized by |
'openai/gpt-4o-mini'
|
verbose
|
bool, default=False
|
If True, prints detailed information during the refinement process, including LLM reasoning and code suggestions. |
False
|
Attributes:
Name | Type | Description |
---|---|---|
best_model_ |
BaseEstimator
|
The best Keras estimator found during the refinement process, after fitting. |
performance_log_ |
list[tuple[str, float]]
|
A log of (code, metric) pairs from the refinement process, after fitting. |
Source code in src/centimators/keras_cortex.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
think_loop(base_estimator, X, y, validation_data, n_iterations=5, **kwargs)
Iteratively refine and retrain a Keras-based estimator.
This method forms the core of KerasCortex
. It takes an initial Keras
estimator, trains it to get a baseline, and then enters a loop:
1. The current build_model
code is sent to the Think
module.
2. The Think
module (using an LLM) suggests a modification to the code.
3. A new model is created with the modified build_model
method.
4. The new model is trained and evaluated on validation data.
5. If the new model performs better, its code becomes the current best.
This loop repeats for n_iterations
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_estimator
|
BaseEstimator
|
An instance of a Keras-based estimator
with |
required |
X
|
array - like
|
Training data (features). |
required |
y
|
array - like
|
Training data (targets). |
required |
validation_data
|
tuple[array - like, array - like]
|
Data (X_val, y_val) for evaluating model performance during refinement. |
required |
n_iterations
|
int, default=5
|
The number of refinement iterations. |
5
|
**kwargs
|
Additional keyword arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
BaseEstimator
|
|
list[tuple[str, float]]
|
|
Source code in src/centimators/keras_cortex.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
fit(X, y, validation_data=None, **kwargs)
Fit the KerasCortex estimator.
This method initiates the think_loop
to find the best model architecture
and then fits this best model. The primary purpose of fit
is to expose
a scikit-learn compatible API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array - like
|
Training data (features). |
required |
y
|
array - like
|
Training data (targets). |
required |
validation_data
|
tuple[array - like, array - like]
|
Data for evaluating model performance during the refinement loop. If None, KerasCortex cannot effectively optimize the model architecture. |
None
|
**kwargs
|
Additional keyword arguments passed to the |
{}
|
Returns:
Name | Type | Description |
---|---|---|
KerasCortex |
The fitted estimator instance. |
Source code in src/centimators/keras_cortex.py
predict(X)
Generate predictions using the best model found by KerasCortex.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array - like
|
Input data (features) for which to make predictions. |
required |
Returns:
Type | Description |
---|---|
array-like: Predictions from the |
Raises:
Type | Description |
---|---|
ValueError
|
If the estimator has not been fitted (i.e., |