liveBook · Manning

KerasHub

As explained in Deep Learning with R, Third Edition

KerasHub is a library that provides reusable components and utilities for working with pretrained and generative machine-learning models in Keras.

KerasHub provides Keras implementations of pretrained models, their weights, and tools for preparing inputs. It is a separate package from Keras. In R, declare the Python dependency with `py_require("keras-hub")`, then load it with `import("keras_hub")`; if you manage your own Python environment, install it with `pip install keras-hub`. section 8.3.1

Start with a pretrained image backbone

`py_require("keras-hub")` `keras_hub <- import("keras_hub")` `conv_base <- keras_hub$models$Backbone$from_preset("xception_41_imagenet")` `from_preset()` retrieves the Xception configuration and weights. A backbone is the feature-extracting network without its classification head. For a dogs-versus-cats classifier, preprocess images with an `ImageConverter` from the same preset, extract features with the backbone, and train a new classifier on those features. Freeze the backbone when training a classifier on augmented images so its pretrained weights do not change. section 8.3.1

Presets also help keep language-model parts together. For RoBERTa, load both `Tokenizer` and `Backbone` with `from_preset("roberta_base_en")`. The tokenizer must map text to the token indices used during pretraining, and the backbone must match the architecture expected by the weights. section 15.4.2

Other practical uses include:

  • Wrap SentencePiece as a Keras tokenizer layer to turn text into integer tokens and detokenize them again. The tokenizer can feed a `tf.data` pipeline for mini-GPT pretraining. section 16.2
  • Load `CausalLM` from the `gemma3_1b` preset to get a pretrained Gemma model with a matching tokenizer. Compile a sampling strategy and call `generate()` to continue a prompt. Accessing these weights requires accepting the Gemma terms and authenticating with Kaggle; plausible output is not necessarily useful or accurate. section 16.3.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Shows installation, preset loading, preprocessing, feature extraction, tokenization, and generation through working code rather than KerasHub implementation internals.
Examples
Uses Xception for dogs-versus-cats classification, RoBERTa tokenization, SentencePiece for mini-GPT, and Gemma prompt continuation.
What it assumes
Assumes basic R and Keras model-building familiarity while showing how to install and import the Python package through reticulate.
What you can do after
Apply it via KerasHub presets to load matching pretrained components, extract image features, or generate text.
How it connects
Connects backbones and transfer learning with tokenizers, Transformers, SentencePiece, and causal language models.

Also explained in

Compare how 2 books explain KerasHub →

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.