---
title: "*1 What is deep learning? 1*: Deep Learning With R"
id: "13172"
type: "page"
slug: "01-deep-learning-with-r"
published_at: "2026-07-19T18:29:47+00:00"
modified_at: "2026-07-20T00:41:40+00:00"
url: "https://preppers-paradise.com/library/deeplearningwithrsecondedition/01-deep-learning-with-r/"
markdown_url: "https://preppers-paradise.com/library/deeplearningwithrsecondedition/01-deep-learning-with-r.md"
excerpt: "This guide provides a comprehensive introduction to deep learning, covering its fundamental concepts, historical evolution, and practical applications. It delves into the mathematical underpinnings of"
taxonomy_category:
  - "AI &amp; Machine Learning"
  - "Books"
  - "Free Teaser"
taxonomy_post_tag:
  - "artificial intelligence"
  - "backpropagation"
  - "data science"
  - "deep learning"
  - "gradient descent"
  - "machine learning"
  - "neural networks"
  - "r programming"
  - "tensors"
---

# *1 What is deep learning? 1*: Deep Learning With R

[← *1 What is deep learning? 1*](/library/deeplearningwithrsecondedition/)

Chapter 1 of 80 · Free teaser

## *Deep Learning with R*

SECOND EDITION

FRANÇOIS CHOLLET WITH TOMASZ KALINOWSKI AND J.J. ALLAIRE

![](https://preppers-paradise.com/wp-content/uploads/x402-books/deeplearningwithrsecondedition/_page_3_Picture_3.jpeg)

{4}------------------------------------------------

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.

> *[Artificial intelligence 2](#page-20-1)* ■ *[Machine learning 3](#page-21-0)* ■ *[Learning](#page-22-0)  [rules and representations from data 4](#page-22-0)* ■ *[The "deep" in "deep](#page-25-0)  [learning" 7](#page-25-0)* ■ *[Understanding how deep learning works, in](#page-26-0)  [three figures 8](#page-26-0)* ■ *[What deep learning has achieved so far 10](#page-28-0) [Don't believe the short-term hype 11](#page-29-0)* ■ *[The promise of AI 12](#page-30-0)*

[1.2 Before deep learning: A brief history of machine learning 13](#page-31-0)

*[Probabilistic modeling 13](#page-31-1)* ■ *[Early neural networks 13](#page-31-2) [Kernel methods 14](#page-32-0)* ■ *[Decision trees, random forests, and](#page-33-0)  [gradient-boosting machines 15](#page-33-0)* ■ *[Back to neural networks 16](#page-34-0) [What makes deep learning different? 17](#page-35-0)* ■ *[The modern machine](#page-35-1)  [learning landscape 17](#page-35-1)*

{6}------------------------------------------------

**vi** CONTENTS

[1.3 Why deep learning? Why now? 20](#page-38-0)

|     | Hardware<br>20<br>■ Data<br>21<br>■ Algorithms<br>22<br>■ A new wave of<br>investment<br>22<br>■ The democratization of deep learning<br>23<br>Will it last?<br>24                                                                                                                                                                                                                                                              |
|-----|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     | 2 The mathematical building blocks of neural networks<br>26                                                                                                                                                                                                                                                                                                                                                                     |
| 2.1 | A first look at a neural network<br>27                                                                                                                                                                                                                                                                                                                                                                                          |
| 2.2 | Data representations for neural networks<br>31                                                                                                                                                                                                                                                                                                                                                                                  |
|     | Scalars (rank 0 tensors)<br>31<br>■ Vectors (rank 1 tensors)<br>31<br>Matrices (rank 2 tensors)<br>32<br>■ Rank 3 and higher-rank<br>tensors<br>32<br>■ Key attributes<br>33<br>■ Manipulating tensors<br>in R<br>34<br>■ The notion of data batches<br>35<br>■ Real-world examples<br>of data tensors<br>35<br>■ Vector data<br>35<br>■ Time-series data or<br>sequence data<br>36<br>■ Image data<br>36<br>■ Video data<br>37 |
| 2.3 | The gears of neural networks: Tensor operations<br>37<br>Element-wise operations<br>38<br>■ Broadcasting<br>40<br>■ Tensor<br>product<br>41<br>■ Tensor reshaping<br>43<br>■ Geometric interpretation<br>of tensor operations<br>44<br>■ A geometric interpretation of deep<br>learning<br>47                                                                                                                                   |
| 2.4 | The engine of neural networks: Gradient-based<br>optimization<br>48                                                                                                                                                                                                                                                                                                                                                             |
|     | What's a derivative?<br>49<br>■ Derivative of a tensor operation: The<br>gradient<br>50<br>■ Stochastic gradient descent<br>51<br>■ Chaining<br>derivatives: The backpropagation algorithm<br>54                                                                                                                                                                                                                                |
| 2.5 | Looking back at our first example<br>59                                                                                                                                                                                                                                                                                                                                                                                         |
|     | Reimplementing our first example from scratch in TensorFlow<br>61<br>Running one training step<br>63<br>■ The full training loop<br>65<br>Evaluating the model<br>66                                                                                                                                                                                                                                                            |
|     | 3 Introduction to Keras and TensorFlow<br>68                                                                                                                                                                                                                                                                                                                                                                                    |
| 3.1 | What's TensorFlow?<br>69                                                                                                                                                                                                                                                                                                                                                                                                        |
| 3.2 | What's Keras?<br>69                                                                                                                                                                                                                                                                                                                                                                                                             |
| 3.3 | Keras and TensorFlow: A brief history<br>71                                                                                                                                                                                                                                                                                                                                                                                     |
| 3.4 | Python and R interfaces: A brief history<br>71                                                                                                                                                                                                                                                                                                                                                                                  |
| 3.5 | Setting up a deep learning workspace<br>72<br>Installing Keras and TensorFlow<br>73                                                                                                                                                                                                                                                                                                                                             |
| 3.6 | First steps with TensorFlow<br>74<br>TensorFlow tensors<br>74                                                                                                                                                                                                                                                                                                                                                                   |

{7}------------------------------------------------

CONTENTS **vii**

[3.7 Tensor attributes 75](#page-93-0)

|     | Tensor shape and reshaping<br>77<br>■ Tensor slicing<br>78<br>■ Tensor<br>broadcasting<br>79<br>■ The tf module<br>80<br>■ Constant tensors and<br>variables<br>81<br>■ Tensor operations: Doing math in TensorFlow<br>82<br>A second look at the GradientTape API<br>83<br>■ An end-to-end<br>example: A linear classifier in pure TensorFlow<br>84                                      |     |
|-----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|
| 3.8 | Anatomy of a neural network: Understanding<br>core Keras APIs<br>89                                                                                                                                                                                                                                                                                                                       |     |
|     | Layers: The building blocks of deep learning<br>89<br>■ From layers<br>to models<br>94<br>■ The "compile" step: Configuring the learning<br>process<br>95<br>■ Picking a loss function<br>98<br>■ Understanding the<br>fit() method<br>99<br>■ Monitoring loss and metrics on validation<br>data<br>99<br>■ Inference: Using a model after training<br>101                                |     |
|     | 4 Getting started with neural networks: Classification                                                                                                                                                                                                                                                                                                                                    |     |
|     | and regression<br>103                                                                                                                                                                                                                                                                                                                                                                     |     |
| 4.1 | Classifying movie reviews: A binary classification example                                                                                                                                                                                                                                                                                                                                | 105 |
|     | The IMDB dataset<br>105<br>■ Preparing the data<br>107<br>■ Building<br>your model<br>108<br>■ Validating your approach<br>110<br>■ Using a<br>trained model to generate predictions on new data<br>113<br>■ Further<br>experiments<br>113<br>■ Wrapping up<br>113                                                                                                                        |     |
| 4.2 | Classifying newswires: A multiclass classification example                                                                                                                                                                                                                                                                                                                                | 114 |
|     | The Reuters dataset<br>114<br>■ Preparing the data<br>116<br>■ Building<br>your model<br>116<br>■ Validating your approach<br>117<br>■ Generating<br>predictions on new data<br>119<br>■ A different way to handle the labels<br>and the loss<br>120<br>■ The importance of having sufficiently large<br>intermediate layers<br>120<br>■ Further experiments<br>121<br>Wrapping up<br>121 |     |
| 4.3 | Predicting house prices: A regression example<br>122                                                                                                                                                                                                                                                                                                                                      |     |
|     | The Boston housing price dataset<br>122<br>■ Preparing the data<br>123<br>Building your model<br>123<br>■ Validating your approach using K-fold<br>validation<br>124<br>■ Generating predictions on new data<br>128<br>Wrapping up<br>128                                                                                                                                                 |     |
|     | 130                                                                                                                                                                                                                                                                                                                                                                                       |     |
| 5.1 | 5 Fundamentals of machine learning<br>Generalization: The goal of machine learning<br>130                                                                                                                                                                                                                                                                                                 |     |
|     | Underfitting and overfitting<br>131<br>■ The nature of generalization in<br>deep learning<br>136                                                                                                                                                                                                                                                                                          |     |
| 5.2 | Evaluating machine learning models<br>142<br>Training, validation, and test sets<br>142<br>■ Beating a common-sense                                                                                                                                                                                                                                                                       |     |

*[baseline 145](#page-163-0)* ■ *[Things to keep in mind about model evaluation 146](#page-164-0)*

{8}------------------------------------------------

**viii** CONTENTS

[5.3 Improving model fit 146](#page-164-1)

*[Tuning key gradient descent parameters 147](#page-165-0)* ■ *[Leveraging better](#page-167-0)  [architecture priors 149](#page-167-0)* ■ *[Increasing model capacity 150](#page-168-0)*

| 5.4 | Improving generalization<br>152                                                                                                                                                                                    |
|-----|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|     | Dataset curation<br>152<br>■ Feature engineering<br>153<br>■ Using early<br>stopping<br>154<br>■ Regularizing your model<br>155                                                                                    |
|     | 166                                                                                                                                                                                                                |
| 6.1 | 6 The universal workflow of machine learning<br>Define the task<br>168                                                                                                                                             |
|     | Frame the problem<br>168<br>■ Collect a dataset<br>169<br>■ Understand<br>your data<br>173<br>■ Choose a measure of success<br>173                                                                                 |
| 6.2 | Develop a model<br>174                                                                                                                                                                                             |
|     | Prepare the data<br>174<br>■ Choose an evaluation protocol<br>175<br>Beat a baseline<br>176<br>■ Scale up: Develop a model that<br>overfits<br>177<br>■ Regularize and tune your model<br>177                      |
| 6.3 | Deploy the model<br>178                                                                                                                                                                                            |
|     | Explain your work to stakeholders and set expectations<br>178<br>■ Ship<br>an inference model<br>179<br>■ Monitor your model in the wild<br>182<br>Maintain your model<br>183                                      |
|     | 185                                                                                                                                                                                                                |
| 7.1 | 7 Working with Keras: A deep dive<br>A spectrum of workflows<br>186                                                                                                                                                |
| 7.2 | Different ways to build Keras models<br>186                                                                                                                                                                        |
|     | The Sequential model<br>187<br>■ The Functional API<br>189<br>Subclassing the Model class<br>196<br>■ Mixing and matching different<br>components<br>199<br>■ Remember: Use the right tool for the job<br>200      |
| 7.3 | Using built-in training and evaluation loops<br>201                                                                                                                                                                |
|     | Writing your own metrics<br>202<br>■ Using callbacks<br>204<br>■ Writing<br>your own callbacks<br>205<br>■ Monitoring and visualization with<br>TensorBoard<br>208                                                 |
| 7.4 | Writing your own training and evaluation loops<br>210                                                                                                                                                              |
|     | Training vs. inference<br>210<br>■ Low-level usage of metrics<br>211<br>A complete training and evaluation loop<br>212<br>■ Make it fast with<br>tf_function()<br>215<br>■ Leveraging fit() with a custom training |
|     | loop<br>216                                                                                                                                                                                                        |
|     | 8 Introduction to deep learning for computer vision<br>220                                                                                                                                                         |

*[operation 228](#page-246-0)*

*[The convolution operation 223](#page-241-0)* ■ *[The max-pooling](#page-246-0)* 

{9}------------------------------------------------

CONTENTS **ix**

| 8.2  | Training a convnet from scratch on a small dataset<br>230<br>The relevance of deep learning for small data problems<br>230<br>Downloading the data<br>231<br>■ Building the model<br>234<br>■ Data<br>preprocessing<br>235<br>■ Using data augmentation<br>241                 |
|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 8.3  | Leveraging a pretrained model<br>245<br>Feature extraction with a pretrained model<br>246<br>■ Fine-tuning a<br>pretrained model<br>254                                                                                                                                        |
|      | 258                                                                                                                                                                                                                                                                            |
| 9.1  | 9 Advanced deep learning for computer vision<br>Three essential computer vision tasks<br>259                                                                                                                                                                                   |
| 9.2  | An image segmentation example<br>260                                                                                                                                                                                                                                           |
| 9.3  | Modern convnet architecture patterns<br>269<br>Modularity, hierarchy, and reuse<br>269<br>■ Residual<br>connections<br>272<br>■ Batch normalization<br>275<br>■ Depthwise<br>separable convolutions<br>278<br>■ Putting it together: A mini<br>Xception-like model<br>280      |
| 9.4  | Interpreting what convnets learn<br>282                                                                                                                                                                                                                                        |
|      | Visualizing intermediate activations<br>283<br>■ Visualizing convnet<br>filters<br>289<br>■ Visualizing heatmaps of class activation<br>294                                                                                                                                    |
|      | 301                                                                                                                                                                                                                                                                            |
| 10.1 | 10 Deep learning for time series<br>Different kinds of time-series tasks<br>301                                                                                                                                                                                                |
| 10.2 | A temperature-forecasting example<br>302<br>Preparing the data<br>306<br>■ A common-sense, non–machine learning<br>baseline<br>310<br>■ Let's try a basic machine learning model<br>311<br>Let's try a 1D convolutional model<br>314<br>■ A first recurrent<br>baseline<br>316 |
| 10.3 | Understanding recurrent neural networks<br>317                                                                                                                                                                                                                                 |
|      | A recurrent layer in Keras<br>320                                                                                                                                                                                                                                              |
| 10.4 | Advanced use of recurrent neural networks<br>324<br>Using recurrent dropout to fight overfitting<br>324<br>■ Stacking<br>recurrent layers<br>327<br>■ Using bidirectional RNNs<br>329<br>Going even further<br>332                                                             |
|      | 334                                                                                                                                                                                                                                                                            |
| 11.1 | 11 Deep learning for text<br>Natural language processing: The bird's-eye view<br>334                                                                                                                                                                                           |
| 11.2 | Preparing text data<br>336                                                                                                                                                                                                                                                     |
|      | Text standardization<br>337<br>■ Text splitting (tokenization)<br>338<br>Vocabulary indexing<br>339<br>■ Using layer_text_vectorization<br>340                                                                                                                                 |
|      |                                                                                                                                                                                                                                                                                |

{10}------------------------------------------------

**x** CONTENTS

| 11.3 | Two approaches for representing groups of words:<br>Sets and sequences<br>344                                                                                                                                                                                                                                                              |
|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|      | Preparing the IMDB movie reviews data<br>345<br>■ Processing words<br>as a set: The bag-of-words approach<br>347<br>■ Processing words as a<br>sequence: The sequence model approach<br>355                                                                                                                                                |
| 11.4 | The Transformer architecture<br>366<br>Understanding self-attention<br>366<br>■ Multi-head attention<br>371<br>The Transformer encoder<br>372<br>■ When to use sequence models over<br>bag-of-words models<br>381                                                                                                                          |
| 11.5 | Beyond text classification: Sequence-to-sequence<br>learning<br>382                                                                                                                                                                                                                                                                        |
|      | A machine translation example<br>383<br>■ Sequence-to-sequence<br>learning with RNNs<br>387<br>■ Sequence-to-sequence learning with<br>Transformer<br>392                                                                                                                                                                                  |
|      | 399                                                                                                                                                                                                                                                                                                                                        |
| 12.1 | 12 Generative deep learning<br>Text generation<br>401                                                                                                                                                                                                                                                                                      |
|      | A brief history of generative deep learning for sequence<br>generation<br>401<br>■ How do you generate sequence data?<br>402<br>The importance of the sampling strategy<br>402<br>■ Implementing text<br>generation with Keras<br>404<br>■ A text-generation callback with<br>variable-temperature sampling<br>408<br>■ Wrapping up<br>413 |
| 12.2 | DeepDream<br>414                                                                                                                                                                                                                                                                                                                           |
|      | Implementing DeepDream in Keras<br>415<br>■ Wrapping up<br>421                                                                                                                                                                                                                                                                             |
| 12.3 | Neural style transfer<br>422                                                                                                                                                                                                                                                                                                               |
|      | The content loss<br>423<br>■ The style loss<br>424<br>■ Neural style transfer<br>in Keras<br>424<br>■ Wrapping up<br>431                                                                                                                                                                                                                   |
| 12.4 | Generating images with variational autoencoders<br>432<br>Sampling from latent spaces of images<br>432<br>■ Concept vectors<br>for image editing<br>433<br>■ Variational autoencoders<br>434<br>Implementing a VAE with Keras<br>436<br>■ Wrapping up<br>442                                                                               |
| 12.5 | Introduction to generative adversarial networks<br>442                                                                                                                                                                                                                                                                                     |
|      | A schematic GAN implementation<br>443<br>■ A bag<br>of tricks<br>444<br>■ Getting our hands on the CelebA dataset<br>445<br>The discriminator<br>447<br>■ The generator<br>447<br>■ The adversarial<br>network<br>448<br>■ Wrapping up<br>452                                                                                              |
|      | 454                                                                                                                                                                                                                                                                                                                                        |
| 13.1 | 13 Best practices for the real world<br>Getting the most out of your models<br>455                                                                                                                                                                                                                                                         |

*[Hyperparameter optimization 455](#page-473-1)* ■ *[Model ensembling 462](#page-480-0)*

{11}------------------------------------------------

CONTENTS **xi**

## [13.2 Scaling-up model training 464](#page-482-0)

*[Speeding up training on GPU with mixed precision 465](#page-483-0) [Multi-GPU training 467](#page-486-0)* ■ *[TPU training 471](#page-489-0)*

# *14 Conclusions 473*

### [14.1 Key concepts in review 474](#page-492-0)

*[Various approaches to AI 474](#page-493-1)* ■ *[What makes deep learning special](#page-493-2)  [within the field of machine learning 474](#page-493-2)* ■ *[How to think about](#page-494-0)  [deep learning 475](#page-494-0)* ■ *[Key enabling technologies 476](#page-495-0)* ■ *[The](#page-496-0)  [universal machine learning workflow 477](#page-496-0)* ■ *[Key network](#page-497-0)  [architectures 478](#page-497-0)* ■ *[The space of possibilities 482](#page-501-0)*

## [14.2 The limitations of deep learning 484](#page-503-0)

*[The risk of anthropomorphizing machine learning models 485](#page-504-0) [Automatons vs. intelligent agents 487](#page-506-0)* ■ *[Local generalization](#page-507-0)  [vs. extreme generalization 488](#page-507-0)* ■ *[The purpose of intelligence 490](#page-509-0) [Climbing the spectrum of generalization 491](#page-510-0)*

- [14.3 Setting the course toward greater generality in AI 492](#page-511-0) *[On the importance of setting the right objective: The shortcut](#page-511-1)  [rule 492](#page-511-1)* ■ *[A new target 494](#page-513-0)*
- [14.4 Implementing intelligence: The missing ingredients 495](#page-514-0) *[Intelligence as sensitivity to abstract analogies 496](#page-515-0)* ■ *[The two poles](#page-516-0)  [of abstraction 497](#page-516-0)* ■ *[The two poles of abstraction 500](#page-519-0)* ■ *[The](#page-519-1)  [missing half of the picture 500](#page-519-1)*

### [14.5 The future of deep learning 501](#page-520-0)

*[Models as programs 502](#page-521-0)* ■ *[Machine learning vs. program](#page-522-0)  [synthesis 503](#page-522-0)* ■ *[Blending together deep learning and program](#page-522-1)  [synthesis 503](#page-522-1)* ■ *[Lifelong learning and modular subroutine](#page-524-0)  [reuse 505](#page-524-0)* ■ *[The long-term vision 506](#page-525-0)*
