Skip to main content

tensorlogic_train/hyperparameter/
value.rs

1//! Hyperparameter value, result, and config type alias.
2
3use std::collections::HashMap;
4
5/// Hyperparameter configuration (a single point in parameter space).
6pub type HyperparamConfig = HashMap<String, HyperparamValue>;
7
8/// Hyperparameter value type.
9#[derive(Debug, Clone, PartialEq)]
10pub enum HyperparamValue {
11    /// Floating-point value.
12    Float(f64),
13    /// Integer value.
14    Int(i64),
15    /// Boolean value.
16    Bool(bool),
17    /// String value.
18    String(String),
19}
20
21impl HyperparamValue {
22    /// Get as f64, if possible.
23    pub fn as_float(&self) -> Option<f64> {
24        match self {
25            HyperparamValue::Float(v) => Some(*v),
26            HyperparamValue::Int(v) => Some(*v as f64),
27            _ => None,
28        }
29    }
30
31    /// Get as i64, if possible.
32    pub fn as_int(&self) -> Option<i64> {
33        match self {
34            HyperparamValue::Int(v) => Some(*v),
35            HyperparamValue::Float(v) => Some(*v as i64),
36            _ => None,
37        }
38    }
39
40    /// Get as bool, if possible.
41    pub fn as_bool(&self) -> Option<bool> {
42        match self {
43            HyperparamValue::Bool(v) => Some(*v),
44            _ => None,
45        }
46    }
47
48    /// Get as string, if possible.
49    pub fn as_string(&self) -> Option<&str> {
50        match self {
51            HyperparamValue::String(v) => Some(v),
52            _ => None,
53        }
54    }
55}
56
57/// Result of a hyperparameter evaluation.
58#[derive(Debug, Clone)]
59pub struct HyperparamResult {
60    /// Hyperparameter configuration used.
61    pub config: HyperparamConfig,
62    /// Evaluation score (higher is better).
63    pub score: f64,
64    /// Additional metrics.
65    pub metrics: HashMap<String, f64>,
66}
67
68impl HyperparamResult {
69    /// Create a new result.
70    pub fn new(config: HyperparamConfig, score: f64) -> Self {
71        Self {
72            config,
73            score,
74            metrics: HashMap::new(),
75        }
76    }
77
78    /// Add a metric to the result.
79    pub fn with_metric(mut self, name: String, value: f64) -> Self {
80        self.metrics.insert(name, value);
81        self
82    }
83}