{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Работа с нейронными сетями на языке R (АГК, кластер-анализ)\n", "\n", "#### Ефиторов А.О.\n", "#### ЛАМОД НИИЯФ МГУ\n", "##### 2019" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Язык R\n", "\n", "* появился в 1993 году в Bell Labs (на тот момент принадлежащая AT&T)\n", "* открытый исходный код\n", "* Написан на R, C, Fortran\n", "\n", "## достоинства\n", "* Rstudio - IDE с интегрированным языком\n", "* Большое количество других инструментов, работающих с R (R Commander, Rattle)\n", "* Интерфейсы к другим языкам\n", "* CRAN - менеджер пакетов, поддерживается программистами и учеными\n", "\n", "## недостатки\n", "* Rstudio server pro - 10k$/year (аналог jupyterhub)\n", "* Специализированный язык (по сравнению с python/c и другими)" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# устанавливаем ядро R в ноутбук\n", "# https://github.com/IRkernel/IRkernel\n", "\n", "sudo apt install r-base # установка\n", "\n", "R # вход в интерпретатор\n", "\n", "install.packages('IRkernel', repos='http://cran.us.r-project.org')\n", "IRkernel::installspec() # регистрируем ядро в jupyter\n", "# ИЛИ\n", "IRkernel::installspec(user = FALSE) # для всей системы\n", "\n", "\n", "\n", "# устанавливаем KERAS\n", "# https://keras.rstudio.com/\n", "\n", "sudo apt install python-pip\n", "\n", "R # вход в интерпретатор\n", "\n", "install.packages('devtools', repos='http://cran.us.r-project.org')\n", "\n", "devtools::install_github(\"rstudio/keras\")\n", "library(keras)\n", "install_keras()\n", "\n", "\n", "# утанавливаем остальные пакеты\n", "\n", "install.packages(\"ChemometricsWithR\", repos='http://cran.us.r-project.org')\n", "install.packages(\"kohonen\", repos='http://cran.us.r-project.org')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "library(kohonen)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Attaching package: ‘ChemometricsWithR’\n", "\n", "The following objects are masked from ‘package:stats’:\n", "\n", " loadings, screeplot\n", "\n" ] } ], "source": [ "library(ChemometricsWithR)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "library(keras)\n", "mnist <- dataset_mnist()\n", "x_train <- mnist$train$x\n", "y_train <- mnist$train$y\n", "x_test <- mnist$test$x\n", "y_test <- mnist$test$y" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# reshape\n", "x_train <- array_reshape(x_train, c(nrow(x_train), 784))\n", "x_test <- array_reshape(x_test, c(nrow(x_test), 784))\n", "# rescale\n", "x_train <- x_train / 255\n", "x_test <- x_test / 255" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "y_train <- to_categorical(y_train, 10)\n", "y_test <- to_categorical(y_test, 10)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "model <- keras_model_sequential() \n", "model %>% \n", " layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% \n", " layer_dropout(rate = 0.4) %>% \n", " layer_dense(units = 128, activation = 'relu') %>%\n", " layer_dropout(rate = 0.3) %>%\n", " layer_dense(units = 10, activation = 'softmax')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "________________________________________________________________________________\n", "Layer (type) Output Shape Param # \n", "================================================================================\n", "dense (Dense) (None, 256) 200960 \n", "________________________________________________________________________________\n", "dropout (Dropout) (None, 256) 0 \n", "________________________________________________________________________________\n", "dense_1 (Dense) (None, 128) 32896 \n", "________________________________________________________________________________\n", "dropout_1 (Dropout) (None, 128) 0 \n", "________________________________________________________________________________\n", "dense_2 (Dense) (None, 10) 1290 \n", "================================================================================\n", "Total params: 235,146\n", "Trainable params: 235,146\n", "Non-trainable params: 0\n", "________________________________________________________________________________\n" ] } ], "source": [ "summary(model)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "model %>% compile(\n", " loss = 'categorical_crossentropy',\n", " optimizer = optimizer_rmsprop(),\n", " metrics = c('accuracy')\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "history <- model %>% fit(\n", " x_train, y_train, \n", " epochs = 30, batch_size = 128, \n", " validation_split = 0.2\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "