Created
June 19, 2021 13:39
-
-
Save dfalbel/608f00c0fdbe958bae8801ff6340b1b2 to your computer and use it in GitHub Desktop.
Multiple outputs Keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(keras) | |
library(tensorflow) | |
input <- layer_input(shape = list(365, 10)) | |
representation <- input %>% | |
layer_lstm(units = 32, input_shape = list(365, 10)) %>% | |
layer_dropout(rate = 0.2) | |
output1 <- representation %>% | |
layer_dense(units = 2, name = "out1") | |
output2 <- representation %>% | |
layer_dense(units = 2, name = "out2") | |
model <- keras_model(input, list(out1 = output1, out2 = output2)) | |
loss1 <- function(y_true, y_pred) { | |
tensorflow::tf$reduce_mean(y_pred) | |
} | |
loss2 <- function(y_true, y_pred) { | |
tensorflow::tf$reduce_mean(-y_pred) | |
} | |
model %>% compile( | |
optimizer = optimizer_adam(), | |
loss = list(out1 = loss1, out2 = loss2), | |
loss_weights = list(0.8, 0.2), | |
metrics = 'mape' | |
) | |
model %>% fit( | |
x = tf$random$uniform(shape = shape(100, 365, 10)), | |
y = list( | |
out1 = tf$random$uniform(shape = shape(100, 1)), | |
out2 = tf$random$uniform(shape = shape(100, 1)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment