{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Comet + R with nnet\n", "\n", "This notebook is based on:\n", "\n", "* https://www.rdocumentation.org/packages/nnet/versions/7.3-13/topics/nnet\n", "\n", "It attempts to learn to identify species of [Iris flowers](https://en.wikipedia.org/wiki/Iris_flower_data_set) based on some of their characteristics." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Started with Comet\n", "\n", "To get started with Comet and R, please see:\n", "https://www.comet.ml/docs/r-sdk/getting-started/\n", "\n", "Specifically, you need to create a .comet.yml file\n", "or add your Comet API key to create_experiment(). In this example, I've created a ~/.comet.yml file with these contents (replace items for your use):\n", "\n", "```yaml\n", "COMET_WORKSPACE: YOUR-COMET-USERNAME\n", "COMET_PROJECT_NAME: PROJECT-NAME\n", "COMET_API_KEY: YOUR-API-KEY\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learning the Iris Dataset\n", "\n", "R libraries needed for this notebook:\n", "\n", "```r\n", "install.packages(\"cometr\")\n", "install.packages(\"nnet\")\n", "install.packages(\"stringr\")\n", "install.packages(\"IRdisplay\")\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ok, now we are ready to machine learn. First we import the needed libraries:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "library(cometr)\n", "library(nnet)\n", "library(stringr)\n", "library(IRdisplay)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we create a Comet experiment marking what we would like to log to the server:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Experiment created: https://www.comet.ml/dsblank/cometr/ae316b3138be4dfdb02b305b1fc438c4 \n" ] } ], "source": [ "exp <- create_experiment(\n", " keep_active = TRUE,\n", " log_output = FALSE,\n", " log_error = FALSE,\n", " log_code = TRUE,\n", " log_system_details = TRUE,\n", " log_git_info = TRUE\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: the notebook source isn't logged through the experiment, but we'll log the entire notebook at the end. \n", "\n", "Let's tag the experiment, so that the experiment will be easy to select in the Comet UI:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "exp$add_tags(c(\"made with nnet\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iris Dataset\n", "\n", "Next, for this example, we sample the iris data:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "sample_size <- 25 # of each iris type\n", "total_size <- 50" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We note the sample_size as a hyperparameter:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "exp$log_parameter(\"sample_size\", sample_size)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And actually sample the dataset:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "ir <- rbind(iris3[,,1], iris3[,,2], iris3[,,3])\n", "targets <- class.ind(c(\n", " rep(\"s\", total_size),\n", " rep(\"c\", total_size),\n", " rep(\"v\", total_size))\n", ")\n", "samp <- c(\n", " sample(1:total_size, sample_size),\n", " sample((total_size + 1):(total_size * 2), sample_size),\n", " sample(((total_size * 2) + 1):(total_size * 3), sample_size)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take a look at one of the samples:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
This example demonstrates using the nnet library on the iris dataset.
\n", "\n", "See the Output tab for confusion matrix.
\n", "\n", "\n", "\n", "For help on the Comet R SDK, please see: www.comet.ml/docs/r-sdk/getting-started/
\n", "\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mark the experiment as created by R:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "exp$log_other(key = \"Created by\", value = \"cometr\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we show how you can display this experiment in the notebook:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "url <- exp$get_url()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_html(sprintf(\"\", url))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At this point, save your notebook. We'll then upload it as an asset:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "exp$upload_asset(\n", " \"Comet-R-nnet.ipynb\",\n", " type = \"notebook\",\n", ")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Comet experiment https://www.comet.ml/dsblank/cometr/ae316b3138be4dfdb02b305b1fc438c4 \n" ] } ], "source": [ "exp$print()\n", "exp$stop()" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 4 }