R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
116: Torch for R Basics
When I first started using torch in R, I spent an embarrassing amount of time fighting with tensor shapes. Coming from base R, where we treat vectors as flexible things that just "work" via recycling, torch is a cold shower. It demands absolute precision about dimensions.
library(torch)
# I'm trying to multiply an input matrix by a weight matrix
# Input: 5 samples, 3 features each
inputs <- torch_tensor(matrix(rnorm(15), nrow = 5, ncol = 3))
# Weights: 3 features mapping to 2 outputs
weights <- torch_tensor(matrix(rnorm(6), nrow = 3, ncol = 2))
# This works fine
result <- torch_matmul(inputs, weights)
# But then I try to add a bias vector I created like this:
bias <- torch_tensor(matrix(rnorm(2), nrow = 2, ncol = 1))
final_output <- result + bias
# Error: torch_add: size mismatch,
# expecting tensor of shape [5, 2] but got [2, 1]
The 'Size Mismatch' Headache
The error above is the "rite of passage" for torch users. Look closely at result and bias. The result tensor has a shape of [5, 2] (5 samples, 2 output values). My bias tensor is [2, 1]. Even though they both involve the number 2, torch doesn't automatically know that I want to apply that bias vector across every row of my result matrix.
In base R, we're used to the language "guessing" our intent through recycling. torch doesn't guess. It follows strict broadcasting rules. Because the dimensions are [5, 2] and [2, 1], the trailing dimensions don't match, and the leading dimensions aren't 1, so it throws its hands up and quits.
Aligning Dimensions for Broadcasting
To fix this, the bias tensor needs to be compatible with the shape [5, 2]. The easiest way to do this is to make the bias a simple 1D tensor of length 2, or a 2D tensor of shape [1, 2]. When torch sees a dimension of 1, it "broadcasts" (virtually copies) that value to fill the gap.
# Fix: Create the bias as a 1D tensor or a row vector
bias_fixed <- torch_tensor(rnorm(2)) # Shape: [2]
# Now this works perfectly
final_output <- result + bias_fixed
# result [5, 2] + bias_fixed [2] -> torch broadcasts [2] to [5, 2]
Tensors and the Neural Network Layer
While you can do the matrix math manually using torch_matmul, in practice, you'll almost always use nn_modules. These modules handle the weight initialization and the bias broadcasting for you, so you don't have to manually track the shapes of your matrices.
The nn_linear() module is the bread and butter of most networks. It implements $y = xA^T + b$. Notice the transpose ($A^T$); torch does this internally so that you can define your layer by simply stating the input and output sizes.
# A linear layer: 3 inputs, 2 outputs
layer <- nn_linear(3, 2)
# We can pass our inputs directly into the layer
# No need to manually manage weights or bias tensors
output <- layer(inputs)
# output shape is [5, 2]
Managing the Computation Graph
One thing that catches people off guard is how torch tracks every operation to calculate gradients for training. If you're just using a model for prediction (inference), you're wasting memory by keeping this graph alive. I always wrap my evaluation code in with_no_grad().
# This tells torch: "Stop tracking gradients, I'm just predicting"
with_no_grad({
prediction <- layer(inputs)
print(prediction)
})
📋 Practical Task
Building a Simple Linear Regression Model for House Prices
Your task is to create a basic linear model using torch to predict a house price based on two features: square footage and number of bedrooms.
- Create a synthetic input tensor
Xwith 4 samples and 2 features. - Create a synthetic target tensor
ywith 4 samples (1 value each). - Initialize a
nn_linearmodule that maps 2 inputs to 1 output. - Pass
Xthrough the model to get the predictions. - Calculate the Mean Squared Error (MSE) between the predictions and
yusingtorch_mse_loss().
Ensure your y tensor is shaped correctly (typically [4, 1]) to avoid the broadcasting issues we discussed in the lesson.
There are no comments for now.