0

I'm creating a backpropagation neural network in R using neural net The data I use are montly u and v wind, sea surface, relative humidity data from era5 in netcdf (.nc) format to predict monthly rainfall. Initially I built a model with neuralnet with code like this:

# Build Model NN
model = neuralnet(
  rainfall ~ wind_u + wind_v,
  data = data_train,
  hidden = c(3,2),
  threshold = 0.1,
  learningrate = 0.01,
  linear.output = TRUE,
  stepmax = 1e6
)

but the program takes a long time to run. so I reduced my train data to only contain u and v wind data to predict rainfall. then I use parallelly to speed up the process with code like this

ncores = parallelly::availableCores() - 6
cl <- makeCluster(ncores)

# Export libraries and data to each cluster node
clusterExport(cl, list("neuralnet", "data_train"))

# start time
start_time <- Sys.time()
cat("Waktu mulai:", format(start_time, "%H:%M:%S"), "\n")


# Train model with parallelly
models <- parLapply(cl, 1:ncores, function(i) {
  neuralnet(rainfall ~ wind_u + wind_v,
            data = data_train,
            hidden = c(3, 2),
            threshold = 0.1,
            learningrate = 0.01,
            linear.output = TRUE,
            stepmax = 1e6)
})

# stop cluster 
stopCluster(cl)

# end time
end_time <- Sys.time()
cat("Waktu selesai:", format(end_time, "%H:%M:%S"), "\n")

# calculate the time of processing model
execution_time <- end_time - start_time
cat("Durasi eksekusi:", execution_time, "\n")

The result is a model containing a list of nn objects from models[[1]] to models[[10]] How can I use a combination of these models so that I can predict rainfall just by

predict(models, data_test)

The following is the program code for preparing the data that I used

# set working directory
setwd('D:\\temp\\stdm\\')

# Load data
nc_ch = nc_open('input\\ch_train.nc')
nc_wind_rh = nc_open('input\\rh_wind_train.nc')
nc_ch_test = nc_open('input\\ch_test.nc')
nc_wind_rh_test = nc_open('input\\rh_wind_test.nc')

# extract values from variables
# train data
# rh = ncvar_get(nc_wind_rh, "r")  
wind_u = ncvar_get(nc_wind_rh, "u")               
wind_v = ncvar_get(nc_wind_rh, "v")                
rainfall = ncvar_get(nc_ch, "tp") *1000 #convert from m to mm            

# test data
# rh_test = ncvar_get(nc_wind_rh_test, "r")  
wind_u_test = ncvar_get(nc_wind_rh_test, "u")                
wind_v_test = ncvar_get(nc_wind_rh_test, "v")               
rainfall_test = ncvar_get(nc_ch_test, "tp") *1000          

# close .nc
nc_close(nc_ch)
nc_close(nc_wind_rh)
nc_close(nc_ch_test)
nc_close(nc_wind_rh_test)

# convert to 2D data
# rh = as.vector(rh)
wind_u = as.vector(wind_u)
wind_v = as.vector(wind_v)
rainfall = as.vector(rainfall)

# rh_test = as.vector(rh_test)
wind_u_test = as.vector(wind_u_test)
wind_v_test = as.vector(wind_v_test)
rainfall_test = as.vector(rainfall_test)

# convert to dataframe
data_train = data.frame(wind_u, wind_v)
data_test = data.frame(wind_u_test, wind_v_test)

# Normalize data (optional)
data_train = scale(data_train)
data_train = cbind(data_train,rainfall)
data_test = scale(data_test)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.