Compile fit and evaluate
Quick answer
compile configures the optimizer, loss and metrics; fit trains parameters and evaluate reports metrics without training. predict returns outputs, which may be logits rather than probabilities. Keep data partitions separate and align loss with the model output. Native TensorFlow examples are unexecuted; local Keras uses Torch. ByteMind execution is unverified.
One deterministic fit step
import osos.environ["KERAS_BACKEND"]="torch"import keras,numpy as npm=keras.Sequential([keras.Input((1,)),keras.layers.Dense(1,use_bias=False,kernel_initializer="zeros")])m.compile(optimizer=keras.optimizers.SGD(0.25),loss="mse")x=np.array([[1.]],dtype="float32"); y=np.array([[2.]],dtype="float32")m.fit(x,y,epochs=1,verbose=0)print(float(m.get_weights()[0][0,0]),round(float(m.evaluate(x,y,verbose=0)),6))Expected output (locally tested): 1.0 1.0. The initial gradient is -4, so the new weight is 1. The same-row loss is illustrative, not a test.
How it works
For regression a Dense(1) linear output can use MeanSquaredError. For binary logits use BinaryCrossentropy(from_logits=True); multiclass integer IDs can use SparseCategoricalCrossentropy(from_logits=True) on class logits. Metrics need matching semantics, for example BinaryAccuracy(threshold=0.0) for logits. fit(X,y,batch_size,epochs,validation_data=(Xv,yv)) returns a History. Validation guides tuning; the final test is reserved for one frozen procedure. evaluate returns loss and configured metrics; return_dict=True makes names explicit. Sample weights and regularization may change the reported loss, so do not equate it blindly with a metric. For a custom loop compute loss inside the backend differentiation context, update only training variables, and pass training=False for evaluation. Match target and prediction shapes to avoid unintended broadcasting.
Logit loss and threshold semantics
import osos.environ["KERAS_BACKEND"]="torch"import kerasfrom keras import opsy=ops.convert_to_tensor([[0.],[1.]])z=ops.convert_to_tensor([[0.],[0.]])loss=keras.losses.BinaryCrossentropy(from_logits=True)(y,z)print(round(float(ops.convert_to_numpy(loss)),6))Expected output (locally tested): 0.693147. Zero logits correspond to probabilities of 0.5 and loss log(2).
Common mistakes
Never call fit on final test rows. Do not apply sigmoid twice, pass probabilities as logits, or assume a history list is always monotone. A low training loss alone does not establish generalization.
Practice and next steps
Use one SGD step on a scalar regression with zero initial weight; compute the expected update before executing. Inspect validation history separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compile fit and evaluate — knowledge check
Apply compile fit and evaluate to small examples and debugging scenarios.
Which method updates weights?
Which loss setting matches raw binary logits?
What does fit return?
Which data should choose architecture?
How should batch loss totals be combined?