liveBook · Manning

Parameter count

As explained in Deep Learning with R, Third Edition

Model size is the amount of structural and parameter capacity in a machine learning model, determined by features such as layer and unit counts.

Model size reflects a model’s capacity, shaped by its number of layers and the size of those layers. Too little capacity can lead to underfitting; too much can make a model memorize training data and overfit. section 5.4.4

Find an appropriate size by comparing architectures:

  • Start with relatively few layers and parameters, then enlarge layers or add layers until validation loss shows diminishing returns. Compare on validation data, not test data. section 5.4.4
  • Watch both training and validation loss: greater capacity can drive training loss down quickly while widening the gap between training and validation performance. section 5.4.4
Listing 5.11 Version of the model with lower capacity
model <- keras_model_sequential() |>
  layer_dense(4, activation = "relu") |>
  layer_dense(4, activation = "relu") |>
  layer_dense(1, activation = "sigmoid")

model |> compile(
  optimizer = "rmsprop",
  loss = "binary_crossentropy",
  metrics = "accuracy"
)

history_smaller_model <- model |> fit(
  train_data, train_labels,
  epochs = 20, batch_size = 512, validation_split = 0.4
)plot(
  NULL,
  main = "Original Model vs. Smaller Model on IMDB Review Classification",
  xlab = "Epochs",
  xlim = c(1, history_original$params$epochs),
  ylab = "Validation Loss",
  ylim = extendrange(c(history_original$metrics$val_loss,
                       history_smaller_model$metrics$val_loss)),
  panel.first = abline(v = 1:history_original$params$epochs,
                       lty = "dotted", col = "lightgrey")
)

lines(history_original     $metrics$val_loss, lty = 2)
lines(history_smaller_model$metrics$val_loss, lty = 1)
legend("topleft", lty = 2:1,
       legend = c("Validation loss of original model",
                  "Validation loss of smaller model"))

Compare the smaller IMDb review model, with four units in each hidden layer, against the much larger version with 512; the accompanying validation-loss comparisons show how their behavior differs. section 5.4.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Describes an iterative validation-loss workflow but gives no formula for calculating parameter counts.
Examples
Compares IMDb review classifiers with four, 16, and 512 units in each hidden layer.
What you can do after
Evaluate candidate model sizes by comparing validation loss as layers or layer sizes increase.
How it connects
Connects model capacity with underfitting, overfitting, training loss, and validation loss.

Also explained in

Compare how 3 books explain parameter count →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.