TensorFlow is a Python-based, open-source machine learning framework. It handles tensor computation and automatic differentiation, giving you the lower-level tools needed to train models without writing gradient calculations by hand. Keras can use TensorFlow as its backend, providing higher-level APIs for layers, losses, optimizers, and training. section 3.3 section 3.2
A classifier from tensor operations
Start with two groups of points in a 2D plane. A linear classifier predicts with `tf$matmul(inputs, W) + b`, where `W` and `b` are trainable variables. Compare predictions with zero-or-one targets using mean squared error. Each training step computes gradients of that loss and subtracts a fraction of each gradient from the corresponding variable. After training, a threshold of 0.5 assigns a class; the learned boundary can be plotted as a line. section 3.3.2
learning_rate <- 0.1
training_step <- tf_function( #1
jit_compile = TRUE,
\(inputs, targets, W, b) {
with(tf$GradientTape() %as% tape, {
predictions <- model(inputs, W, b) #2
loss <- mean_squared_error(targets, predictions)
})
grad_loss_wrt <- tape$gradient(loss, list(W = W, b = b)) #3
W$assign_sub(grad_loss_wrt$W * learning_rate) #4
b$assign_sub(grad_loss_wrt$b * learning_rate) #4
loss #4
}
)
The training-step listing shows the central TensorFlow pattern: run the forward pass and loss inside `tf$GradientTape()`, retrieve gradients for `W` and `b`, then update both variables. Here `tf_function(jit_compile = TRUE, ...)` wraps the step. section 3.3.2
Choose the level of control you need. Keras handles a standard model-training loop; a custom TensorFlow training step lets you explicitly calculate the loss, obtain gradients with `GradientTape`, and apply them with an optimizer. TensorFlow also supports graph mode and XLA compilation, `tf.data` preprocessing, string and ragged tensors, and an ecosystem for deployment. The trade-offs include a sprawling API and less support on Hugging Face than some alternatives. section 3.7 section 7.4.2 section 3.3.3



