{"id":8035,"date":"2023-10-25T15:19:34","date_gmt":"2023-10-25T23:19:34","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8035"},"modified":"2025-04-24T17:05:08","modified_gmt":"2025-04-24T17:05:08","slug":"text-classification-using-r-keras-and-comet-ml","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\/","title":{"rendered":"Text Classification Using R, Keras, and Comet ML"},"content":{"rendered":"\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\">\n\n\n\n<figure class=\"wp-block-image lz ma mb mc md me lw lx paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Source: <a class=\"af mq\" href=\"https:\/\/unsplash.com\/photos\/IhWYiwSxm8g\" target=\"_blank\" rel=\"noopener ugc nofollow\">https:\/\/unsplash.com\/photos\/IhWYiwSxm8g<\/a><\/figcaption><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"c584\">Text classification is an interesting application of natural language processing. It is a supervised learning methodology that predicts if a piece of text belongs to one category or the other. As a machine learning engineer, you start with a labeled data set that has vast amounts of text that have already been categorized. These algorithms can perform sentiment analysis, create spam filters, and other applications.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"4034\">This tutorial will teach you how to train your binary text classifiers using Keras. You will use the <a class=\"af mq\" href=\"https:\/\/www.kaggle.com\/datasets\/lakshmi25npathi\/imdb-dataset-of-50k-movie-reviews\" target=\"_blank\" rel=\"noopener ugc nofollow\">IMDB datase<\/a>t that has 50K+ movie reviews that are classified as positive or negative. The main goal of this tutorial is to equip you with the skills of using R and R Studio to build the classifier while using Comet ML\u2019s platform to monitor your experiments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"f27e\"><strong class=\"al\">Pre-requisites<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"b51a\">To follow along with this tutorial, you need to prepare your development environment as follows:<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"cfe2\">\u00b7 Make sure you have R installed from the <a class=\"af mq\" href=\"https:\/\/cran.r-project.org\/bin\/windows\/base\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Official Website<\/a><\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"da2e\">\u00b7 After installing R, install <a class=\"af mq\" href=\"https:\/\/posit.co\/download\/rstudio-desktop\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">R Studio<\/a>, the preferred IDE for creating R projects.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"02cc\">\u00b7 Visit <a class=\"af mq\" href=\"\/signup?utm_source=heartbeat&amp;utm_medium=referral&amp;utm_campaign=AMS_US_EN_SNUP_heartbeat_CTA\" target=\"_blank\" rel=\"noopener ugc nofollow\">Comet ML\u2019s platform<\/a> to sign up for an account<\/p>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"5f02\"><strong class=\"al\">Getting Started<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"3135\">Let\u2019s install some dependencies that you need to build your classifier. You will install all these dependencies by using R Studio. Open R Studio and in the console, type in the following commands to install the dependencies needed.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"caa1\">First, install Comet\u2019s R package which will be used to log metrics to your Comet<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"40b1\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">install.packages<span class=\"hljs-punctuation\">(<\/span>\u201ccometr\u201d<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"ee32\">Next, you will need to install the Keras package for R by running the following command in the console. Keras provides a simple API to build neural networks and uses Tensorflow as the backend.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"c368\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">install.packages<span class=\"hljs-punctuation\">(<\/span>\u201ckeras\u201d<span class=\"hljs-punctuation\">)<\/span>\ninstall_keras<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"590f\">Now we have our development environment setup. Let\u2019s move on to loading the datasets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"0745\"><strong class=\"al\">Data Loading<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"5f53\">The IMDB dataset is already preloaded with Keras, making it easy to load it. Create a new R Script and call it train.R. This file will hold all your source code for this project. Add the code below to download the IMDB dataset that has 50K+ reviews for movies from the IMDB website.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"3078\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">library<span class=\"hljs-punctuation\">(<\/span>keras<span class=\"hljs-punctuation\">)<\/span>\nlibrary<span class=\"hljs-punctuation\">(<\/span>cometr<span class=\"hljs-punctuation\">)<\/span>\nimdb <span class=\"hljs-operator\">&lt;-<\/span> dataset_imdb<span class=\"hljs-punctuation\">(<\/span>num_words <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">10000<\/span><span class=\"hljs-punctuation\">)<\/span>\ntrain_data <span class=\"hljs-operator\">&lt;-<\/span> imdb<span class=\"hljs-operator\">$<\/span>train<span class=\"hljs-operator\">$<\/span>x\ntrain_labels <span class=\"hljs-operator\">&lt;-<\/span> imdb<span class=\"hljs-operator\">$<\/span>train<span class=\"hljs-operator\">$<\/span>y\ntest_data <span class=\"hljs-operator\">&lt;-<\/span> imdb<span class=\"hljs-operator\">$<\/span>test<span class=\"hljs-operator\">$<\/span>x\ntest_labels <span class=\"hljs-operator\">&lt;-<\/span> imdb<span class=\"hljs-operator\">$<\/span>test<span class=\"hljs-operator\">$<\/span>y<\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"06d9\">The data is partially preprocessed. This means that the sequence of words representing reviews has already been converted to a sequence of integers. Each integer stands for a unique word in a dictionary.<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"56d2\">However, you will still need to convert the sequence of numbers to vectors. In order to do this, you will write a simple function that vectorizes your lists. This function will use the one-hot encoding methodology. The one-hot encoding converts your lists of integers to an N-dimensional vector where N is the count of unique words in the data. The term being represented will have a value of 1 while the rest are represented by 0. You can choose to explore other vectorization functions like TF-IDF that are a bit more memory efficient.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"af88\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">vectorize_seq <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-keyword\">function<\/span><span class=\"hljs-punctuation\">(<\/span>sequences<span class=\"hljs-punctuation\">,<\/span> dimension <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">10000<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-punctuation\">{<\/span>\n  results <span class=\"hljs-operator\">&lt;-<\/span> matrix<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">0<\/span><span class=\"hljs-punctuation\">,<\/span> nrow <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">length<\/span><span class=\"hljs-punctuation\">(<\/span>sequences<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span> ncol <span class=\"hljs-operator\">=<\/span> dimension<span class=\"hljs-punctuation\">)<\/span>\n  <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-punctuation\">(<\/span>i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-operator\">:<\/span><span class=\"hljs-built_in\">length<\/span><span class=\"hljs-punctuation\">(<\/span>sequences<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span>\n    results<span class=\"hljs-punctuation\">[<\/span>i<span class=\"hljs-punctuation\">,<\/span> sequences<span class=\"hljs-punctuation\">[[<\/span>i<span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">]<\/span><span class=\"hljs-punctuation\">]<\/span> <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-number\">1<\/span>\n  results\n<span class=\"hljs-punctuation\">}<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"ff45\">Make sure you convert the training and testing datasets using the function created above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"837b\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">x_train <span class=\"hljs-operator\">&lt;-<\/span> vectorize_seq<span class=\"hljs-punctuation\">(<\/span>train_data<span class=\"hljs-punctuation\">)<\/span>\nx_test <span class=\"hljs-operator\">&lt;-<\/span> vectorize_seq<span class=\"hljs-punctuation\">(<\/span>test_data<span class=\"hljs-punctuation\">)<\/span>\n\ny_train <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">as.numeric<\/span><span class=\"hljs-punctuation\">(<\/span>train_labels<span class=\"hljs-punctuation\">)<\/span>\ny_test <span class=\"hljs-operator\">&lt;-<\/span> <span class=\"hljs-built_in\">as.numeric<\/span><span class=\"hljs-punctuation\">(<\/span>test_labels<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"2bbf\"><code class=\"cw pc pd pe ou b\">Training Neural Network<\/code><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"b7a7\">Let\u2019s use a simple architecture to build the network. A stack of fully connected layers with relu activation functions can solve our binary classifications problem. However, some design decisions need to be made<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"e5cf\">\u00b7 How many layers will your network have?<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"3e23\">\u00b7 How many hidden units do you need for each layer?<\/p>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"882c\">For this tutorial, let\u2019s keep it simple and have the architecture as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image oo op oq or os me lw lx paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:504\/1*wtTOiypEOK2izNrgsUW4AQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"fc08\">It is also important to note that you will need a sigmoid function that will be used to calculate the output probabilities (0 for negative or 1 for positive). Keras provides a simple API and you can create your model as shown in the code block below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"5bbb\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">model <span class=\"hljs-operator\">&lt;-<\/span> keras_model_sequential<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&gt;%<\/span>\n  layer_dense<span class=\"hljs-punctuation\">(<\/span>units <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">16<\/span><span class=\"hljs-punctuation\">,<\/span> activation <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"relu\"<\/span><span class=\"hljs-punctuation\">,<\/span> input_shape <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-number\">10000<\/span><span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&gt;%<\/span>\n  layer_dense<span class=\"hljs-punctuation\">(<\/span>units <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">16<\/span><span class=\"hljs-punctuation\">,<\/span> activation <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"relu\"<\/span><span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">%&gt;%<\/span>\n  layer_dense<span class=\"hljs-punctuation\">(<\/span>units <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">1<\/span><span class=\"hljs-punctuation\">,<\/span> activation <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"sigmoid\"<\/span><span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"3523\"><strong class=\"al\">Loss Function &amp; Optimizers<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"59a8\">This section demonstrates how to add loss functions and optimizers to your neural network. Because the output of the network is a probability, you can use a binary cross-entropy loss function. You will use a learning rate of 0.01 for your optimizer. Add the following code block to your R script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"4f3c\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">learning_rate <span class=\"hljs-operator\">&lt;-<\/span> 0.001\nmodel <span class=\"hljs-operator\">%&gt;%<\/span> compile<span class=\"hljs-punctuation\">(<\/span>\n  optimizer <span class=\"hljs-operator\">=<\/span> optimizer_rmsprop<span class=\"hljs-punctuation\">(<\/span>lr<span class=\"hljs-operator\">=<\/span>learning_rate<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span>\n  loss <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"binary_crossentropy\"<\/span><span class=\"hljs-punctuation\">,<\/span>\n  metrics <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">c<\/span><span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"accuracy\"<\/span><span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">)<\/span> <\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"8a5f\"><strong class=\"al\">Monitoring<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"6d06\">Before you start training, you\u2019ll need to integrate Comet ML\u2019s R package by adding the following code block. Make sure you have your API key from your Comet ML\u2019s account then create a <code class=\"cw pc pd pe ou b\">~\/.comet.yml.<\/code> Follow the official <a class=\"af mq\" href=\"https:\/\/www.comet.com\/docs\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">documentation<\/a> for additional help with getting started with R.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"c5f2\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\"><span class=\"hljs-attr\">COMET_WORKSPACE:<\/span> <span class=\"hljs-string\">my-comet-user-name<\/span>\n<span class=\"hljs-attr\">COMET_PROJECT_NAME:<\/span> <span class=\"hljs-string\">nlpkeras<\/span>\n<span class=\"hljs-attr\">COMET_API_KEY:<\/span> <span class=\"hljs-string\">my-comet-api-key<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"d8a3\">Next, create an experiment as shown below in your R script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"0a4a\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\"><span class=\"hljs-built_in\">exp<\/span> <span class=\"hljs-operator\">&lt;-<\/span> create_experiment<span class=\"hljs-punctuation\">(<\/span>\n  keep_active <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_output <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_error <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">FALSE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_code <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_system_details <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span><span class=\"hljs-punctuation\">,<\/span>\n  log_git_info <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-literal\">TRUE<\/span>\n<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"ca9d\">Next, you need to specify the number of epochs you want to train the network and log it using Comet ML.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"d7ca\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">epochs <span class=\"hljs-operator\">&lt;-<\/span> 20\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_parameter<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"epochs\"<\/span><span class=\"hljs-punctuation\">,<\/span> epochs<span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_parameter<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"learning_rate\"<\/span><span class=\"hljs-punctuation\">,<\/span> learning_rate<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"a860\"><strong class=\"al\">Training<\/strong><\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"3a48\">Now, train the model using the training datasets created above. Also, make sure you add the model evaluation using the validation sets created above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"4aa7\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">history <span class=\"hljs-operator\">&lt;-<\/span> model <span class=\"hljs-operator\">%&gt;%<\/span> fit<span class=\"hljs-punctuation\">(<\/span>\n  partial_x_train<span class=\"hljs-punctuation\">,<\/span>\n  partial_y_train<span class=\"hljs-punctuation\">,<\/span>\n  epochs <span class=\"hljs-operator\">=<\/span> epochs<span class=\"hljs-punctuation\">,<\/span>\n  batch_size <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">512<\/span><span class=\"hljs-punctuation\">,<\/span>\n  validation_data <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-built_in\">list<\/span><span class=\"hljs-punctuation\">(<\/span>x_val<span class=\"hljs-punctuation\">,<\/span> y_val<span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"a90a\">The fit method returns a history object that has a plot function that you can use to visualize the training and validation metrics by epoch.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"fdb1\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">plot<span class=\"hljs-punctuation\">(<\/span>history<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"3081\">Make sure you log the training loss and accuracy metrics to Comet ML.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><span id=\"a1ae\" class=\"ox np fr ou b bf oy oz l pa pb\" data-selectable-paragraph=\"\">results <span class=\"hljs-operator\">&lt;-<\/span> model <span class=\"hljs-operator\">%&gt;%<\/span> evaluate<span class=\"hljs-punctuation\">(<\/span>x_test<span class=\"hljs-punctuation\">,<\/span> y_test<span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_metric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"val_loss\"<\/span><span class=\"hljs-punctuation\">,<\/span> results<span class=\"hljs-operator\">$<\/span>loss<span class=\"hljs-punctuation\">)<\/span>\n<span class=\"hljs-built_in\">exp<\/span><span class=\"hljs-operator\">$<\/span>log_metric<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"val_acc\"<\/span><span class=\"hljs-punctuation\">,<\/span> results<span class=\"hljs-operator\">$<\/span>acc<span class=\"hljs-punctuation\">)<\/span><\/span><\/pre>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt mu mv mw mx my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn fk bj\" id=\"4504\">Here\u2019s an example of visualization from Comet ML\u2019s platform showcasing the model\u2019s accuracy.<\/p>\n\n\n\n<figure class=\"wp-block-image oo op oq or os me lw lx paragraph-image\"><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*xDHVcNcGnprfRNAg8e0Pkg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading no np fr be nq nr ns nt nu nv nw nx ny nb nz oa ob nf oc od oe nj of og oh oi bj\" id=\"6a18\">Conclusion<\/h2>\n\n\n\n<p class=\"pw-post-body-paragraph mr ms fr be b mt oj mv mw mx ok mz na nb ol nd ne nf om nh ni nj on nl nm nn fk bj\" id=\"71d8\">In this article we used Keras with R to build a neural network that can classify text into two categories. In addition, we logged some metrics like loss, accuracy, and epochs to Comet ML\u2019s platform. This tutorial was just a simple introduction to how to use R to build NLP models while monitoring your experiments using Comet ML. Kindly visit Comet ML\u2019s <a class=\"af mq\" href=\"https:\/\/www.comet.com\/docs\/v2\/api-and-sdk\/r-sdk\/overview\/\" target=\"_blank\" rel=\"noopener ugc nofollow\">Official Documentation<\/a>to gain more insights on how to monitor your R projects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Text classification is an interesting application of natural language processing. It is a supervised learning methodology that predicts if a piece of text belongs to one category or the other. As a machine learning engineer, you start with a labeled data set that has vast amounts of text that have already been categorized. These algorithms [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[9,7],"tags":[],"coauthors":[165],"class_list":["post-8035","post","type-post","status-publish","format-standard","hentry","category-product","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Text Classification Using R, Keras, and Comet ML - Comet<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Text Classification Using R, Keras, and Comet ML\" \/>\n<meta property=\"og:description\" content=\"Text classification is an interesting application of natural language processing. It is a supervised learning methodology that predicts if a piece of text belongs to one category or the other. As a machine learning engineer, you start with a labeled data set that has vast amounts of text that have already been categorized. These algorithms [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-25T23:19:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:05:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg\" \/>\n<meta name=\"author\" content=\"Klurdy Studios\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Klurdy Studios\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Text Classification Using R, Keras, and Comet ML - Comet","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml","og_locale":"en_US","og_type":"article","og_title":"Text Classification Using R, Keras, and Comet ML","og_description":"Text classification is an interesting application of natural language processing. It is a supervised learning methodology that predicts if a piece of text belongs to one category or the other. As a machine learning engineer, you start with a labeled data set that has vast amounts of text that have already been categorized. These algorithms [&hellip;]","og_url":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2023-10-25T23:19:34+00:00","article_modified_time":"2025-04-24T17:05:08+00:00","og_image":[{"url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg","type":"","width":"","height":""}],"author":"Klurdy Studios","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Klurdy Studios","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\/"},"author":{"name":"Klurdy Studios","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/069e186ad4a5b6d6950292821ea0f37b"},"headline":"Text Classification Using R, Keras, and Comet ML","datePublished":"2023-10-25T23:19:34+00:00","dateModified":"2025-04-24T17:05:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\/"},"wordCount":897,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg","articleSection":["Product","Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml\/","url":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml","name":"Text Classification Using R, Keras, and Comet ML - Comet","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#primaryimage"},"thumbnailUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg","datePublished":"2023-10-25T23:19:34+00:00","dateModified":"2025-04-24T17:05:08+00:00","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#primaryimage","url":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg","contentUrl":"https:\/\/miro.medium.com\/v2\/resize:fit:700\/1*V9y2IkTtXlovTPiHESe-Cw.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/text-classification-using-r-keras-and-comet-ml#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"Text Classification Using R, Keras, and Comet ML"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/069e186ad4a5b6d6950292821ea0f37b","name":"Klurdy Studios","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/b1a3bf3caaa793aaad2da005b3ba38ba","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1635710213869-96x96.jpg","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2023\/08\/1635710213869-96x96.jpg","caption":"Klurdy Studios"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/brianklurdy-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8035"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8035\/revisions"}],"predecessor-version":[{"id":15483,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8035\/revisions\/15483"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8035"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}