ravimohan19 commited on
Commit
aa6ad0d
·
verified ·
1 Parent(s): 4e35872

Upload examples/multi_fidelity_example.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/multi_fidelity_example.py +73 -0
examples/multi_fidelity_example.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example: Multi-Fidelity Bayesian Optimization
3
+
4
+ Uses the physics model as a cheap low-fidelity source and experimental
5
+ measurements as the expensive high-fidelity source. The multi-fidelity GP
6
+ learns the correlation between fidelities to transfer knowledge.
7
+ """
8
+
9
+ import torch
10
+ from torch import Tensor
11
+
12
+ from physics_informed_bo.experiment.parameter_space import ParameterSpace
13
+ from physics_informed_bo.models.multi_fidelity import MultiFidelitySurrogate
14
+
15
+
16
+ def physics_model(X: Tensor) -> Tensor:
17
+ """Low-fidelity physics model."""
18
+ x1, x2 = X[:, 0], X[:, 1]
19
+ return torch.sin(x1) * x2 + x1 * 0.5
20
+
21
+
22
+ def true_function(X: Tensor) -> Tensor:
23
+ """High-fidelity ground truth (simulating experiments)."""
24
+ x1, x2 = X[:, 0], X[:, 1]
25
+ return torch.sin(x1) * x2 + x1 * 0.5 + 0.3 * torch.cos(3 * x1 * x2)
26
+
27
+
28
+ def main():
29
+ torch.manual_seed(42)
30
+
31
+ # Define space
32
+ space = ParameterSpace()
33
+ space.add_continuous("x1", 0.0, 6.28)
34
+ space.add_continuous("x2", 0.0, 5.0)
35
+
36
+ # Multi-fidelity surrogate
37
+ mf_model = MultiFidelitySurrogate(
38
+ physics_fn=physics_model,
39
+ device="cpu",
40
+ )
41
+
42
+ # Small set of expensive experiments
43
+ X_exp = torch.tensor([
44
+ [1.0, 2.0], [3.0, 1.0], [5.0, 4.0], [2.0, 3.0], [4.0, 2.5]
45
+ ], dtype=torch.float64)
46
+ y_exp = true_function(X_exp).unsqueeze(-1) + 0.05 * torch.randn(5, 1, dtype=torch.float64)
47
+
48
+ # Build multi-fidelity dataset (physics=low, experiments=high)
49
+ X_mf, y_mf = mf_model.build_multi_fidelity_data(
50
+ X_experiment=X_exp,
51
+ y_experiment=y_exp,
52
+ n_physics_points=50,
53
+ )
54
+
55
+ print(f"Multi-fidelity dataset: {len(X_mf)} points "
56
+ f"({len(X_mf) - len(X_exp)} physics + {len(X_exp)} experimental)")
57
+
58
+ # Fit the model
59
+ mf_model.fit(X_mf, y_mf)
60
+
61
+ # Predict at new points (always at high fidelity)
62
+ X_test = torch.tensor([[2.5, 2.5], [4.0, 3.0]], dtype=torch.float64)
63
+ mean, var = mf_model.predict(X_test)
64
+
65
+ print("\nPredictions (high fidelity):")
66
+ for i, (m, v) in enumerate(zip(mean, var)):
67
+ true_val = true_function(X_test[i:i+1]).item()
68
+ print(f" x={X_test[i].tolist()} -> pred={m.item():.3f} ± {v.sqrt().item():.3f} "
69
+ f"(true={true_val:.3f})")
70
+
71
+
72
+ if __name__ == "__main__":
73
+ main()