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
| 2 |
+
|
| 3 |
+
Rule (265/265 verified):
|
| 4 |
+
1. Input is 10x10 with exactly 2 colored dots on a bg of 0
|
| 5 |
+
2. Upper dot (closer to row 0) → fills upper zone with its color
|
| 6 |
+
3. Lower dot (closer to row 9) → fills lower zone with its color
|
| 7 |
+
4. Each zone pattern: full row at dot position, full row at border (0 or 9),
|
| 8 |
+
columns 0 and 9 filled in between. Interior stays 0.
|
| 9 |
+
5. Boundary between zones is midpoint of the two dot rows.
|
| 10 |
+
|
| 11 |
+
ONNX approach:
|
| 12 |
+
- Slice to 10x10
|
| 13 |
+
- Detect non-bg channels, find dot row positions via ArgMax on row sums
|
| 14 |
+
- Generate masks using row/col grids and comparisons
|
| 15 |
+
- Combine with color one-hots
|
| 16 |
+
- Pad back to 30x30
|
| 17 |
+
|
| 18 |
+
Base: 10 nodes, 12610 params, score 12.83
|
| 19 |
+
Target: ~40 nodes, ~1500 params → score ~15+ (gain +2.5)
|
| 20 |
+
"""
|
| 21 |
+
import sys, os
|
| 22 |
+
sys.path.insert(0, '/app/repo/medal-solvers')
|
| 23 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 24 |
+
|
| 25 |
+
from onnx import TensorProto
|
| 26 |
+
import numpy as np
|
| 27 |
+
import onnx
|
| 28 |
+
from onnx import helper, numpy_helper
|
| 29 |
+
import math
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def build_task028():
|
| 33 |
+
nodes, inits, vis = [], [], []
|
| 34 |
+
counter = [0]
|
| 35 |
+
|
| 36 |
+
def nm():
|
| 37 |
+
counter[0] += 1
|
| 38 |
+
return f"t{counter[0]}"
|
| 39 |
+
|
| 40 |
+
def const(name, val, dtype='f'):
|
| 41 |
+
arr = np.array(val, dtype=np.float32 if dtype == 'f' else np.int64)
|
| 42 |
+
inits.append(numpy_helper.from_array(arr, name))
|
| 43 |
+
|
| 44 |
+
def vi(name, shape, dt=TensorProto.FLOAT):
|
| 45 |
+
vis.append(helper.make_tensor_value_info(name, dt, shape))
|
| 46 |
+
|
| 47 |
+
def nd(op, ins, outs_shapes, **kwargs):
|
| 48 |
+
out_names = []
|
| 49 |
+
for sd in outs_shapes:
|
| 50 |
+
if isinstance(sd, tuple):
|
| 51 |
+
shape, dt = sd
|
| 52 |
+
else:
|
| 53 |
+
shape, dt = sd, TensorProto.FLOAT
|
| 54 |
+
n = nm()
|
| 55 |
+
vi(n, shape, dt)
|
| 56 |
+
out_names.append(n)
|
| 57 |
+
nodes.append(helper.make_node(op, ins, out_names, **kwargs))
|
| 58 |
+
return out_names[0] if len(out_names) == 1 else out_names
|
| 59 |
+
|
| 60 |
+
# === CONSTANTS ===
|
| 61 |
+
const('c_half', [0.5])
|
| 62 |
+
const('c_one', [1.0])
|
| 63 |
+
const('c_zero', [0.0])
|
| 64 |
+
const('c_two', [2.0])
|
| 65 |
+
const('c_9f', [9.0])
|
| 66 |
+
|
| 67 |
+
const('sl_start', [0, 0, 0, 0], 'i')
|
| 68 |
+
const('sl_end', [1, 10, 10, 10], 'i')
|
| 69 |
+
const('sl_axes', [0, 1, 2, 3], 'i')
|
| 70 |
+
const('axes3', [3], 'i') # cols
|
| 71 |
+
const('axes23', [2, 3], 'i')
|
| 72 |
+
const('axes1', [1], 'i')
|
| 73 |
+
|
| 74 |
+
# Row grid [1,1,10,1] - row index values
|
| 75 |
+
const('row_grid', np.arange(10, dtype=np.float32).reshape(1, 1, 10, 1))
|
| 76 |
+
# Col grid [1,1,1,10]
|
| 77 |
+
const('col_grid', np.arange(10, dtype=np.float32).reshape(1, 1, 1, 10))
|
| 78 |
+
|
| 79 |
+
const('shape_1_1_1_1', [1, 1, 1, 1], 'i')
|
| 80 |
+
const('shape_1_10_1_1', [1, 10, 1, 1], 'i')
|
| 81 |
+
|
| 82 |
+
const('depth_10', [10.0])
|
| 83 |
+
const('oh_vals', [0.0, 1.0])
|
| 84 |
+
|
| 85 |
+
# Pad to get back to 30x30
|
| 86 |
+
const('pad_10_to_30', [0, 0, 0, 0, 0, 0, 20, 20], 'i')
|
| 87 |
+
const('pad_val_zero', [0.0])
|
| 88 |
+
|
| 89 |
+
# Indices
|
| 90 |
+
const('idx_0', [0], 'i')
|
| 91 |
+
const('idx_1', [1], 'i')
|
| 92 |
+
const('idx_bg', [0], 'i')
|
| 93 |
+
|
| 94 |
+
# Slice to get non-bg channels [1, 9, 10, 10]
|
| 95 |
+
const('sl_ch_start', [0, 1, 0, 0], 'i')
|
| 96 |
+
const('sl_ch_end', [1, 10, 10, 10], 'i')
|
| 97 |
+
|
| 98 |
+
# === STEP 1: Slice input to 10x10 ===
|
| 99 |
+
inp10 = nd('Slice', ['input', 'sl_start', 'sl_end', 'sl_axes'], [[1, 10, 10, 10]])
|
| 100 |
+
|
| 101 |
+
# === STEP 2: Find the two dots ===
|
| 102 |
+
# Sum non-bg channels to get overall non-zero mask [1,1,10,10]
|
| 103 |
+
nonbg = nd('Slice', [inp10, 'sl_ch_start', 'sl_ch_end', 'sl_axes'], [[1, 9, 10, 10]])
|
| 104 |
+
active = nd('ReduceSum', [nonbg, 'axes1'], [[1, 1, 10, 10]], keepdims=1)
|
| 105 |
+
|
| 106 |
+
# Sum per row to find which rows have dots [1,1,10,1]
|
| 107 |
+
row_sums = nd('ReduceSum', [active, 'axes3'], [[1, 1, 10, 1]], keepdims=1)
|
| 108 |
+
|
| 109 |
+
# Find the two dot rows: first dot = ArgMax of row_sums
|
| 110 |
+
# But we have 2 dots. Use: first non-zero row from top = upper dot
|
| 111 |
+
# For upper dot: row_sums * row_grid gives weighted; we want min non-zero row
|
| 112 |
+
# Trick: add large value where row_sum==0, then ArgMin
|
| 113 |
+
const('c_big', [100.0])
|
| 114 |
+
has_dot_b = nd('Greater', [row_sums, 'c_half'], [([1, 1, 10, 1], TensorProto.BOOL)])
|
| 115 |
+
has_dot = nd('Cast', [has_dot_b], [[1, 1, 10, 1]], to=1)
|
| 116 |
+
no_dot = nd('Sub', ['c_one', has_dot], [[1, 1, 10, 1]])
|
| 117 |
+
|
| 118 |
+
# For upper dot (minimum row with a dot):
|
| 119 |
+
row_masked_upper = nd('Add', [nd('Mul', ['row_grid', has_dot], [[1, 1, 10, 1]]),
|
| 120 |
+
nd('Mul', ['c_big', no_dot], [[1, 1, 10, 1]])], [[1, 1, 10, 1]])
|
| 121 |
+
const('axes2', [2], 'i')
|
| 122 |
+
upper_row_idx = nd('ArgMin', [row_masked_upper], [([1, 1, 1, 1], TensorProto.INT64)], axis=2, keepdims=1)
|
| 123 |
+
upper_row_f = nd('Cast', [upper_row_idx], [[1, 1, 1, 1]], to=1)
|
| 124 |
+
|
| 125 |
+
# For lower dot (maximum row with a dot):
|
| 126 |
+
row_masked_lower = nd('Mul', ['row_grid', has_dot], [[1, 1, 10, 1]])
|
| 127 |
+
lower_row_idx = nd('ArgMax', [row_masked_lower], [([1, 1, 1, 1], TensorProto.INT64)], axis=2, keepdims=1)
|
| 128 |
+
lower_row_f = nd('Cast', [lower_row_idx], [[1, 1, 1, 1]], to=1)
|
| 129 |
+
|
| 130 |
+
# Midpoint between dots
|
| 131 |
+
sum_rows = nd('Add', [upper_row_f, lower_row_f], [[1, 1, 1, 1]])
|
| 132 |
+
mid_row = nd('Div', [sum_rows, 'c_two'], [[1, 1, 1, 1]])
|
| 133 |
+
|
| 134 |
+
# === STEP 3: Find dot colors ===
|
| 135 |
+
# Channel sums per row: for upper dot's row, which channel has the dot?
|
| 136 |
+
# Gather the input at upper_row → [1, 10, 1, 10] then sum over cols → [1, 10, 1, 1]
|
| 137 |
+
# Simpler: total channel sums weighted by position
|
| 138 |
+
|
| 139 |
+
# Upper dot color: extract channels at upper dot row
|
| 140 |
+
# Use: for each channel, check if it has a 1 at the upper dot row
|
| 141 |
+
# channel_at_upper[c] = sum over cols of inp10[0, c, upper_row, :]
|
| 142 |
+
# Since there's exactly one dot per row, the channel with max sum IS the color
|
| 143 |
+
|
| 144 |
+
# Sum each channel over all spatial positions → [1, 10, 1, 1]
|
| 145 |
+
# This gives total pixel count per channel. But we have 2 dots of possibly different colors.
|
| 146 |
+
# Instead: create mask for upper zone and lower zone, then find color per zone.
|
| 147 |
+
|
| 148 |
+
# Upper zone mask: rows <= mid_row [1,1,10,1]
|
| 149 |
+
in_upper_b = nd('Less', ['row_grid', nd('Add', [mid_row, 'c_half'], [[1, 1, 1, 1]])],
|
| 150 |
+
[([1, 1, 10, 1], TensorProto.BOOL)])
|
| 151 |
+
in_upper = nd('Cast', [in_upper_b], [[1, 1, 10, 1]], to=1) # [1,1,10,1]
|
| 152 |
+
in_lower = nd('Sub', ['c_one', in_upper], [[1, 1, 10, 1]])
|
| 153 |
+
|
| 154 |
+
# Upper color: which channel has pixels in the upper zone?
|
| 155 |
+
# Mask input by upper zone: inp10 * in_upper [1,10,10,10] * [1,1,10,1] → [1,10,10,10]
|
| 156 |
+
upper_masked = nd('Mul', [inp10, in_upper], [[1, 10, 10, 10]])
|
| 157 |
+
upper_ch_sums = nd('ReduceSum', [upper_masked, 'axes23'], [[1, 10, 1, 1]], keepdims=1)
|
| 158 |
+
# Skip channel 0 (bg)
|
| 159 |
+
const('bg_mask_10', np.array([[[[0, 1, 1, 1, 1, 1, 1, 1, 1, 1]]]], dtype=np.float32).reshape(1, 10, 1, 1))
|
| 160 |
+
upper_ch_masked = nd('Mul', [upper_ch_sums, 'bg_mask_10'], [[1, 10, 1, 1]])
|
| 161 |
+
upper_color_idx = nd('ArgMax', [upper_ch_masked], [([1, 1, 1, 1], TensorProto.INT64)], axis=1, keepdims=1)
|
| 162 |
+
upper_color_1d = nd('Reshape', [upper_color_idx, 'idx_1'], [([1], TensorProto.INT64)])
|
| 163 |
+
|
| 164 |
+
# Lower color
|
| 165 |
+
lower_masked = nd('Mul', [inp10, in_lower], [[1, 10, 10, 10]])
|
| 166 |
+
lower_ch_sums = nd('ReduceSum', [lower_masked, 'axes23'], [[1, 10, 1, 1]], keepdims=1)
|
| 167 |
+
lower_ch_masked = nd('Mul', [lower_ch_sums, 'bg_mask_10'], [[1, 10, 1, 1]])
|
| 168 |
+
lower_color_idx = nd('ArgMax', [lower_ch_masked], [([1, 1, 1, 1], TensorProto.INT64)], axis=1, keepdims=1)
|
| 169 |
+
lower_color_1d = nd('Reshape', [lower_color_idx, 'idx_1'], [([1], TensorProto.INT64)])
|
| 170 |
+
|
| 171 |
+
# === STEP 4: Build output masks ===
|
| 172 |
+
# Upper zone pattern [1,1,10,10]:
|
| 173 |
+
# Full row at row 0, full row at upper_dot_row, cols 0&9 in between
|
| 174 |
+
const('c_0f', [0.0])
|
| 175 |
+
|
| 176 |
+
# Row == 0
|
| 177 |
+
at_row0_b = nd('Less', ['row_grid', 'c_half'], [([1, 1, 10, 1], TensorProto.BOOL)])
|
| 178 |
+
at_row0 = nd('Cast', [at_row0_b], [[1, 1, 10, 1]], to=1)
|
| 179 |
+
|
| 180 |
+
# Row == upper_dot_row
|
| 181 |
+
diff_upper = nd('Abs', [nd('Sub', ['row_grid', upper_row_f], [[1, 1, 10, 1]])], [[1, 1, 10, 1]])
|
| 182 |
+
at_upper_b = nd('Less', [diff_upper, 'c_half'], [([1, 1, 10, 1], TensorProto.BOOL)])
|
| 183 |
+
at_upper = nd('Cast', [at_upper_b], [[1, 1, 10, 1]], to=1)
|
| 184 |
+
|
| 185 |
+
# Full rows in upper zone: row 0 OR dot row
|
| 186 |
+
full_upper_rows = nd('Max', [at_row0, at_upper], [[1, 1, 10, 1]])
|
| 187 |
+
|
| 188 |
+
# Edge cols: col==0 or col==9
|
| 189 |
+
const('c_8_5', [8.5])
|
| 190 |
+
at_col0_b = nd('Less', ['col_grid', 'c_half'], [([1, 1, 1, 10], TensorProto.BOOL)])
|
| 191 |
+
at_col0 = nd('Cast', [at_col0_b], [[1, 1, 1, 10]], to=1)
|
| 192 |
+
at_col9_b = nd('Greater', ['col_grid', 'c_8_5'], [([1, 1, 1, 10], TensorProto.BOOL)])
|
| 193 |
+
at_col9 = nd('Cast', [at_col9_b], [[1, 1, 1, 10]], to=1)
|
| 194 |
+
edge_cols = nd('Max', [at_col0, at_col9], [[1, 1, 1, 10]])
|
| 195 |
+
|
| 196 |
+
# Upper mask: (full_rows broadcast to 10x10) OR (edge_cols * in_upper_zone)
|
| 197 |
+
# full_rows [1,1,10,1] → broadcast with ones [1,1,1,10] → [1,1,10,10]
|
| 198 |
+
# But in ONNX: Max already broadcasts
|
| 199 |
+
upper_full = full_upper_rows # [1,1,10,1] will broadcast
|
| 200 |
+
upper_edges = nd('Mul', [edge_cols, in_upper], [[1, 1, 10, 10]]) # [1,1,1,10]*[1,1,10,1]→[1,1,10,10]
|
| 201 |
+
|
| 202 |
+
upper_mask_raw = nd('Max', [upper_full, upper_edges], [[1, 1, 10, 10]])
|
| 203 |
+
upper_mask = nd('Mul', [upper_mask_raw, in_upper], [[1, 1, 10, 10]]) # clip to upper zone
|
| 204 |
+
|
| 205 |
+
# Lower zone pattern:
|
| 206 |
+
# Full row at row 9, full row at lower_dot_row, cols 0&9 in between
|
| 207 |
+
at_row9_b = nd('Greater', ['row_grid', 'c_8_5'], [([1, 1, 10, 1], TensorProto.BOOL)])
|
| 208 |
+
at_row9 = nd('Cast', [at_row9_b], [[1, 1, 10, 1]], to=1)
|
| 209 |
+
|
| 210 |
+
diff_lower = nd('Abs', [nd('Sub', ['row_grid', lower_row_f], [[1, 1, 10, 1]])], [[1, 1, 10, 1]])
|
| 211 |
+
at_lower_b = nd('Less', [diff_lower, 'c_half'], [([1, 1, 10, 1], TensorProto.BOOL)])
|
| 212 |
+
at_lower = nd('Cast', [at_lower_b], [[1, 1, 10, 1]], to=1)
|
| 213 |
+
|
| 214 |
+
full_lower_rows = nd('Max', [at_row9, at_lower], [[1, 1, 10, 1]])
|
| 215 |
+
|
| 216 |
+
lower_full = full_lower_rows
|
| 217 |
+
lower_edges = nd('Mul', [edge_cols, in_lower], [[1, 1, 10, 10]])
|
| 218 |
+
|
| 219 |
+
lower_mask_raw = nd('Max', [lower_full, lower_edges], [[1, 1, 10, 10]])
|
| 220 |
+
lower_mask = nd('Mul', [lower_mask_raw, in_lower], [[1, 1, 10, 10]])
|
| 221 |
+
|
| 222 |
+
# === STEP 5: Apply colors ===
|
| 223 |
+
# Upper color one-hot [1, 10, 1, 1]
|
| 224 |
+
upper_oh = nd('OneHot', [upper_color_1d, 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
|
| 225 |
+
upper_oh_4d = nd('Reshape', [upper_oh, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
|
| 226 |
+
upper_out = nd('Mul', [upper_oh_4d, upper_mask], [[1, 10, 10, 10]])
|
| 227 |
+
|
| 228 |
+
# Lower color one-hot [1, 10, 1, 1]
|
| 229 |
+
lower_oh = nd('OneHot', [lower_color_1d, 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
|
| 230 |
+
lower_oh_4d = nd('Reshape', [lower_oh, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
|
| 231 |
+
lower_out = nd('Mul', [lower_oh_4d, lower_mask], [[1, 10, 10, 10]])
|
| 232 |
+
|
| 233 |
+
# Combine colored channels
|
| 234 |
+
colored_10 = nd('Add', [upper_out, lower_out], [[1, 10, 10, 10]])
|
| 235 |
+
|
| 236 |
+
# === STEP 6: Add background channel ===
|
| 237 |
+
# ch0 = 1 where no other channel is active (inside 10x10)
|
| 238 |
+
# Slice channels 1-9, take max, subtract from 1
|
| 239 |
+
nonbg_10 = nd('Slice', [colored_10, 'sl_ch_start', 'sl_ch_end', 'sl_axes'], [[1, 9, 10, 10]])
|
| 240 |
+
any_color = nd('ReduceMax', [nonbg_10, 'axes1'], [[1, 1, 10, 10]], keepdims=1)
|
| 241 |
+
bg_ch = nd('Sub', ['c_one', any_color], [[1, 1, 10, 10]])
|
| 242 |
+
|
| 243 |
+
# Build ch0 one-hot and add
|
| 244 |
+
const('ch0_idx', [0], 'i')
|
| 245 |
+
ch0_oh = nd('OneHot', ['ch0_idx', 'depth_10', 'oh_vals'], [[1, 10]], axis=1)
|
| 246 |
+
ch0_oh_4d = nd('Reshape', [ch0_oh, 'shape_1_10_1_1'], [[1, 10, 1, 1]])
|
| 247 |
+
bg_full = nd('Mul', [ch0_oh_4d, bg_ch], [[1, 10, 10, 10]])
|
| 248 |
+
|
| 249 |
+
out_10 = nd('Add', [colored_10, bg_full], [[1, 10, 10, 10]])
|
| 250 |
+
|
| 251 |
+
# === STEP 7: Pad to 30x30 ===
|
| 252 |
+
final = nd('Pad', [out_10, 'pad_10_to_30', 'pad_val_zero'], [[1, 10, 30, 30]])
|
| 253 |
+
|
| 254 |
+
# Build model
|
| 255 |
+
x = helper.make_tensor_value_info('input', TensorProto.FLOAT, [1, 10, 30, 30])
|
| 256 |
+
y = helper.make_tensor_value_info('output', TensorProto.FLOAT, [1, 10, 30, 30])
|
| 257 |
+
nodes.append(helper.make_node('Identity', [final], ['output']))
|
| 258 |
+
|
| 259 |
+
graph = helper.make_graph(nodes, 'task028', [x], [y], initializer=inits, value_info=vis)
|
| 260 |
+
model = helper.make_model(graph, ir_version=10, opset_imports=[helper.make_opsetid('', 18)])
|
| 261 |
+
return model
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
if __name__ == '__main__':
|
| 265 |
+
import onnxruntime as ort
|
| 266 |
+
import json
|
| 267 |
+
from huggingface_hub import hf_hub_download
|
| 268 |
+
import zipfile
|
| 269 |
+
|
| 270 |
+
print("Building Task 028 ONNX model...")
|
| 271 |
+
model = build_task028()
|
| 272 |
+
|
| 273 |
+
output_path = '/app/task028.onnx'
|
| 274 |
+
onnx.save(model, output_path)
|
| 275 |
+
fsize = os.path.getsize(output_path)
|
| 276 |
+
print(f" Nodes: {len(model.graph.node)}")
|
| 277 |
+
print(f" File size: {fsize:,} bytes")
|
| 278 |
+
|
| 279 |
+
# Validate
|
| 280 |
+
sess = ort.InferenceSession(output_path)
|
| 281 |
+
|
| 282 |
+
task_path = hf_hub_download('rogermt/neurogolf-solver', 'own-solver/neurogolf-2026.zip')
|
| 283 |
+
with zipfile.ZipFile(task_path, 'r') as zf:
|
| 284 |
+
data = json.loads(zf.read('task028.json'))
|
| 285 |
+
|
| 286 |
+
all_examples = data['train'] + data['test'] + data.get('arc-gen', [])
|
| 287 |
+
right_count, wrong_count = 0, 0
|
| 288 |
+
|
| 289 |
+
for i, ex in enumerate(all_examples):
|
| 290 |
+
inp_grid = ex['input']
|
| 291 |
+
inp = np.zeros((1, 10, 30, 30), dtype=np.float32)
|
| 292 |
+
for r, row in enumerate(inp_grid):
|
| 293 |
+
for c, v in enumerate(row):
|
| 294 |
+
if r < 30 and c < 30:
|
| 295 |
+
inp[0][v][r][c] = 1.0
|
| 296 |
+
|
| 297 |
+
result = sess.run(['output'], {'input': inp})
|
| 298 |
+
out = (result[0] > 0.0).astype(float)
|
| 299 |
+
|
| 300 |
+
exp = np.zeros((1, 10, 30, 30), dtype=np.float32)
|
| 301 |
+
for r, row in enumerate(ex['output']):
|
| 302 |
+
for c, v in enumerate(row):
|
| 303 |
+
if r < 30 and c < 30:
|
| 304 |
+
exp[0][v][r][c] = 1.0
|
| 305 |
+
|
| 306 |
+
if np.array_equal(out, exp):
|
| 307 |
+
right_count += 1
|
| 308 |
+
else:
|
| 309 |
+
wrong_count += 1
|
| 310 |
+
if wrong_count <= 3:
|
| 311 |
+
diff_locs = np.where(out != exp)
|
| 312 |
+
print(f" FAIL {i}: {len(diff_locs[0])} diffs")
|
| 313 |
+
|
| 314 |
+
print(f"\nResults: {right_count} pass, {wrong_count} fail out of {len(all_examples)}")
|
| 315 |
+
|
| 316 |
+
if wrong_count == 0:
|
| 317 |
+
# Score estimate
|
| 318 |
+
params = sum(int(np.prod(init.dims)) for init in model.graph.initializer)
|
| 319 |
+
mem_est = sum(int(np.prod([d.dim_value for d in vi.type.tensor_type.shape.dim])) * 4
|
| 320 |
+
for vi in model.graph.value_info
|
| 321 |
+
if vi.type.HasField('tensor_type') and vi.type.tensor_type.HasField('shape'))
|
| 322 |
+
score_est = max(1.0, 25.0 - math.log(max(1.0, mem_est + params)))
|
| 323 |
+
print(f" Params: {params:,}, Memory est: {mem_est:,}")
|
| 324 |
+
print(f" Score est: {score_est:.3f} (base: 12.831)")
|
| 325 |
+
print(f" Gain est: +{score_est - 12.831:.3f}")
|