{"metadata": {"language_info": {"version": "3.4.2", "codemirror_mode": "r", "name": "R", "mimetype": "text/x-r-source", "file_extension": ".r", "pygments_lexer": "r"}, "kernelspec": {"language": "R", "display_name": "R", "name": "ir"}}, "nbformat": 4, "cells": [{"outputs": [], "source": ["# This R environment comes with all of CRAN preinstalled, as well as many other helpful packages\n", "# The environment is defined by the kaggle/rstats docker image: https://github.com/kaggle/docker-rstats\n", "# For example, here's several helpful packages to load in \n", "\n", "library(ggplot2) # Data visualization\n", "library(readr) # CSV file I/O, e.g. the read_csv function\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "system(\"ls ../input\")\n", "\n", "# Any results you write to the current directory are saved as output."], "metadata": {"_cell_guid": "83b48a63-e943-43a9-ba9e-f6f413d3b05f", "_uuid": "877b5ef4622f8ff84b6cc24c83ec1c467638fa2e"}, "cell_type": "code", "execution_count": 2}, {"outputs": [], "source": ["library(keras)"], "metadata": {"_cell_guid": "0ce6a781-2363-433e-bd43-8753380abbb0", "_uuid": "633b587a4adcfedd78c7aa3489fdb26200f87d63"}, "cell_type": "code", "execution_count": 1}, {"outputs": [], "source": ["# generate dummy data\n", "x_train <- array(runif(100 * 100 * 100 * 3), dim = c(100, 100, 100, 3))\n", "\n", "y_train <- runif(100, min = 0, max = 9) %>% \n", "  round() %>%\n", "  matrix(nrow = 100, ncol = 1) %>% \n", "  to_categorical(num_classes = 10)\n", "\n", "x_test <- array(runif(20 * 100 * 100 * 3), dim = c(20, 100, 100, 3))\n", "\n", "y_test <- runif(20, min = 0, max = 9) %>% \n", "  round() %>%\n", "  matrix(nrow = 20, ncol = 1) %>% \n", "  to_categorical(num_classes = 10)\n", "\n", "# create model\n", "model <- keras_model_sequential()\n", "\n", "# define and compile model\n", "# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.\n", "# this applies 32 convolution filters of size 3x3 each.\n", "model %>% \n", "  layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu', \n", "                input_shape = c(100,100,3)) %>% \n", "  layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu') %>% \n", "  layer_max_pooling_2d(pool_size = c(2,2)) %>% \n", "  layer_dropout(rate = 0.25) %>% \n", "  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>% \n", "  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>% \n", "  layer_max_pooling_2d(pool_size = c(2,2)) %>% \n", "  layer_dropout(rate = 0.25) %>% \n", "  layer_flatten() %>% \n", "  layer_dense(units = 256, activation = 'relu') %>% \n", "  layer_dropout(rate = 0.25) %>% \n", "  layer_dense(units = 10, activation = 'softmax') %>% \n", "  compile(\n", "    loss = 'categorical_crossentropy', \n", "    optimizer = optimizer_sgd(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = TRUE)\n", "  )\n", "  \n", "# train\n", "model %>% fit(x_train, y_train, batch_size = 32, epochs = 10)\n", "\n", "# evaluate\n", "score <- model %>% evaluate(x_test, y_test, batch_size = 32)\n", "score"], "metadata": {"_cell_guid": "609a1bc8-b26a-4bb9-8e90-26abb7132f5a", "_uuid": "27e5513b356ca7f05f712b3a38b5e1507ddbe8a9"}, "cell_type": "code", "execution_count": 3}, {"outputs": [], "source": [], "metadata": {}, "cell_type": "code", "execution_count": null}], "nbformat_minor": 1}