Several packages allow users to use machine learning directly in R such as nnet for single layer neural nets, rpart for decision trees, and ranger for random forests. Furthermore, with mlr3verse a series of packages exists for managing different algorithms with a unified interface.
These packages can be used with a ‘normal’ computer and provide an easy installation. In terms of natural language processing, these approaches are currently limited. State-of-the-art approaches rely on neural nets with multiple layers and consist of a huge number of parameters making them computationally demanding. With specialized libraries such as keras, PyTorch and tensorflow, graphical processing units (gpu) can help to speed up computations significantly. However, many of these specialized libraries for machine learning are written in python. Fortunately, an interface to python is provided via the R package reticulate.
The R package Artificial Intelligence for Education (aifeducation) aims to provide educators, educational researchers, and social researchers a convincing interface to these state-of-the-art models for natural language processing and tries to address the special needs and challenges of the educational and social sciences. The package currently supports the application of Artificial Intelligence (AI) for tasks such as text embedding, classification, and question answering.
Since state-of-the-art approaches in natural language processing rely on large models compared to classical statistical methods (e.g., latent class analysis, structural equation modeling) and are based largely on python, some additional installation steps are necessary.
If you would like to train and to develop your own models and AIs, a compatible graphic device is necessary. Even a low performing graphic device can speed up computations significantly. If you prefer using pre-trained models however, this is not necessary. In this case a ‘normal’ office computer without a graphic device should be sufficient in most cases.
In order to use the package, you first need to install it. This can be done by:
With this command, all necessary R packages are installed on your machine.
Note: There is currently a bug in reticulate 1.32.0 which does not allow to load python packages such as tensorflow, keras or torch. The error seems to occur only when working with RStudio and Windows machines. Using R console directly seems not to evoke the error. The bug has been fixed in the developer version of reticulate. You can install this version with
install.packages("pak")
andpak::pak("rstudio/reticulate")
. With the release of reticulate 1.33.0 you can ignore this note.
Since natural language processing with neural nets is based on models which are computationally intensive, keras, PyTorch, and tensorflow are used within this package together with some other specialized python libraries. To install them, you need to install python on your machine first. This may take some time.
You can check if everything is working by using the function
reticulate::py_available()
. This should return
TRUE
.
The next step is to install miniconda since aifeducation uses conda environments for managing the different modules.
PyTorch and tensorflow as underlying machine learning backend run on MacOS, Linux, and Windows. However, there are some limitations for accelerate computations with graphic cards. The following table provides an overview.
Table: Possible gpu acceleration by operating system
Operating System | PyTorch | tensorflow |
---|---|---|
MacOS | No | No |
Linux | Yes | Yes |
Windows | Yes | Only with versions <=2.10 |
Windows with WSL | Yes | Yes |
If you have a suitable machine and would like to use a graphic card for computations you need to install some further software. If not you can skip this step. A list with links to downloads can be found here if you would like to use tensorflow as machine learning framework: https://www.tensorflow.org/install/pip#linux
If you would like to use PyTorch as framework you can find further information here: https://pytorch.org/get-started/locally/
In general you need
Except the gpu drivers all components will be installed in step 5 automatically.
If you would like to use Windows with WSL (Windows Subsystem for Linux) installing gpu acceleration is a more complex topic. In this case please refer to the specific Windows or Ubuntu documentations.
If everything is working, you can now install the remaining python
libraries. For convenience, aifeducation comes with an
auxiliary function install_py_modules()
doing that for
you.
#For Linux
aifeducation::install_py_modules(envname="aifeducation",
remove_first=FALSE,
tf_version="<=2.14",
pytorch_cuda_version = "12.1"
cpu_only=FALSE)
#For Windows with gpu support
aifeducation::install_py_modules(envname="aifeducation",
remove_first=FALSE,
tf_version="<=2.10",
pytorch_cuda_version = "12.1"
cpu_only=FALSE)
#For Windows without gpu support
aifeducation::install_py_modules(envname="aifeducation",
remove_first=FALSE,
tf_version="<=2.14",
pytorch_cuda_version = "12.1"
cpu_only=TRUE)
#For MacOS
aifeducation::install_py_modules(envname="aifeducation",
remove_first=FALSE,
tf_version="<=2.14",
pytorch_cuda_version = "12.1"
cpu_only=TRUE)
It is very important that you call this function before loading the package the first time. If you load the library without installing the necessary modules an error may occur.
This function installs the following python modules:
and its dependencies in the environment “aifeducation”.
If you would like to use aifeducation with other packages or within other environments, please ensure that these python modules are available. For gpu support some further packages are installed.
With check_aif_py_modules()
you can check, if all
modules are successfully installed.
Now everything is ready to use the package.
Important note: When you start a new R session, please note that you have to call
reticulate::use_condaenv(condaenv = "aifeducation")
before loading the library to make the python modules available for work.
In general, educators and educational researchers neither have access to high performance computing nor do they own computers with a performing graphic device for their work. Thus, some additional configuration can be done to get computations working on your machine.
If you do use a computer that does own a graphic device, but you
would like to use cpu only you can disable the graphic device support of
tensorflow with the function set_config_cpu_only()
.
Now your machine only uses cpus only for computations.
If your machine has a graphic card but with limited memory, it is
recommended to change the configuration of the memory usage with
set_config_gpu_low_memory()
This enables your machine to compute ‘large’ models with limited resources. For ‘small’ models, this option is not relevant since it decreases the computational speed.
Finally, in some cases you might want to disable tensorflow to print
information on the console. You can change the behavior with the
function set_config_tf_logger()
.
You can choose between five levels “FATAL”, “ERROR”, “WARN”, “INFO”, and “DEBUG”, setting the minimal level for logging.
Before you can work with aifeducation you must set up a new R session. First, it is necessary that you load the library. Second, you must set up python via reticulate. In case you installed python as suggested in this vignette you may start a new session like this:
reticulate::use_condaenv(condaenv = "aifeducation")
library(aifeducation)
set_transformers_logger("ERROR")
Next you have to choose the machine learning framework you would like to use. You can set the framework for the complete session with
#For tensorflow
aifeducation_config$set_global_ml_backend("tensorflow")
#For PyTorch
aifeducation_config$set_global_ml_backend("pytorch")
In the case that you would like to use tensorflow now is a good time to configure that backend, since some configurations can only be done before tensorflow is used the first time.
#if you would like to use only cpus
set_config_cpu_only()
#if you have a graphic device with low memory
set_config_gpu_low_memory()
#if you would like to reduce the tensorflow output to errors
set_config_os_environ_logger(level = "ERROR")
Note: Please remember: Every time you start a new session in R you have to to set the correct conda environment, to load the library aifeducation, and to chose your machine learning framework.
A short introduction into the package with examples for classification tasks can be found in vignette 02 classification tasks.
Documenting and sharing your work is described in vignette 03 sharing and using trained AI/models