Skip to main content

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.py dumps. 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 ∂L/∂(params)\partial\mathcal{L}/\partial(\text{params}) can be computed automatically. Walk Δ\Delta, BB, and ∂L/∂x\partial\mathcal{L}/\partial x for L=(y−mx−b)2\mathcal{L}=(y-mx-b)^{2} — pick each matching equation among lookalikes.

Hydrogen isotope table (u) for mass-excess checks:

NNMM (u)NNMM (u)
01.00782703245.035
12.0140177856.045
23.01604927867.05
34.026

Sklearn-style regression fits a polynomial in AA to tabulated B/AB/A points — fine for pedagogy; nuclear physics is not a single polynomial.

Figure 11.7–11.8 — B/A vs A; Hubble v vs r.

Hubble line via MSE​

Hubble (1929): recessional speed vs distance, historically v≈H0rv\approx H_0 r (huge H0H_0 by modern standards). TF training loop: predict y=mr+by=mr+b, Loss = mean (ypred−ytrue)2(y_{\mathrm{pred}}-y_{\mathrm{true}})^2, step m,bm,b with ∇L\nabla\mathcal{L} for hundreds of iterations. Same SGD story as §11.3, now on astronomy data.

Polynomial features (sklearn)​

PolynomialFeatures(degree=k) expands A↦(A,A2,…)A\mapsto(A,A^2,\ldots); LinearRegression fits coefficients in that basis. Always reshape(-1,1) single-feature columns. High degree overfits seven noisy B/AB/A 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​

ExampleCheck
A=Z+NA=Z+NTensor add
Mass excess(M−A)×931.494028(M-A)\times 931.494028
B/AB/ABinding curve / poly fit
GradientTapeAutodiff of MSE
HubbleSGD linear fit
Quiz

TF / sklearn physics

1 / 4

Mass excess (MeV/c²) uses…

Pick an answer to see if you are right.

Next: 11.6b TF / sklearn labs.