-
Notifications
You must be signed in to change notification settings - Fork 1
/
exemplo-06.R
49 lines (34 loc) · 983 Bytes
/
exemplo-06.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
library(keras)
x <- sample(c(letters, LETTERS, 0:9), size = 10000, replace = TRUE)
x_int <- as.integer(as.factor(x)) - 1L
y <- rnorm(10000, mean = x_int, sd = 2)
tapply(y, x, mean)
x2 <- to_categorical(x_int)
# Com one-hot encoding ----------------------------------------------------
input <- layer_input(shape = c(62))
output <- input %>%
layer_dense(32, use_bias = FALSE) %>%
layer_dense(1)
model <- keras_model(input, output)
model
model %>%
compile(
loss = "mse",
optimizer = "sgd"
)
model %>%
fit(x2, y, validation_split = 0.2)
# Com embedding -----------------------------------------------------------
input <- layer_input(shape = c(1))
output <- input %>%
layer_embedding(input_dim = 62, output_dim = 32) %>%
layer_flatten() %>%
layer_dense(1)
model_emb <- keras_model(input, output)
model_emb %>%
compile(
loss = "mse",
optimizer = "sgd"
)
model_emb %>%
fit(matrix(x_int, ncol = 1), y, validation_split = 0.2)