Package ‘neuralnet’ in R, rectified linear unit ReLU activation function?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The neuralnet package does not ship with a built-in "relu" option the way it does for "logistic" and "tanh". It can accept a custom activation function through act.fct, but the package expects that function to be differentiable, which makes raw ReLU a slightly awkward fit compared with modern deep-learning frameworks.
What neuralnet Actually Supports
In neuralnet, the act.fct argument accepts:
- the string
"logistic" - the string
"tanh" - a custom differentiable R function
That means there is no native "relu" keyword. If you want ReLU-like behavior, you either provide a custom function or use a different library that treats ReLU as a first-class choice.
This matters because neuralnet is an older package aimed at relatively small feed-forward networks, not modern deep-learning workloads where ReLU is the default hidden-layer activation.
Why Raw ReLU Is Not Ideal Here
ReLU is defined as max(0, x). In practical machine learning, that is fine because frameworks define a usable derivative convention at zero. In neuralnet, the story is less smooth because the package expects a differentiable activation function and works best with functions it can handle cleanly during training.
So the answer is:
- '
neuralnetdoes not provide built-in ReLU' - a custom function may be possible
- a smooth approximation is often safer in this package
If your goal is specifically “something ReLU-like,” softplus is often the better choice in neuralnet.
A Practical Alternative: Softplus
Softplus behaves like a smoothed ReLU:
This example is runnable, keeps the custom activation path, and stays closer to what neuralnet expects from an activation function.
If You Still Want to Try ReLU
You can experiment with a custom ReLU-style function, but the risk is training instability or derivative-handling issues depending on how the function is defined and how the package processes it.
For small educational experiments, the package can still be useful. For production work or real experimentation with ReLU-heavy networks, keras in R is a better tool.
Here is the equivalent idea with a framework that treats ReLU normally:
That is the cleaner route if the question is really about using ReLU as a standard network component rather than forcing it into an older package.
When neuralnet Is Still Fine
neuralnet is still useful for:
- small tabular experiments
- teaching feed-forward network concepts
- quick prototypes with logistic or tanh activations
It is less attractive when you need:
- ReLU and its common variants
- large datasets
- convolutional or recurrent layers
- modern optimizer and callback support
That boundary is worth stating explicitly so the tool choice matches the problem.
Common Pitfalls
The biggest mistake is assuming act.fct = "relu" is a built-in option. It is not.
Another issue is treating a raw piecewise ReLU definition as if every R neural-network package will handle it like TensorFlow or PyTorch. neuralnet is older and expects custom activation functions to fit its differentiation model.
Developers also sometimes set linear.output incorrectly. If you want the activation applied at the output layer, it must be FALSE; if you want a linear output layer, keep it TRUE.
Finally, do not force an outdated package into a job it was not designed for. If ReLU is central to the model design, switching libraries is often the more honest engineering decision.
Summary
- '
neuralnethas built-in"logistic"and"tanh", not built-in ReLU.' - It can accept a custom activation function through
act.fct. - Because the package expects differentiable custom functions, softplus is often safer than raw ReLU.
- For modern ReLU-based neural networks,
kerasin R is usually the better choice. - Choose the library based on the model you actually want to train.
Related reading
- PacMan what kinds of heuristics are mainly used?
- Pandas and scikit-learn KeyError .... not in index
- pandas dataframe columns scaling with sklearn
- Parallel fitting of multiple Keras Models on single GPU
- Packing different sized circles into rectangle - d3.js
- Padding time-series subsequences for LSTM-RNN training
- Parallel jobs don't finish in scikit-learn's GridSearchCV
- Parallel processes in distributed tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.