11.6 TensorFlow and scikit-learn examples
Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §11.5. Rewritten in original wording — no textbook
TensorBE.py/SkPolyFit.py/Hubble.pydumps. Continues 11.5b. Next: 11.6b.
Physics toys check that the ML stack’s arithmetic matches nuclear / cosmology formulas. In this course’s sandbox we evaluate the same formulas in pure Python; run TensorFlow / sklearn locally in a notebook when you want the real APIs.
Mass excess, binding, and autodiff
Atomic masses in u (Daltons); nuclear binding lowers rest energy; TF’s GradientTape records ops so can be computed automatically. Walk , , and for — pick each matching equation among lookalikes.
Hydrogen isotope table (u) for mass-excess checks:
| (u) | (u) | ||
|---|---|---|---|
| 0 | 1.007827032 | 4 | 5.035 |
| 1 | 2.01401778 | 5 | 6.045 |
| 2 | 3.016049278 | 6 | 7.05 |
| 3 | 4.026 |
Sklearn-style regression fits a polynomial in to tabulated points — fine for pedagogy; nuclear physics is not a single polynomial.
Hubble line via MSE
Hubble (1929): recessional speed vs distance, historically (huge by modern standards). TF training loop: predict , Loss = mean , step with for hundreds of iterations. Same SGD story as §11.3, now on astronomy data.
Polynomial features (sklearn)
PolynomialFeatures(degree=k) expands ; LinearRegression fits coefficients in that basis. Always reshape(-1,1) single-feature columns. High degree overfits seven noisy points — expect ugly curves (that’s the lesson).
Sketch (structure only)
# nuclear_ml_sketch.py — original structure (pure Python or TF locally)
U_TO_MEV = 931.494028
def mass_excess_mev(M_u, A):
return (M_u - A) * U_TO_MEV
# TF notebook: Z = tf.constant(1); N = tf.constant(2); A = tf.add(Z, N)
# sklearn: poly.fit_transform(mA.reshape(-1, 1)); LinearRegression().fit(...)
# GradientTape: record loss; tape.gradient(loss, x)
Takeaway
| Example | Check |
|---|---|
| Tensor add | |
| Mass excess |