# install.packages("torch")
library(torch)
::install_torch() torch
Building a Simple Neural Network in R with torch
R
Deep Learning
torch
The torch
package brings the power of deep learning to R by providing bindings to the popular PyTorch library. In this post, you’ll learn how to build and train a simple neural network using torch
in R.
Installation
To get started, install the torch
package from CRAN and set up the backend:
Creating a Simple Neural Network
Let’s create a neural network to perform regression on a simple dataset (predicting y
from x
).
1. Generate Sample Data
set.seed(42)
<- torch_randn(100, 1)
x <- 3 * x + 2 + torch_randn(100, 1) * 0.3 y
2. Define the Neural Network Module
<- nn_module(
net initialize = function() {
$fc1 <- nn_linear(1, 8)
self$fc2 <- nn_linear(8, 1)
self
},forward = function(x) {
%>% self$fc1() %>% nnf_relu() %>% self$fc2()
x
}
)<- net() model
3. Set Up the Optimizer and Loss Function
<- optim_adam(model$parameters, lr = 0.01)
optimizer <- nnf_mse_loss loss_fn
4. Training Loop
for(epoch in 1:300) {
$train()
model$zero_grad()
optimizer<- model(x)
y_pred <- loss_fn(y_pred, y)
loss $backward()
loss$step()
optimizerif(epoch %% 50 == 0) {
cat(sprintf("Epoch %d, Loss: %3f\n", epoch, loss$item()))
} }
Epoch 50, Loss: 2.607060
Epoch 100, Loss: 0.559272
Epoch 150, Loss: 0.276259
Epoch 200, Loss: 0.195613
Epoch 250, Loss: 0.170077
Epoch 300, Loss: 0.154462
5. Visualize the Results
<- as.numeric(x$squeeze())
x_np <- as.numeric(y$squeeze())
y_np <- as.numeric(model(x)$squeeze())
y_pred_np
plot(x_np, y_np, main = "Neural Net Regression with torch", xlab = "x", ylab = "y")
points(x_np, y_pred_np, col = 'red', pch = 20)
legend('topleft', legend = c('Actual', 'Predicted'), col = c('black', 'red'), pch = c(1, 20))
This example demonstrates how easy it is to build and train neural networks in R using torch
. You can extend this approach to more complex datasets and deeper architectures as needed.