nanxstats / deep-learning-recipes Goto Github PK
View Code? Open in Web Editor NEW๐ฑ R implementation for selected machine learning methods with deep learning frameworks (Keras, Tensorflow)
License: MIT License
๐ฑ R implementation for selected machine learning methods with deep learning frameworks (Keras, Tensorflow)
License: MIT License
Hi Nan,
really helpful blogpost!
I am trying to extend your code and add classification to the triplet problem. In my case I am trying to learn a classifier for two MNIST digits.
First I am generating triplets of 1s and 2s like this:
# Load and prepare MNIST data
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
dim(x_train) <- c(nrow(x_train), 784)
dim(x_test) <- c(nrow(x_test), 784)
x_train <- x_train / 255
x_test <- x_test / 255
ok <- which(y_train %in% c(1,2))
x_tmp <- x_train[ok,]
y_tmp <- y_train[ok]
ones <- which(y_tmp == 1)
ones <- sample(ones)
twos <- which(y_tmp == 2)
twos <- sample(twos)
tmp_one <- do.call(rbind, lapply(ones[1:5000], function(x){
anchor <- x
positive <- sample(setdiff(ones, x), 1)
negative <- sample(twos, 1)
c(anchor, positive, negative)
}))
tmp_two <- do.call(rbind, lapply(twos[1:5000], function(x){
anchor <- x
positive <- sample(setdiff(twos, x), 1)
negative <- sample(ones, 1)
c(anchor, positive, negative)
}))
triplets <- rbind(tmp_one, tmp_two)
triplets <- triplets[sample(1:nrow(triplets)), ]
Next, I tried to adapt your implementation to my scenario:
# Define keras triplet model ####
loss_identity <- function(y_true, y_pred) k_mean(y_pred - 0 * y_true)
# margin-based triplet loss
loss_margin_triplet <- function(x) {
embed_user <- x[[1]]
embed_item_positive <- x[[2]]
embed_item_negative <- x[[3]]
loss <- k_maximum(
0.0,
k_sum(embed_user * embed_item_negative, axis = -1, keepdims = TRUE) -
k_sum(embed_user * embed_item_positive, axis = -1, keepdims = TRUE) +
10.0
)
loss
}
# input layers for items (positive and negative)
input_item_anchor <- layer_input(shape = c(784), name = "input_item_anchor")
input_item_positive <- layer_input(shape = c(784), name = "input_item_positive")
input_item_negative <- layer_input(shape = c(784), name = "input_item_negative")
layer_embed_item <- layer_embedding(
input_dim = 10000, output_dim = 2, name = "embed_item")
embed_item_anchor <- input_item_anchor %>%
layer_embed_item() %>%
layer_flatten()
embed_item_positive <- input_item_positive %>%
layer_embed_item() %>%
layer_flatten()
embed_item_negative <- input_item_negative %>%
layer_embed_item() %>%
layer_flatten()
# margin-based triplet loss is the output
loss <- list(embed_item_anchor, embed_item_positive, embed_item_negative) %>%
layer_lambda(loss_margin_triplet, output_shape = c(2))
# define model inputs/outputs
model <- keras_model(
inputs = c(input_item_anchor, input_item_positive, input_item_negative),
outputs = loss
)
model %>% compile(loss = loss_identity, optimizer = optimizer_nadam())
However, I am not quite sure how to continue. Why do we use the loss_identity() function in the compilation step? Dont we want the triplet loss?
Also, how would I add another classification layer to the network?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.