This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.R
167 lines (94 loc) · 3.08 KB
/
script.R
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# See deep_learning_with_keras_r.Rmd for explanations of everything!
set.seed(2018)
# Import packages we'll be using
library(keras)
# Prepare data
iris
iris <- iris[sample(nrow(iris)), ]
iris_labels <- as.numeric(iris$Species) - 1
iris_onehot <- to_categorical(iris_labels)
iris_onehot
# Build the model
model <- keras_model_sequential()
model %>%
layer_dense(15, activation = "sigmoid", input_shape = 4) %>%
layer_dense(3, activation = "softmax")
summary(model)
# Compile and fit
model %>%
compile(loss = "categorical_crossentropy", optimizer = "sgd", metrics = c("accuracy"))
model %>%
fit(as.matrix(iris[, -5]), iris_onehot, epochs = 50, batch_size = 20, validation_split = 0.2)
# Load walking data
walking <- readRDS("data/walking_data.rds")
dim(walking)
# Set up a quick plotting function
plot_series <- function(series) {
# x-channel
plot(series[, 1], type = "l", col = "red")
# y-channel
lines(series[, 2], col = "darkgreen")
# z-channel
lines(series[, 3], col = "blue")
}
plot_series(walking[100, , ])
# Import labels (they're stored separately!)
walking_labels <- readRDS("data/walking_labels.rds")
unique(walking_labels)
# Prepare partitions for training/testing
m <- nrow(walking)
indices <- sample(1:m, m)
train_indices <- indices[1:floor(m*0.6)]
val_indices <- indices[ceiling(m*0.6):floor(m*0.8)]
test_indices <- indices[ceiling(m*0.8):m]
X_train <- walking[train_indices, , ]
X_val <- walking[val_indices, , ]
X_test <- walking[test_indices, , ]
y_train <- to_categorical(walking_labels[train_indices])
y_val <- to_categorical(walking_labels[val_indices])
y_test <- to_categorical(walking_labels[test_indices])
# Create the model
model <- keras_model_sequential()
model %>%
layer_conv_1d(filters = 30, kernel_size = 40, strides = 2, activation = "relu", input_shape = c(260, 3))
model %>%
layer_max_pooling_1d(pool_size = 2)
model %>%
layer_conv_1d(filters = 40, kernel_size = 10, activation = "relu") %>%
layer_max_pooling_1d(pool_size = 2)
# Check model output
model$output_shape
model %>%
layer_flatten()
model$output_shape
model %>%
layer_dense(units = 100, activation = "sigmoid") %>%
layer_dense(units = 15, activation = "softmax")
# See what we've done!
summary(model)
# Compile and fit
model %>%
compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = c("accuracy"))
model %>%
fit(X_train, y_train, epochs = 10, batch_size = 100, validation_data = list(X_val, y_val))
# Prediction and reporting
y_pred <- model %>%
predict_classes(X_test)
table("Actual" = max.col(y_test) - 1, "Predicted" = y_pred)
# Looking inside the neurons
plot_filter <- function(model, layer, k) {
weights <- get_weights(model$layers[[layer]])[[1]][, , k]
plot_series(weights)
}
model %>%
plot_filter(1, 5)
# Autocorrelation of learned weights
plot_filter_corr <- function(model, layer, k) {
weights <- get_weights(model$layers[[layer]])[[1]][, , k]
corrs <- apply(weights, 2, function(x) {
acf(x, lag.max = nrow(weights), plot = FALSE)[["acf"]]
})
plot_series(corrs)
}
model %>%
plot_filter_corr(1, 5)