Skip to main content

11.5 Part II: Machine learning software

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §11.4. Rewritten in original wording — install steps summarized; no full conda cookbook dump. Continues 11.4b. Next: 11.5b.

Part I built neurons by hand. Part II uses industrial stacks — especially TensorFlow (Google Brain; TF2 since 2019) and friends — for deeper nets. Preprocessing often dominates wall-clock time. Nets shine on large problems (e.g. exoplanet images); tiny pedagogy may not need a TPU.

Tensors as multi-index arrays​

TensorFlow’s computational model is a dataflow graph: nodes = ops, edges = multidimensional arrays (tensors), NumPy-compatible.

TermMeaning
SizeTotal number of elements
Axis / dimensionOne index direction
Rank 0Scalar (shape ())
Rank 1Vector
Rank 2Matrix
Rank NNNN indices
ShapeLength along each axis, e.g. (2,3)
dtypefloat32, float64, int32, bool, …

Example physics check: mass number A=Z+NA=Z+N. With Z=1Z=1, N=2N=2 (tritium) you expect A=3A=3 — a rank-0 integer tensor in TF-speak.

Figure 11.5 — stacked dense layers as a dataflow of tensors.

TF tensors vs NumPy (mental model)​

Treat a TF tensor as a NumPy ndarray with a device and a graph of ops attached. Elementwise +, @, and broadcasting feel the same; the differences that bite beginners are:

Habit from NumPyIn TensorFlow 2
Eager print(arr)Eager works too — but tf.function may freeze a graph
In-place a[i] = …Prefer new tensors / tf.Variable.assign
Always host RAMOps may run on GPU; host↔device copies cost
float64 defaultMany TF recipes default to float32

Conversion bridges: tf.convert_to_tensor(np_arr) and tensor.numpy(). For the isotope / Hubble toys in this chapter, NumPy alone is enough; use TF when you want autodiff (GradientTape) or a Dense stack that matches production notebooks.

When a GPU helps (and when it does not)​

A GPU pays off when many parallel multiply-adds dominate — large batches, wide Dense layers, convolutions on images. It does not help much when:

  • the dataset fits in a few thousand rows and you train for seconds,
  • preprocessing (CSV parse, Python loops) is the bottleneck,
  • you only evaluate a handful of scalar tensors once.

Rule of thumb for this course: develop on CPU; move to GPU only if a real exoplanet / image job spends most of its time inside model.fit. Cloud TPUs are the same idea at larger scale — skip them for pedagogy.

Local install sketch (outside this sandbox)​

On your machine (not graded here):

  1. Python + Jupyter (e.g. via Anaconda / conda).
  2. Create/activate an environment.
  3. conda install -c conda-forge tensorflow (or the current official pip/conda recipe).
  4. In a notebook: import tensorflow as tf then print(tf.__version__) — want 2.x, not 1.x.

Cloud TPUs exist for heavy jobs; you will not need them for the isotope / Hubble toys.

Sklearn in one line​

scikit-learn (pip install scikit-learn) supplies classifiers, regressors, clusterers, and preprocessing helpers. It prefers 2D column layouts (reshape(-1,1)) for single features — different from a casual 1D NumPy vector, but efficient for sparse data.

Takeaway​

ToolRole
TensorFlowDeep nets; tensor graphs; autodiff
NumPyHost array math TF builds on / mirrors
GPU / TPUWorth it for large batch / conv jobs — not tiny toys
scikit-learnClassical ML + preprocessing / poly features
JupyterInteractive exploration

Next lessons compute nuclear / Hubble quantities with pure Python locks that mirror TF/sklearn workflows, then sketch GradientTape and poly regression.

Quiz

ML software

1 / 4

A TensorFlow tensor of shape () is…

Pick an answer to see if you are right.

Next: 11.5b ML software labs.