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.pydump. 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):
| Argument | Role |
|---|---|
units | Output dimension |
activation | sigmoid, relu, tanh, selu, … |
use_bias | Add bias terms |
kernel_initializer | How weights start (e.g. Glorot) |
*_regularizer / *_constraint | Penalties / 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 . 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.
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
- Keras layers = reusable building blocks.
- Dense = affine map + optional nonlinearity.
compile+fit= Loss + optimizer + epochs.- One-unit Dense ≡ linear regression (Hubble toy).
Keras
1 / 4A dense layer means…
Pick an answer to see if you are right.
Next: 11.8b Keras labs.