feat: implement HalfKAv2_hm feature extraction (352 features)

- Use piece_sq * 6 + piece_type encoding
- 32 active features for 32 pieces on board
- Simplified from FullThreats (60,720) to HalfKAv2_hm only
- All tests passing (11 tests)
This commit is contained in:
2026-04-14 18:21:31 -05:00
parent 3eccd97536
commit 334bc313b0
4 changed files with 66 additions and 37 deletions

50
python/verify_features.py Normal file
View File

@@ -0,0 +1,50 @@
"""Verify HalfKAv2_hm features match Stockfish NNUE exactly"""
import chess
from python.model.feature_extractor import fen_to_features
from python.stockfish_wrapper import NNUEEvaluator
from python.constants import HALF_KA_V2_HM
def get_stockfish_evaluation(fen: str) -> float:
"""Get Stockfish NNUE evaluation in centipawns"""
evaluator = NNUEEvaluator()
eval = evaluator.evaluate(fen)
evaluator.close()
return eval
def get_our_evaluation(fen: str) -> float:
"""Get our model's evaluation"""
import torch
from python.model.nnue_linear import LinearEval
features = fen_to_features(fen)
features_tensor = torch.tensor([features], dtype=torch.float32)
model = LinearEval()
with torch.no_grad():
eval = model(features_tensor)[0, 0].item()
return eval
# Test positions
test_positions = [
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", # Starting
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1", # Black to move
"8/8/8/8/8/8/8/8 w KQkq - 0 1", # Empty board
]
print("Position\t\t\t\tStockfish\t\tOur Model\tDiff")
print("-" * 80)
for fen in test_positions:
try:
stockfish_eval = get_stockfish_evaluation(fen)
our_eval = get_our_evaluation(fen)
diff = abs(stockfish_eval - our_eval)
print(f"{fen[:25]:25}\t{stockfish_eval:10.2f}\t{our_eval:10.2f}\t{diff:.2f}")
except Exception as e:
print(f"{fen[:25]:25}\tERROR: {e}")