Skip to main content

11.8 Keras: Python’s deep-learning API

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §11.7. Rewritten in original wording — no textbook Keras.py dump. Continues 11.7b. Next: 11.8b.

Keras (via tf.keras) is a high-level API for stacking and training deep nets. Deep = data flows through multiple layers. A layer takes inputs, applies weights / biases / activations, and emits outputs to the next layer. A dense (fully connected) layer links every unit to every unit in the previous layer.

Dense knobs (conceptually)​

Typical arguments (names matter more than memorizing defaults):

ArgumentRole
unitsOutput dimension
activationsigmoid, relu, tanh, selu, …
use_biasAdd bias terms
kernel_initializerHow weights start (e.g. Glorot)
*_regularizer / *_constraintPenalties / bounds on weights

Hubble again, one dense unit​

A minimal regression model is a single Dense(units=1, input_shape=[1]) inside a Sequential model: that is exactly y=wr+by=wr+b. Compile with MSE loss and an optimizer (e.g. Adam), fit for many epochs, watch Loss fall, then read get_weights() for slope and intercept.

Figure 11.11 — Loss vs epoch; learned line.

Sandbox labs lock the linear map and activation pieces in pure Python; run real Keras in a local TF2 notebook.

# keras_hubble_sketch.py — structure only (local TF)
# layer0 = tf.keras.layers.Dense(units=1, input_shape=[1])
# model = tf.keras.Sequential([layer0])
# model.compile(loss="mean_squared_error", optimizer=tf.keras.optimizers.Adam(...))
# history = model.fit(r, v, epochs=2000, verbose=0)
# w, b = layer0.get_weights()

Takeaway​

  1. Keras layers = reusable building blocks.
  2. Dense = affine map + optional nonlinearity.
  3. compile + fit = Loss + optimizer + epochs.
  4. One-unit Dense ≡ linear regression (Hubble toy).
Quiz

Keras

1 / 4

A dense layer means…

Pick an answer to see if you are right.

Next: 11.8b Keras labs.