library(tensorflow)
library(keras)
Beginner
fit()
.
TensorFlow 2 quickstart for beginners
This short introduction uses Keras to:
- Load a prebuilt dataset.
- Build a neural network machine learning model that classifies images.
- Train this neural network.
- Evaluate the accuracy of the model.
Set up TensorFlow
Import TensorFlow into your program to get started:
See the installation guide to learn how to correctly install TensorFlow for R.
Load a dataset
Load and prepare the MNIST dataset. Convert the sample data from integers to floating-point numbers:
c(c(x_train, y_train), c(x_test, y_test)) %<-% keras::dataset_mnist()
<- x_train / 255
x_train <- x_test / 255 x_test
Build a machine learning model
Build a sequential model by stacking layers.
<- keras_model_sequential(input_shape = c(28, 28)) %>%
model layer_flatten() %>%
layer_dense(128, activation = "relu") %>%
layer_dropout(0.2) %>%
layer_dense(10)
For each example, the model returns a vector of logits or log-odds scores, one for each class.
<- predict(model, x_train[1:2, , ]) predictions
1/1 - 0s - 73ms/epoch - 73ms/step
predictions
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.1651525 0.01435302 0.06943683 -0.6274899 0.3188712 0.51209235
[2,] -0.5096894 0.11335309 -0.02402511 -0.8324738 0.2125668 0.07842563
[,7] [,8] [,9] [,10]
[1,] -0.01122756 0.3119570 0.9944065 -0.2704043
[2,] 0.07199600 0.4042611 0.8706847 -0.6965256
The tf$nn$softmax
function converts these logits to probabilities for each class:
$nn$softmax(predictions) tf
tf.Tensor(
[[0.09313394 0.08009707 0.0846329 0.04215682 0.10860934 0.13175954
0.07807412 0.10786099 0.21342654 0.06024875]
[0.05505754 0.10265987 0.0894825 0.03986881 0.11336753 0.09913611
0.09850075 0.13732209 0.21893019 0.04567461]], shape=(2, 10), dtype=float64)
Note: It is possible to bake the tf$nn$softmax
function into the activation function for the last layer of the network. While this can make the model output more directly interpretable, this approach is discouraged as it’s impossible to provide an exact and numerically stable loss calculation for all models when using a softmax output.
Define a loss function for training using loss_sparse_categorical_crossentropy()
, which takes a vector of logits and an integer index of which are TRUE
and returns a scalar loss for each example.
<- loss_sparse_categorical_crossentropy(from_logits = TRUE) loss_fn
This loss is equal to the negative log probability of the true class: The loss is zero if the model is sure of the correct class. This untrained model gives probabilities close to random (1/10 for each class), so the initial loss should be close to -log(1/10) ~= 2.3
.
loss_fn(y_train[1:2], predictions)
tf.Tensor(2.4630766052891806, shape=(), dtype=float64)
Before you start training, configure and compile the model using Keras compile()
. Set the optimizer
class to "adam"
, set the loss
to the loss_fn
function you defined earlier, and specify a metric to be evaluated for the model by setting the metrics
parameter to "accuracy"
.
%>% compile(
model optimizer = "adam",
loss = loss_fn,
metrics = "accuracy"
)
Train and evaluate your model
Use the fit()
method to adjust your model parameters and minimize the loss:
%>% fit(x_train, y_train, epochs = 5) model
Epoch 1/5
1875/1875 - 3s - loss: 0.2946 - accuracy: 0.9149 - 3s/epoch - 2ms/step
Epoch 2/5
1875/1875 - 3s - loss: 0.1414 - accuracy: 0.9582 - 3s/epoch - 1ms/step
Epoch 3/5
1875/1875 - 3s - loss: 0.1054 - accuracy: 0.9676 - 3s/epoch - 1ms/step
Epoch 4/5
1875/1875 - 3s - loss: 0.0824 - accuracy: 0.9746 - 3s/epoch - 1ms/step
Epoch 5/5
1875/1875 - 3s - loss: 0.0711 - accuracy: 0.9775 - 3s/epoch - 1ms/step
The evaluate()
method checks the models performance, usually on a Validation-set or Test-set.
%>% evaluate(x_test, y_test, verbose = 2) model
313/313 - 0s - loss: 0.0712 - accuracy: 0.9783 - 360ms/epoch - 1ms/step
loss accuracy
0.07116836 0.97829998
The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the TensorFlow tutorials.
If you want your model to return which class had the highest probability, you can reuse the trained model to define a new sequential model that also calls softmax and argmax:
<- keras_model_sequential() %>%
probability_model model() %>%
layer_activation_softmax() %>%
layer_lambda(tf$argmax)
probability_model(x_test[1:5, , ])
tf.Tensor([3 2 1 0 4 3 2 0 2 4], shape=(10), dtype=int64)
Conclusion
Congratulations! You have trained a machine learning model using a prebuilt dataset using the Keras API.
For more examples of using Keras, check out the tutorials. To learn more about building models with Keras, read the guides. If you want learn more about loading and preparing data, see the tutorials on image data loading or CSV data loading.