/
tinydnn_cnn.Rmd
123 lines (108 loc) · 3.51 KB
/
tinydnn_cnn.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: "Convolutional Neural Network Example with tinydnn"
author: "Andrey Ogurtsov"
date: "February 25, 2017"
output:
prettydoc::html_pretty:
theme: cayman
highlight: github
number_sections: true
vignette: >
%\VignetteIndexEntry{Convolutional Neural Network Example with tinydnn}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r include=FALSE}
library(knitr)
knitr::opts_chunk$set(eval = FALSE)
```
This example shows how to train Convolutional Neural Networks (CNN) with
the **tinydnn** package. We will use the [MNIST](http://yann.lecun.com/exdb/mnist/)
dataset and the LeNet architecture
([mxnet realization](http://mxnet.io/tutorials/r/mnistCompetition.html)).
# Data
```{r}
# Download data from https://www.kaggle.com/c/digit-recognizer/data
train <- read.csv("train.csv")
train_x <- as.matrix(train[, -1])
train_y <- factor(train[, 1])
rm(train)
test <- read.csv("test.csv")
test_x <- as.matrix(test)
rm(test)
```
# Network Structure
Creating network structure is very straightforward; somewhat tricky part is setting right shapes for next layer after convolution or pool one.
```{r}
library(tinydnn)
net <- net_seq()
net %<%
fc(ncol(train_x),
ncol(train_x),
act = "identity") %<%
conv(in_width = 28,
in_height = 28,
window_width = 5,
window_height = 5,
in_channels = 1, # set to 1 for grayscale images
out_channels = 20, # number of filters
act = "tan_h") %<%
max_pool(in_width = 24, # smaller images after convolution
in_height = 24,
in_channels = 20, # number of filters in conv
pool_size_x = 2,
pool_size_y = 2,
stride_x = 2,
stride_y = 2,
activation = "identity") %<%
conv(in_width = 12,
in_height = 12,
window_width = 5,
window_height = 5,
in_channels = 20,
out_channels = 50,
act = "tan_h") %<%
max_pool(in_width = 8,
in_height = 8,
in_channels = 50,
pool_size_x = 2,
pool_size_y = 2,
stride_x = 2,
stride_y = 2,
activation = "identity") %<%
fc(800, 500, act = "tan_h") %<%
fc(500, nlevels(train_y), act = "softmax")
```
# Network Training
Train network with `batch_size = 100` and `epochs = 10`.
```{r}
set.seed(123)
net$fit(train_x,
train_y,
batch_size = 100,
epochs = 10,
verbose = TRUE)
```
# Making Predictions
```{r}
pred_train <- net$predict(train_x[1:3000, ], type = "class")
table(pred_train, train_y[1:3000])
# pred_train 0 1 2 3 4 5 6 7 8 9
# 0 296 0 1 0 0 0 0 1 0 1
# 1 0 339 0 0 0 0 0 0 1 0
# 2 1 1 324 1 1 1 0 1 1 0
# 3 0 0 0 267 0 1 0 1 1 0
# 4 0 0 1 0 290 0 0 0 0 0
# 5 0 0 0 2 0 284 0 0 0 2
# 6 1 1 0 0 1 1 307 0 1 1
# 7 1 0 1 0 0 0 0 298 0 4
# 8 0 1 0 0 0 0 0 1 274 0
# 9 0 0 0 0 3 1 0 0 2 282
pred_test <- net$predict(test_x, type = "class")
submission <- data.frame(ImageId = 1:nrow(test_x),
Label = pred_test)
write.csv(submission,
file = "submission.csv",
row.names = FALSE,
quote = FALSE)
```