R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Building a neural network in R is made significantly easier using specialized packages. One of the popular choices is keras
, a high-level neural network API that interfaces nicely with R.
In this tutorial, we'll use the keras
package in R to build a simple feedforward neural network.
First, you need to install the necessary packages:
install.packages("keras")
Next, you'll need to install TensorFlow, the backend for keras
. You can do this in R using:
library(keras) install_keras()
For this example, we'll use the classic MNIST dataset, which consists of 28x28 pixel handwritten digit images:
mnist <- dataset_mnist() train_images <- mnist$train$x train_labels <- mnist$train$y test_images <- mnist$test$x test_labels <- mnist$test$y
# Rescale the images by dividing every pixel value by 255. train_images <- train_images / 255 test_images <- test_images / 255 # One-hot encode the labels train_labels <- to_categorical(train_labels) test_labels <- to_categorical(test_labels)
Let's create a simple feedforward network with one hidden layer:
model <- keras_model_sequential() %>% layer_flatten(input_shape = c(28, 28)) %>% layer_dense(units = 128, activation = 'relu') %>% layer_dropout(rate = 0.5) %>% layer_dense(units = 10, activation = 'softmax') # Print the summary of the model summary(model)
model %>% compile( loss = 'categorical_crossentropy', optimizer = optimizer_rmsprop(), metrics = c('accuracy') )
history <- model %>% fit( train_images, train_labels, epochs = 10, batch_size = 128, validation_split = 0.2 )
Evaluate the model's performance on the test set:
scores <- model %>% evaluate(test_images, test_labels, batch_size = 128) cat('Test loss:', scores[[1]], '\n') cat('Test accuracy:', scores[[2]], '\n')
predictions <- model %>% predict(test_images)
That's it! You've just built, trained, and evaluated a simple neural network in R using keras
. You can expand on this by experimenting with adding more layers, different activation functions, and other hyperparameters.
Building a Basic Neural Network in R Example:
# Using neuralnet package library(neuralnet) # Generate sample data set.seed(123) data <- data.frame( input1 = rnorm(100), input2 = rnorm(100), output = rnorm(100) ) # Create a basic neural network model neural_model <- neuralnet(output ~ input1 + input2, data = data, hidden = c(5, 3), linear.output = TRUE) # Print the neural network model print(neural_model)
Creating Input and Output Layers in R Neural Network:
# Creating input and output layers neural_model <- neuralnet(output ~ input1 + input2, data = data, hidden = c(5, 3), linear.output = TRUE)
Adding Hidden Layers to a Neural Network in R:
# Adding hidden layers neural_model <- neuralnet(output ~ input1 + input2, data = data, hidden = c(5, 3), linear.output = TRUE)
Training a Simple Neural Network in R:
# Training a neural network trained_model <- train(neural_model, data)
Visualizing a Simple Neural Network in R:
plot
function in the neuralnet
package.# Visualizing a neural network plot(neural_model)