Skip to Content
Course content

116: Torch for R Basics

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 X with 4 samples and 2 features.
  • Create a synthetic target tensor y with 4 samples (1 value each).
  • Initialize a nn_linear module that maps 2 inputs to 1 output.
  • Pass X through the model to get the predictions.
  • Calculate the Mean Squared Error (MSE) between the predictions and y using torch_mse_loss().

Ensure your y tensor is shaped correctly (typically [4, 1]) to avoid the broadcasting issues we discussed in the lesson.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.